Python pip 在哪里安装它的包?

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

Where does pip install its packages?

pythondjangopipvirtualenv

提问by user2719875

I activated a virtualenv which has pip installed. I did

我激活了一个安装了 pip 的 virtualenv。我做了

pip3 install Django==1.8

and Django successfully downloaded. Now, I want to open up the Django folder. Where is the folder located? Normally it would be in "downloads" but I'm not sure where it would be if I installed it using pip in a virtualenv.

和 Django 成功下载。现在,我想打开 Django 文件夹。文件夹在哪里?通常它会在“下载”中,但如果我在 virtualenv 中使用 pip 安装它,我不确定它会在哪里。

采纳答案by khampson

pipwhen used with virtualenvwill generally install packages in the path <virtualenv_name>/lib/<python_ver>/site-packages.

pipvirtualenv 一起使用时,通常会在路径中安装包<virtualenv_name>/lib/<python_ver>/site-packages

For example, I created a test virtualenv named venv_testwith Python2.7, and the djangofolder is in venv_test/lib/python2.7/site-packages/django.

例如,我用Python2.7创建了一个名为venv_test的测试 virtualenv ,文件夹在.djangovenv_test/lib/python2.7/site-packages/django

回答by gdbj

By popular demand, an option provided via posted answer:

根据大众需求,通过发布的答案提供了一个选项:

pip show <package name>will provide the location for Windows and macOS, and I'm guessing any system. :)

pip show <package name>将为 Windows 和 macOS 提供位置,我猜是任何系统。:)

For example:

例如:

> pip show cvxopt
Name: cvxopt
Version: 1.2.0
...
Location: /usr/local/lib/python2.7/site-packages

回答by CognizantApe

By default, on Linux, Pip installs packages to /usr/local/lib/python2.7/dist-packages.

默认情况下,在 Linux 上,Pip 将软件包安装到 /usr/local/lib/python2.7/dist-packages。

Using virtualenv or --user during install will change this default location. If you use pip showmake sure you are using the right user or else pipmay not see the packages you are referencing.

在安装期间使用 virtualenv 或 --user 将更改此默认位置。如果您使用,请pip show确保您使用的是正确的用户,否则pip可能看不到您正在引用的包。

回答by flow2k

In a Python interpreter or script, you can do

在 Python 解释器或脚本中,您可以执行以下操作

import site
site.getsitepackages() # list of global package locations

and

site.getusersitepackages() #string for user-specific package location

for locations 3rd party packages (those not in the core Python distribution) are installed to.

对于安装第 3 方包(不在核心 Python 发行版中的包)的位置。

On my Brew-installed Python on MacOS, the former outputs

在 MacOS 上我的 Brew 安装的 Python 上,前一个输出

['/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'],

['/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'],

which canonicalizes to the same path output by pip show, as mentioned in a previous answer:

pip show正如前面的回答中提到的,它规范化为相同的路径输出:

$ readlink -f /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
/usr/local/lib/python3.7/site-packages

Reference: https://docs.python.org/3/library/site.html#site.getsitepackages

参考:https: //docs.python.org/3/library/site.html#site.getsitepackages

回答by jiasli

pip list -vcan be used to list packages' install locations, introduced in https://pip.pypa.io/en/stable/news/#b1-2018-03-31

pip list -v可用于列出包的安装位置,在https://pip.pypa.io/en/stable/news/#b1-2018-03-31 中介绍

Show install locations when list command ran with “-v” option. (#979)

使用“-v”选项运行 list 命令时显示安装位置。(#979)

>pip list -v
Package                  Version   Location                                                             Installer
------------------------ --------- -------------------------------------------------------------------- ---------
alabaster                0.7.12    c:\users\me\appdata\local\programs\python\python38\lib\site-packages pip
apipkg                   1.5       c:\users\me\appdata\local\programs\python\python38\lib\site-packages pip
argcomplete              1.10.3    c:\users\me\appdata\local\programs\python\python38\lib\site-packages pip
astroid                  2.3.3     c:\users\me\appdata\local\programs\python\python38\lib\site-packages pip
...


Update: This feature is introduced in pip10.0.0b1. On Ubuntu 18.04, pipor pip3installed with sudo apt install python-pipor sudo apt install python3-pipis 9.0.1 which doesn't have this feature. Check https://github.com/pypa/pip/issues/5599for suitable ways of upgrading pipor pip3.

更新:此功能在pip10.0.0b1 中引入。在 Ubuntu 18.04 上,pippip3安装了9.0.1sudo apt install python-pipsudo apt install python3-pip9.0.1 没有此功能。检查https://github.com/pypa/pip/issues/5599以获取合适的升级方式pippip3.