user1007632
user1007632

Reputation: 159

Having problems with importing a module

I use os.chdir() to change the current directory where my recommendations.py file is. Then I type Import recommendations and I get error:

ImportError: No module named recommendations.

What could be the problem?

Upvotes: 3

Views: 99

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599600

Python doesn't use the current working directory to import modules, except insofar as it adds the initial directory to the path when starting up. You need to add the directory to your Python path, either by setting the PYTHONPATH environment variable or by modifying sys.path.

Upvotes: 2

icktoofay
icktoofay

Reputation: 129011

Python only looks in the initial working directory (and a few other places) by default. If you change the current directory, insert the new working directory into the search path:

sys.path.insert(0, os.getcwd())

Upvotes: 5

Related Questions