尝试运行 Python 脚本时出现“ImportError: No module named”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15514593/
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" when trying to run Python script
提问by CodeOcelot
I'm trying to run a script that launches, amongst other things, a python script. I get a ImportError: No module named ..., however, if I launch ipython and import the same module in the same way through the interpreter, the module is accepted.
我正在尝试运行一个脚本,该脚本启动一个 python 脚本。我收到一个 ImportError: No module named ...,但是,如果我启动 ipython 并通过解释器以相同的方式导入相同的模块,则该模块被接受。
What's going on, and how can I fix it? I've tried to understand how python uses PYTHONPATH but I'm thoroughly confused. Any help would greatly appreciated.
这是怎么回事,我该如何解决?我试图了解 python 如何使用 PYTHONPATH,但我很困惑。任何帮助将不胜感激。
采纳答案by tyleha
This issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does(be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run
出现此问题的原因是命令行 IPython 解释器使用您当前路径的方式与单独进程的方式(无论是 IPython 笔记本、外部进程等)。IPython 将查找要导入的模块,这些模块不仅可以在您的 sys.path 中找到,还可以在您当前的工作目录中找到。从命令行启动解释器时,您正在操作的当前目录与您启动 ipython 的目录相同。如果您运行
import os
os.getcwd()
you'll see this is true.
你会发现这是真的。
However, let's say you're using an ipython notebook, run os.getcwd()and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dirsetting).
但是,假设您正在使用 ipython 笔记本,运行os.getcwd()并且您当前的工作目录是您在 ipython_notebook_config.py 文件(通常使用c.NotebookManager.notebook_dir设置)中告诉笔记本进行操作的文件夹。
The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:
解决方案是为 python 解释器提供你的模块的路径。最简单的解决方案是将该路径附加到您的 sys.path 列表中。在您的笔记本中,首先尝试:
import sys
sys.path.append('my/path/to/module/folder')
import module-of-interest
If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.
如果这不起作用,您将遇到与导入路径无关的其他问题,您应该提供有关您的问题的更多信息。
The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unixor Windows.
解决这个问题的更好(也更持久)的方法是设置你的PYTHONPATH,它为解释器提供了额外的目录来查找 python 包/模块。将 PYTHONPATH 编辑或设置为全局变量取决于操作系统,此处针对Unix或Windows进行了详细讨论。
回答by pandorabob
Before installing ipython, I installed modules through easy_install; say sudo easy_install mechanize.
在安装ipython之前,我通过easy_install安装了模块;说sudo easy_install mechanize。
After installing ipython, I had to re-run easy_install for ipython to recognize the modules.
安装 ipython 后,我不得不为 ipython 重新运行 easy_install 以识别模块。
回答by acannon828
Doing sys.path.append('my-path-to-module-folder')will work, but to avoid having to do this in IPython every time you want to use the module, you can add export PYTHONPATH="my-path-to-module-folder:$PYTHONPATH"to your ~/.bash_profilefile.
这样做sys.path.append('my-path-to-module-folder')会奏效,但是为了避免每次要使用模块时都必须在 IPython 中执行此操作,您可以添加export PYTHONPATH="my-path-to-module-folder:$PYTHONPATH"到~/.bash_profile文件中。
回答by Mohideen bin Mohammed
Just create an empty python file with the name __init__.pyunder the folder which showing error, while you running the python project.
在__init__.py运行 python 项目时,只需在显示错误的文件夹下创建一个名称为空的 python 文件。
回答by z0r
Make sure they are both using the same interpreter. This happened to me on Ubuntu:
确保他们都使用相同的解释器。这在 Ubuntu 上发生在我身上:
$ ipython3 -c 'import sys; print(sys.version)'
3.4.2 (default, Jun 19 2015, 11:34:49) \n[GCC 4.9.1]
$ python3 -c 'import sys; print(sys.version)'
3.3.0 (default, Nov 27 2012, 12:11:06) \n[GCC 4.6.3]
And sys.pathwas different between the two interpreters. To fix it, I removed Python 3.3.
并且sys.path在两个口译员之间是不同的。为了修复它,我删除了 Python 3.3。
回答by peter n
Had a similar problem, fixed it by calling python3instead of python, my modules were in Python3.5.
有一个类似的问题,通过调用python3而不是修复它python,我的模块在 Python3.5 中。
回答by Jesse
The main reason is the sys.paths of Python and IPython are different.
主要原因是Python和IPython的sys.paths不同。
Please refer to lucypark link, the solution works in my case. It happen when install opencv by
请参阅lucypark 链接,该解决方案适用于我的情况。安装opencv时会发生这种情况
conda install opencv
conda install opencv
And got import error in iPython, There are three steps to solve this issue:
并在 iPython 中出现导入错误,解决此问题的三个步骤:
import cv2
ImportError: ...
1. Check path in Python and iPython with following command
1.使用以下命令检查Python和iPython中的路径
import sys
sys.path
You will find different result from Python and Jupyter. Second step, just use sys.path.appendto fix the missed path by try-and-error.
你会发现与 Python 和 Jupyter 不同的结果。第二步,仅用于sys.path.append通过尝试和错误来修复丢失的路径。
2. Temporary solution
2. 临时解决方案
In iPython:
在 iPython 中:
import sys
sys.path.append('/home/osboxes/miniconda2/lib/python2.7/site-packages')
import cv2
the ImportError:..issue solved
该ImportError:..问题的解决
3. Permanent solution
3. 永久解决方案
Create an iPython profile and set initial append:
创建一个 iPython 配置文件并设置初始附加:
In bash shell:
在 bash 外壳中:
ipython profile create
... CHECK the path prompted , and edit the prompted config file like my case
vi /home/osboxes/.ipython/profile_default/ipython_kernel_config.py
In vi, append to the file:
在 vi 中,附加到文件:
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/home/osboxes/miniconda2/lib/python2.7/site-packages")'
]
DONE
完毕
回答by user3233042
Remove pathliband reinstall it. Delete the pathlib in sitepackagesfolder and reinstall the pathlib package by using pip command:
删除pathlib并重新安装它。删除sitepackages文件夹中的pathlib并使用pip命令重新安装pathlib包:
pip install pathlib
回答by pomber
This is how I fixed it:
这是我修复它的方式:
import os
import sys
module_path = os.path.abspath(os.getcwd() + '\..')
if module_path not in sys.path:
sys.path.append(module_path)
回答by kaleissin
I found yet another source of this discrepancy:
我发现了这种差异的另一个来源:
I have ipython installed both locally and in commonly in virtualenvs. My problem was that, inside a newly made virtualenv with ipython, the system ipython was picked up, which was a different version than the python and ipython in the virtualenv (a 2.7.x vs. a 3.5.x), and hilarity ensued.
我在本地和 virtualenvs 中都安装了 ipython。我的问题是,在新制作的带有 ipython 的 virtualenv 中,系统 ipython 被选中,它与 virtualenv 中的 python 和 ipython 版本不同(2.7.x 与 3.5.x),随之而来的是欢呼声。
I think the smart thing to do whenever installing something that will have a binary in yourvirtualenv/binis to immediately run rehashor similar for whatever shell you are using so that the correct python/ipython gets picked up. (Gotta check if there are suitable pippost-install hooks...)
我认为每当安装将有二进制文件的东西时,明智的做法yourvirtualenv/bin是立即运行rehash或类似您正在使用的任何 shell,以便选择正确的 python/ipython。(必须检查是否有合适pip的安装后挂钩...)

