无法从 site-packages 目录加载通过 pip 安装的 Python 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25276329/
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
Can't load Python modules installed via pip from site-packages directory
提问by Chase McCoy
I am trying to install and use the Evernote module (https://github.com/evernote/evernote-sdk-python) . I ran pip install evernoteand it says that the installation worked.
我正在尝试安装和使用 Evernote 模块 ( https://github.com/evernote/evernote-sdk-python)。我跑了pip install evernote,它说安装成功了。
I can confirm that the evernote module exists in /usr/local/lib/python2.7/site-packages. However, when I try to run python -c "import evernote"I get the following error:
我可以确认印象笔记模块存在于/usr/local/lib/python2.7/site-packages. 但是,当我尝试运行时,出现python -c "import evernote"以下错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named evernote
This is the contents of my .bash-profile:
这是我的内容.bash-profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# Setting PATH for Python 3.3
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.3/bin:${PATH}"
export PATH
export PATH=$PATH:/usr/local/bin/
I am having this same problem with other modules installed with pip. Help?
我在安装其他模块时遇到了同样的问题pip。帮助?
EDIT: I am a super newbie and have not edited that .bash-profilefile.
编辑:我是一个超级新手,还没有编辑过那个.bash-profile文件。
EDIT: python -c 'import sys; print "\n".join(sys.path)'Outputs the following:
编辑:python -c 'import sys; print "\n".join(sys.path)'输出以下内容:
/Library/Python/2.7/site-packages/setuptools-1.3.2-py2.7.egg
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
EDIT:I seemed to have made progress towards a solution by adding export PYTHONPATH=“/usr/local/lib/python2.7/site-packages”to my .bash_profilefile. However, now when I run python -c 'from evernote.api.client import EvernoteClient'it tries to import oauth2, which fails with the same error. The ouath2 module is present in the module directory.
编辑:通过添加export PYTHONPATH=“/usr/local/lib/python2.7/site-packages”到我的.bash_profile文件中,我似乎在解决方案方面取得了进展。但是,现在当我运行python -c 'from evernote.api.client import EvernoteClient'它时尝试导入 oauth2,但失败并出现相同的错误。ouath2 模块存在于模块目录中。
采纳答案by Jason S
/usr/bin/pythonis the executable for the python that comes with OS X. /usr/local/libis a location for user-installed programs only, possibly from Python.org or Homebrew. So you're mixing different Python installs, and changing the python path is only a partial workaround for different packages being installed for different installations.
/usr/bin/python是 OS X 附带的 python 的可执行文件。/usr/local/lib仅用于用户安装的程序的位置,可能来自 Python.org 或 Homebrew。因此,您正在混合不同的 Python 安装,并且更改 python 路径只是针对为不同安装安装的不同软件包的部分解决方法。
In order to make sure you use the pipassociated with a particular python, you can run python -m pip install <pkg>, or go look at what the pipon your path is, or is symlinked to.
为了确保您使用pip与特定 python 相关联的 ,您可以运行python -m pip install <pkg>,或查看pip您的路径上的内容,或符号链接到的内容。
回答by Chase McCoy
I figured it out! I added this line:
我想到了!我添加了这一行:
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
to my .bash_profileand now I can import modules stored in that directory. Thanks for everyone who answered.
我.bash_profile现在我可以导入存储在该目录中的模块。感谢所有回答的人。
回答by cnaak
None of this helped me with my similar problem. Instead, I had to fix the newly installed files permissionsto be able to import. This is usually an obvious thing, but not so much when you use sudowhen installing module/packages.
这些都没有帮助我解决类似的问题。相反,我必须修复新安装的文件权限才能导入。这通常是很明显的事情,但当您sudo在安装模块/包时使用时就不那么明显了。
回答by prarit lamba
I faced similar problem,its related to /usr/local/lib/python2.7/site-packages had no read or write permission for group and other, and they were owned by root. This means that only the root user could access them.
我遇到了类似的问题,它与 /usr/local/lib/python2.7/site-packages 相关,没有组和其他人的读或写权限,它们归 root 所有。这意味着只有 root 用户可以访问它们。
Try this:
尝试这个:
$ sudo chmod -R go+rX /usr/local/lib/python2.7/site-packages
回答by amr
Simply just type in terminal:
只需在终端中输入:
sudo pip install pillow
and type import(whatever you like) or type from(whatever you like) import(whatever you like).
并键入import(无论您喜欢什么)或键入from(无论您喜欢import什么)(无论您喜欢什么)。

