Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named ***** in python

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?

like image 567
cubearth Avatar asked Oct 22 '10 00:10

cubearth


People also ask

How do I fix the ImportError No module named error in Python?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

What is ImportError in Python?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.


1 Answers

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 :

  • http://docs.python.org/tutorial/modules.html#the-module-search-path
  • http://docs.python.org/library/sys.html#sys.path

Also look at (PYTHONPATH) environment variable details here:

  • http://docs.python.org/using/cmdline.html#environment-variables
like image 123
pyfunc Avatar answered Sep 30 '22 04:09

pyfunc