简体   繁体   中英

What's wrong with my “import module based on string” file

- ModuleNotFoundError: No module named 'Example_Mission.py'; 'Example_Mission' is not a package

Here's my code.

for root, dirs, files in os.walk(os.getcwd()):
  for filename in files:
    if filename[len(filename)-3:len(filename)] == ".py":
      Mission = importlib.import_module("Example_Mission.py") # Seems useless in this big loop, but I'll change this later.
      Mission.Init()

I've added a blank __init__.py to the current dictionary, but I still get an error. Any clue why? (Some of it may seem useless, i'll implement it later)

All of the files mentioned are on the same folder.

The module named "Example_Mission" lives in a file called "Example_Mission.py", so you need to specify the module name, not the filename, as in:

Mission = importlib.import_module("Example_Mission")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM