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
  • 97,316
  • 47
  • 222
  • 267
Newbie101
  • 491
  • 1
  • 6
  • 16

2 Answers2

1

use:

import importlib

importlib.import_module(name)
dallonsi
  • 1,238
  • 1
  • 6
  • 24
-1

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