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 Commented 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.