python - python Pip安装未安装到正确的目录中?

我使用sudo pip install无法将它安装到以下目录中:


/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/



这样我就可以使用python导入模块

我已经


sudo pip install scikit-learn --upgrade



结果


Requirement already up-to-date: scikit-learn in /usr/local/lib/python2.7/site-packages


Cleaning up...



如何让sudo pip install 安装到正确的目录中?

另外我尝试了

sudo pip install Scrappy

我得到以下消息


new-host-2:site-packages Chris$ sudo pip install Scrapy


Password:


Requirement already satisfied (use --upgrade to upgrade): Scrapy in /usr/local/lib/python2.7/site-packages


Requirement already satisfied (use --upgrade to upgrade): Twisted>=10.0.0 in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): w3lib>=1.8.0 in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): queuelib in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): pyOpenSSL in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.9 in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): six>=1.5.2 in /usr/local/lib/python2.7/site-packages (from Scrapy)


Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /usr/local/lib/python2.7/site-packages (from Twisted>=10.0.0->Scrapy)


Requirement already satisfied (use --upgrade to upgrade): cryptography>=0.2.1 in /usr/local/lib/python2.7/site-packages (from pyOpenSSL->Scrapy)


Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/lib/python2.7/site-packages (from zope.interface>=3.6.0->Twisted>=10.0.0->Scrapy)


Requirement already satisfied (use --upgrade to upgrade): cffi>=0.8 in /usr/local/lib/python2.7/site-packages (from cryptography>=0.2.1->pyOpenSSL->Scrapy)


Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python2.7/site-packages (from cffi>=0.8->cryptography>=0.2.1->pyOpenSSL->Scrapy)



两个实例都表明它已安装但不正确,例如在python中运行以下导入时:


import scrapy


---------------------------------------------------------------------------


ImportError Traceback (most recent call last)


<ipython-input-6-51c73a18167b> in <module>()


----> 1 import scrapy



ImportError: No module named scrapy



我已经尝试了以下操作:


sudo ln -s /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/



时间:

看起来你安装了多个版本的python,而且使用了错误的版本。

首先,要知道你使用的python版本,只需键入which python ,你应该看到:


which python


/Library/Frameworks/Python.framework/Versions/2.7/bin/python



如果你要使用正确的python版本,或者:


which python


/usr/bin/python



要使pip进入正确的版本,你首先必须更改路径:


 export PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python:${PATH}



pip可执行文件实际上是一个python脚本。

默认情况下,它包含(在Linux上):


#!/usr/bin/python


# EASY-INSTALL-ENTRY-SCRIPT: 'pip==1.5.6','console_scripts','pip'


__requires__ = 'pip==1.5.6'


import sys


from pkg_resources import load_entry_point



if __name__ == '__main__':


 sys.exit(


 load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()


 )



如果macOS中有相同的值,pip总是使用/usr/bin/python

但这是默认的,你仍然可以通过编辑文件或者使用python来提供所需的python版本。

如果which python返回/usr/bin/python,那么在安装出现了版本错误,如果是 /Library/Frameworks/Python.framework/Versions/2.7/bin/python你可以直接调用:


sudo python `which pip` install scikit-learn --upgrade



原因是sudo正在重置你所有的环境变量,要使它正常工作,最简单的方法是使用:


sudo -E pip install scikit-learn --upgrade



或者


sudo -E python `which pip` install scikit-learn --upgrade



取决于你的设置。

2你应该怎么做

更好的方法是安装virtualenv 。

最好使用virtualenv,这样你就可以为每个项目设置一个自定义环境,这是理想的维护方法,因为每个项目和应用程序的更新都不会干扰其他项目。

这是我在Windows上工作的。 原因是安装了多个python

  • 使用正确的python更新路径
  • 使用卸载pip python -m pip uninstall pip setuptools
  • 未定义

确保你的版本与python版本匹配。

要获取你的python版本:

python -V

然后安装正确的pip 。

pip-2.5 install. . .

pip-2.7 install. . .

port select --list pip

然后切换到你使用的相同python版本。

sudo port select --set pip pip27

...