简体   繁体   English

无法从site-packages目录加载通过pip安装的Python模块

[英]Can't load Python modules installed via pip from site-packages directory

I am trying to install and use the Evernote module ( https://github.com/evernote/evernote-sdk-python ) . 我正在尝试安装和使用Evernote模块( https://github.com/evernote/evernote-sdk-python )。 I ran pip install evernote and it says that the installation worked. 我运行了pip install evernote ,它说安装有效。

I can confirm that the evernote module exists in /usr/local/lib/python2.7/site-packages . 我可以确认Evernote模块存在于/usr/local/lib/python2.7/site-packages However, when I try to run python -c "import evernote" I get the following error: 但是,当我尝试运行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 . 我在pip安装的其他模块也遇到了同样的问题。 Help? 救命?

EDIT: I am a super newbie and have not edited that .bash-profile file. 编辑:我是一个超级新手,还没有编辑该.bash-profile文件。

EDIT: python -c 'import sys; print "\\n".join(sys.path)' 编辑: python -c 'import sys; print "\\n".join(sys.path)' 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_profile file. 编辑:通过向我的.bash_profile文件中添加export PYTHONPATH=“/usr/local/lib/python2.7/site-packages” ,我似乎在解决方案方面取得了进展。 However, now when I run python -c 'from evernote.api.client import EvernoteClient' it tries to import oauth2, which fails with the same error. 但是,现在,当我运行python -c 'from evernote.api.client import EvernoteClient'它尝试导入oauth2,但失败并出现相同的错误。 The ouath2 module is present in the module directory. ouath2模块位于模块目录中。

/usr/bin/python is the executable for the python that comes with OS X. /usr/local/lib is a location for user-installed programs only, possibly from Python.org or Homebrew. /usr/bin/python是OS X随附的python的可执行文件。/ /usr/local/lib是仅用于用户安装程序的位置,可能来自Python.org或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. 因此,您要混合使用不同的Python安装,并且针对不同的安装所安装的不同软件包,更改python路径只是部分解决方法。

In order to make sure you use the pip associated with a particular python, you can run python -m pip install <pkg> , or go look at what the pip on your path is, or is symlinked to. 为了确保您使用与特定python相关联的pip ,可以运行python -m pip install <pkg> ,或者查看路径上的pip是什么或被符号链接到。

I figured it out! 我想到了! I added this line: 我添加了这一行:

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages

to my .bash_profile and now I can import modules stored in that directory. 到我的.bash_profile ,现在我可以导入存储在该目录中的模块了。 Thanks for everyone who answered. 感谢所有回答的人。

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. 我遇到了类似的问题,它与/usr/local/lib/python2.7/site-packages有关,没有对group和other的读写权限,并且它们是root用户拥有的。 This means that only the root user could access them. 这意味着只有root用户才能访问它们。

Try this: 尝试这个:

$ sudo chmod -R go+rX /usr/local/lib/python2.7/site-packages

None of this helped me with my similar problem. 这些都没有帮助我解决类似的问题。 Instead, I had to fix the newly installed files permissions to be able to import. 相反,我必须修复新安装的文件权限才能导入。 This is usually an obvious thing, but not so much when you use sudo when installing module/packages. 这通常是显而易见的事情,但是在安装模块/软件包时使用sudo时并没有那么多。

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 (无论您喜欢什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM