Python 无法导入明确安装的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14295680/
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
Unable to import a module that is definitely installed
提问by roy
After installing mechanize, I don't seem to be able to import it.
安装mechanize 后,我似乎无法导入它。
I have tried installing from pip, easy_install, and via python setup.py installfrom this repo: https://github.com/abielr/mechanize. All of this to no avail, as each time I enter my Python interactive I get:
我尝试从 pip、easy_install 和通过python setup.py install这个 repo 安装:https: //github.com/abielr/mechanize。所有这些都无济于事,因为每次我进入 Python 交互时,我都会得到:
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>>
The installations I ran previously reported that they had completed successfully, so I expect the import to work. What could be causing this error?
我之前运行的安装报告说它们已成功完成,因此我希望导入能够正常工作。什么可能导致此错误?
采纳答案by Paul Wang
In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!
就我而言,这是权限问题。该软件包以某种方式仅以 root rw 权限安装,其他用户无法 rw 到它!
回答by Jesse Briggs
If you learn how to use virtualenv (which is pretty dead-simple), you will have less of these issues. You'll just source the virtualenv and then you will be using local (to the project) packages.
如果您学习了如何使用 virtualenv(这非常简单),您将不会遇到这些问题。您只需获取 virtualenv,然后您将使用本地(项目)包。
It solves a lot of headache for me with paths, versions, etc.
它通过路径、版本等解决了我很多头痛的问题。
回答by Ali Afshar
The Python import mechanism works, really, so, either:
Python 导入机制确实有效,因此,要么:
- Your PYTHONPATH is wrong,
- Your library is not installed where you think it is
- You have another library with the same name masking this one
- 你的 PYTHONPATH 是错误的,
- 您的库未安装在您认为的位置
- 你有另一个同名的图书馆掩盖了这个
回答by Ryan Artecona
When you install via easy_installor pip, is it completing successfully? What is the full output? Which python installation are you using? You may need to use sudobefore your installation command, if you are installing modules to a system directory (if you are using the system python installation, perhaps). There's not a lot of useful information in your question to go off of, but some tools that will probably help include:
当您通过easy_install或安装时pip,是否成功完成?什么是完整输出?你使用的是哪个python安装?sudo如果您将模块安装到系统目录(如果您使用系统 python 安装,也许),您可能需要在安装命令之前使用。您的问题中没有很多有用的信息可供参考,但一些可能会有所帮助的工具包括:
echo $PYTHONPATHand/orecho $PATH: when importing modules, Python searches one of these environment variables (lists of directories,:delimited) for the module you want. Importing problems are often due to the right directory being absent from these listswhich python,which pip, orwhich easy_install: these will tell you the location of each executable. It may help to know.Use virtualenv, like @JesseBriggs suggests. It works very well with
pipto help you isolate and manage the modules and environment for separate Python projects.
echo $PYTHONPATH和/或echo $PATH:在导入模块时,Python 会在这些环境变量之一(目录列表,:分隔)中搜索您想要的模块。导入问题通常是由于这些列表中缺少正确的目录which python,which pip, 或which easy_install: 这些将告诉您每个可执行文件的位置。了解它可能会有所帮助。使用virtualenv,就像@JesseBriggs 建议的那样。它可以很好
pip地帮助您隔离和管理单独的 Python 项目的模块和环境。
回答by user1552891
It's the python path problem.
这是python路径问题。
In my case, I have python installed in:
就我而言,我安装了 python:
/Library/Frameworks/Python.framework/Versions/2.6/bin/python,
/Library/Frameworks/Python.framework/Versions/2.6/bin/python,
and there is no site-packages directory within the python2.6.
并且python2.6中没有site-packages目录。
The package(SOAPpy) I installed by pip is located
我通过pip安装的包(SOAPpy)位于
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
And site-package is not in the python path, all I did is add site-packages to PYTHONPATH permanently.
而且站点包不在 python 路径中,我所做的只是将站点包永久添加到 PYTHONPATH。
- Open up Terminal
- Type open .bash_profile
In the text file that pops up, add this line at the end:
export PYTHONPATH=$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
- Save the file, restart the Terminal, and you're done
- 打开终端
- 输入 open .bash_profile
在弹出的文本文件中,在最后添加这一行:
export PYTHONPATH=$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
- 保存文件,重新启动终端,你就完成了
回答by Dan H
I had the same problem: script with import coloramawas throwing and ImportError, but sudo pip install coloramawas telling me "package already installed".
我有同样的问题:脚本import colorama抛出和导入错误,但sudo pip install colorama告诉我“包已经安装”。
My fix: run pip without sudo: pip install colorama. Then pip agreed it needed to be installed, installed it, and my script ran.
我的解决方法:在没有 sudo 的情况下运行 pip : pip install colorama。然后 pip 同意需要安装它,安装它,然后我的脚本运行。
My environment is Ubuntu 14.04 32-bit; I think I saw this before and after I activated my virtualenv.
我的环境是 Ubuntu 14.04 32 位;我想我在激活 virtualenv 之前和之后都看到了这一点。
UPDATE: even better, use python -m pip install <package>. The benefit of this is, since you are executing the specific version of python that you want the package in, pip will unequivocally install the package in to the "right" python. Again, don'tuse sudo in this case... then you get the package in the right place, but possibly with (unwanted) root permissions.
更新:更好的是,使用python -m pip install <package>. 这样做的好处是,由于您正在执行要在其中安装包的特定版本的 python,pip 会明确地将包安装到“正确的”python 中。同样,在这种情况下不要使用 sudo ……然后您将包放在正确的位置,但可能具有(不需要的)root 权限。
回答by ivanlan
I had this exact problem, but none of the answers above worked. It drove me crazy until I noticed that sys.path was different after I had imported from the parent project. It turned out that I had used importlib to write a little function in order to import a file not in the project hierarchy. Bad idea: I forgot that I had done this. Even worse, the import process mucked with the sys.path--and left it that way. Verybad idea.
我遇到了这个确切的问题,但上面的答案都没有奏效。它让我发疯,直到我注意到从父项目导入后 sys.path 是不同的。原来,我使用 importlib 编写了一个小函数,以便导入不在项目层次结构中的文件。坏主意:我忘了我做过这件事。更糟糕的是,导入过程与 sys.path 混为一谈——并保持原样。很糟糕的主意。
The solution was to stop that, and simply put the file I needed to import into the project. Another approach would have been to put the file into its own project, as it needs to be rebuilt from time to time, and the rebuild may or may not coincide with the rebuild of the main project.
解决方案是停止它,只需将我需要导入的文件放入项目中。另一种方法是将文件放入自己的项目中,因为它需要不时重建,并且重建可能会或可能不会与主项目的重建同时发生。
回答by Locane
I have been banging my head against my monitor on this until a young-hip intern told me the secret is to "python setup.py install" inside the module directory.
我一直把头撞在显示器上,直到一个年轻的实习生告诉我秘密是在模块目录中安装“python setup.py install” 。
For some reason, running the setup from there makes it just work.
出于某种原因,从那里运行设置使其正常工作。
To be clear, if your module's name is "foo":
需要明确的是,如果您的模块名称是“foo”:
[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root 118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root 93 Jun 21 15:23 foo
-rw-r--r-- 1 root root 416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->
If you try to run setup.py from any other directory by calling out its path, you end up with a borked install.
如果您尝试通过调用其路径来从任何其他目录运行 setup.py,您最终会遇到一个无聊的安装。
DOES NOT WORK:
不起作用:
python /root/foo/setup.py install
DOES WORK:
有效:
cd /root/foo
python setup.py install
回答by George Weber
I couldn't get my PYTHONPATH to work properly. I realized adding exportfixed the issue:
我无法让我的 PYTHONPATH 正常工作。我意识到添加export解决了这个问题:
(did work)
(确实有效)
export PYTHONPATH=$PYTHONPATH:~/test/site-packages
vs.
对比
(did not work)
(不工作)
PYTHONPATH=$PYTHONPATH:~/test/site-packages
回答by Patrick B.
I had this problem with 2.7 and 3.5 installed on my system trying to test a telegram bot with Python-Telegram-Bot.
我在系统上安装了 2.7 和 3.5 时遇到了这个问题,试图用Python-Telegram-Bot测试电报机器人。
I couldn't get it to work after installing with pip and pip3, with sudo or without. I always got:
使用 pip 和 pip3 安装后,无论是否使用 sudo,我都无法让它工作。我总是得到:
Traceback (most recent call last):
File "telegram.py", line 2, in <module>
from telegram.ext import Updater
File "$USER/telegram.py", line 2, in <module>
from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package
Reading the error message correctly tells me that python is looking in the current directory for a telegram.py. And right, I had a script lying there called telegram.py and this was loaded by python when I called import.
正确阅读错误消息告诉我,python 正在当前目录中查找telegram.py. 是的,我有一个名为 telegram.py 的脚本,当我调用import.
Conclusion, make sure you don't have any package.pyin your current working dir when trying to import. (And read error message thoroughly).
结论,package.py在尝试导入时,请确保您当前的工作目录中没有任何内容。(并仔细阅读错误消息)。

