0

If one of my python files is called hello.py, is there a way to import it to my other python file, called main.py, in the following way:

main.py

name = "hello"
import name

I get the following error:

ModuleNotFoundError: No module named 'name'

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Newbie101
  • 501
  • 2
  • 6
  • 17

2 Answers2

1

use:

import importlib

importlib.import_module(name)
dallonsi
  • 1,299
  • 1
  • 8
  • 29
-1

Other create python project. for example answer.py in this import hello.py