Python Pip 安装没有安装到正确的目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25607837/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:22:37  来源:igfitidea点击:

Pip Install not installing into correct directory?

pythonbashinstallationpip

提问by Chris

I can't seem to use sudo pip install correctly so that it installs into the following directory:

我似乎无法正确使用 sudo pip install 以将其安装到以下目录中:

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

so that I can then import the module using python

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

I've run

我跑了

sudo pip install scikit-learn --upgrade

Result

结果

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

However, it's not in the correct directory

但是,它不在正确的目录中

How do I get sudo pip install to install into correct directory?

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

In addition, I've tried

另外,我试过

sudo pip install Scrappy

须藤 pip 安装 Scrappy

I get the following message

我收到以下消息

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)

Both these instances demonstrate that it's been installed but not correctly. For example, when I run the following import in python:

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

import scrapy
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-51c73a18167b> in <module>()
----> 1 import scrapy

ImportError: No module named scrapy

I've tried the following:

我尝试了以下方法:

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

采纳答案by Roy2012

From the comments to the original question, it seems that you have multiple versions of Python installed, and that pip just goes to the wrong version.

从评论到原始问题,您似乎安装了多个版本的 Python,而 pip 只是转到了错误的版本。

First, to know which version of python you're using, just type which python. You should either see:

首先,要知道您使用的是哪个版本的 python,只需键入which python. 您应该看到:

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

if you're going to the right version of python, or:

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

which python
/usr/bin/python

If you're going to the 'wrong' version. To make pip go to the right version, you first have to change the path:

如果您要使用“错误”版本。要使 pip 转到正确的版本,您首先必须更改路径:

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

typing 'which python' would now get you to the right result. Next, install pip (if it's not already installed for this installation of python). Finally, use it. you should be fine now.

输入“which python”现在可以得到正确的结果。接下来,安装 pip(如果尚未为此安装 python 安装它)。最后,使用它。你现在应该没事了。

回答by Chris

1 - Something that might work

1 - 可能有用的东西

The pipexecutable is actually a Python script.

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

By default it contains (on Linux):

默认情况下,它包含(在 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')()
    )

So if you got the same in MacOS, pipwould always use /usr/bin/python.

因此,如果您在 MacOS 中得到相同的结果,pip将始终使用/usr/bin/python.

But this is a default. You can still provide the version of python you want either by editing the file or by using python explicitly.

但这是默认设置。您仍然可以通过编辑文件或显式使用 python 来提供所需的 python 版本。

If which pythonreturns /usr/bin/pythonthen something went wrong when you installed your own version. If it is /Library/Frameworks/Python.framework/Versions/2.7/bin/python, you can directly call:

如果which python返回,/usr/bin/python那么当您安装自己的版本时出现问题。如果是/Library/Frameworks/Python.framework/Versions/2.7/bin/python,则可以直接调用:

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

However, chances are high that it won't work. The reason is that sudois resetting all your environment variables. To make it work, the easiest would be to use:

但是,它行不通的可能性很高。原因是sudo正在重置所有环境变量。要使其工作,最简单的方法是使用:

sudo -E pip install scikit-learn --upgrade

or

或者

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

depending on your setup.

取决于您的设置。

2 - What you should do

2 - 你应该做什么

pipwas not thought of as something that rootshould execute. The actual best way to use it is to install a local, non-root, python version. You just have to make sure that you use it by default by setting up the correct environment variables (such as PATHon Linux) and then install pipwithout sudousing that python version.

pip不被认为是root应该执行的东西。实际使用它的最佳方法是安装一个本地的、非 root 的 Python 版本。您只需要通过设置正确的环境变量(例如PATH在 Linux 上)来确保默认使用它,然后在pipsudo使用该 python 版本的情况下进行安装。

An even better way would be to setup virtualenvs from your root install.

更好的方法是virtualenv从 root 安装设置s。

This way, you can install/update whatever you want without root privileges and never bother again about why sudo pipis not working. You would also avoid to provide root privileges to whatever is on Pypi and that would warrant that you don't mix system libs with your own.

这样,您可以在没有 root 权限的情况下安装/更新您想要的任何内容,并且再也不用担心为什么sudo pip不起作用了。您还将避免为 Pypi 上的任何内容提供 root 权限,这将保证您不会将系统库与您自己的库混合。

回答by Wyrmwood

Virtualenvis your friend

Virtualenv是你的朋友

Even if you want to add a package to your primary install, it's still best to do it in a virtual environment first, to ensure compatibility with your other packages. However, if you get familiar with virtualenv, you'll probably find there's really no reason to install anything in your base install.

即使您想在主安装中添加一个包,最好还是先在虚拟环境中进行,以确保与其他包的兼容性。但是,如果您熟悉 virtualenv,您可能会发现真的没有理由在您的基础安装中安装任何东西。

回答by Emily

I totally agree with the guys, it's better to use virtualenvso you can set a custom environment for every project. It ideal for maintenance because it's like a different world for every project and every update of an application you make won't interfere with other projects.

我完全同意这些人的看法,最好使用virtualenv以便您可以为每个项目设置自定义环境。它非常适合维护,因为对于每个项目来说,它就像一个不同的世界,并且您所做的应用程序的每次更新都不会干扰其他项目。

Here you can find a nutshell of virtualenvrelated to installation and first steps.

在这里,您可以找到与安装和第一步相关的 virtualenv 的概要

回答by Julian

Make sure you pip version matches your python version.

确保您的 pip 版本与您的 python 版本匹配。

to get your python version use:

让你的python版本使用:

python -V

蟒蛇 -V

then install the correct pip. You might already have intall in that case try to use:

然后安装正确的pip。在这种情况下,您可能已经安装了 intall 尝试使用:

pip-2.5 install ...

pip-2.5 安装...

pip-2.7 install ...

pip-2.7 安装...

or for those of you using macports make sure your version match using.

或者对于那些使用 macports 的人,请确保您的版本匹配使用。

port select --list pip

端口选择 --list pip

then change to the same python version you are using.

然后更改为您正在使用的相同 python 版本。

sudo port select --set pip pip27

须藤端口选择 --set pip pip27

Hope this helps. It work on my end.

希望这可以帮助。它对我有用。

回答by Jabda

This is what worked for me on Windows. The cause being multiple python installations

这就是在 Windows 上对我有用的方法。原因是多个 python 安装

  1. update path with correct python
  2. uninstall pip using python -m pip uninstall pip setuptools
  3. restart windows didn't work until a restart
  1. 使用正确的python更新路径
  2. 使用卸载 pip python -m pip uninstall pip setuptools
  3. 重新启动 Windows直到重新启动才起作用

回答by salomeow

  1. download pip at https://pypi.python.org/pypi/pip(tar)
  2. unzip tar file
  3. cd to the file's directory
  4. sudo python2.7 setup.py install
  1. https://pypi.python.org/pypi/pip(tar)下载 pip
  2. 解压tar文件
  3. cd 到文件目录
  4. 须藤 python2.7 setup.py 安装

回答by sivan.liao

You Should uninstall the existed python,then download new version.

您应该卸载现有的python,然后下载新版本。

回答by user9950741

You could just change the shebang line. I do this all the time on new systems.

你可以改变shebang线。我一直在新系统上这样做。

If you want pipto install to a current version of Python installed just update the shebang line to the correct version of pythons path.

如果要pip安装到已安装的当前版本的 Python,只需将 shebang 行更新为正确版本的 pythons 路径。

For example, to change pip (not pip3) to install to Python 3:

例如,要更改 pip(不是 pip3)以安装到 Python 3:

#!/usr/bin/python

To:

到:

#!/usr/bin/python3

Any module you install using pipshould install to Python not Python.

您使用pip安装的任何模块都应该安装到 Python 而不是 Python。

Or you could just change the path.

或者你可以改变路径。