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?
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With