导入错误:python 中没有名为 ***** 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3992952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named ***** in python
提问by cubearth
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:
我对 python 很陌生,大约一个月,我试图弄清楚 python 中的导入是如何工作的。有人告诉我,我可以导入任何包含 Python 代码的“模块”。所以我试图导入一个模块只是为了尝试一下,但我不断收到“导入错误:没有名为 redue 的模块”。这是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?
我究竟做错了什么?我是否忽略了某些东西,还是我被错误地告知了?
采纳答案by pyfunc
These files are not on sys.path. It should have been though.
这些文件不在 sys.path 中。不过应该是的。
If you want to access them from the interpreter, you will need to add the location to sys.path
如果要从解释器访问它们,则需要将位置添加到 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
您还可以在环境变量中包含路径 - 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
- 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:
另请在此处查看(PYTHONPATH)环境变量详细信息:

