0

I want to make a function that loads all the .py files in a directory, and imports them using

__import__(), but I keep getting an ImportError: No module named toolboxtool1.

This is file structure:

project/dirreader.py
project/tools/toolboxtool1.py
project/tools/toolboxtool2.py
project/tools/toolboxtool3.py

What am I doing wrong?

import os
os.chdir(os.getcwd()+"/tools/")
stuff = os.listdir(os.getcwd())
for i in range(0,len(stuff)):
    if stuff[i][-3:] == ".py":
        stuff[i] = stuff[i][:-3]
    else:
        pass
modules = map(__import__, stuff)
1
  • What is the text of your import error? It seems like it could be a relative path problem
    – Geoff Genz
    Feb 7, 2014 at 16:14

1 Answer 1

1

Try prefixing the module names with "tools."

stuff[i] = 'tools.' + stuff[i][:-3]

because the modules you are trying to import are inside tools module package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.