16

I am very new to python, about one month, and am trying to figure out how the importing works in python. I was told that I can import any 'module' that has Python code in it. So I am trying to import a module just to try it out, but I keep getting an 'ImportError: No module named redue'. This is an example of the python shell:

>>> import os
>>> os.chdir('C:\Users\Cube\Documents\Python')
>>> for file in os.listdir(os.getcwd()):
     print file
pronounce.py
pronounce.pyc
readwrite.py
rectangle.py
reduc.py

>>> import reduc

Traceback (most recent call last):
   File "<pyshell#32>", line 1, in <module>
    import reduc
ImportError: No module named reduc

What am I doing wrong? I am I overlooking something, or was I just wrongly informed?

sth
  • 222,467
  • 53
  • 283
  • 367
cubearth
  • 1,153
  • 4
  • 15
  • 30

1 Answers1

16

These files are not on sys.path. It should have been though.

If you want to access them from the interpreter, you will need to add the location to sys.path

>>> import sys
>>> print sys.path
>>> sys.path.append('C:\\Users\\Cube\\Documents\\Python')
>>> import reduc

You could also include the path in environment variable - PYTHONPATH

See the details on module search path here :

Also look at (PYTHONPATH) environment variable details here:

pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 2
    Isn't the current working directory usually in the search path? – sth Oct 22 '10 at 00:19
  • @sth: It should have worked! It is difficult to check out why. So I just wanted to ensure that it is on sys.path and if there are other errors than we could follow up. – pyfunc Oct 22 '10 at 00:23