开发者

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

开发者 https://www.devze.com 2023-01-20 17:06 出处:网络
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 impor

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 <mod开发者_StackOverflowule>
    import reduc
ImportError: No module named reduc

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号