ChatGPT解决这个技术问题 Extra ChatGPT

Where does pip install its packages?

I activated a virtualenv which has pip installed. I did

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.

Can the directory in which to install packages be overridden via CLI args, environment variables or a config file?

P
Peter Mortensen

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

For example:

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

On mac this showed me a directory with the awscli source code, but no binary. :/
Correct, but to be fully compliant with the question asked, it should be pip3 show <package name>
i'm using python 3.6 on windows and "pip" refers to pip for python 3. i don't need to specify "pip3".
@CoryKlein Rather than site-packages/, I found it in ~/Library/Python/3.7/bin/
Worked very well for ubuntu 18.04
k
khampson

pip when used with virtualenv will generally install packages in the path <virtualenv_name>/lib/<python_ver>/site-packages.

For example, I created a test virtualenv named venv_test with Python 2.7, and the django folder is in venv_test/lib/python2.7/site-packages/django.


Sometimes you may not know the virtualenv name/path, such as using with Jupyterhub/binderhub spawned resources. I found way #1 here to work when pip show did nothing. Briefly, it consist of entering the appropriate python console and typing help("module_name"), where module_name is replaced with the actual module name in which you are interested. You can see the installed modules with help("modules") in the python console.
in which way you built your virtualenv? I am using conda to create my virtual environment while the pip3 didn't install package in the folder you mentioned
At the time (almost 4 years ago), I was using virtualenv and Python 2.x. Now, I am using venv and Python 3.5.x. I still find the same general folder structure, though. With Anaconda, that's a curated, distinct distribution, so it may structure things differently, at least in part.
Some of the other answer provide more generic ways to locate a package location.
P
Peter Mortensen

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

Show install locations when list command ran with “-v” option. (#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
...

This feature is introduced in pip 10.0.0b1. On Ubuntu 18.04 (Bionic Beaver), pip or pip3 installed with sudo apt install python-pip or sudo apt install python3-pip is 9.0.1 which doesn't have this feature.

Check https://github.com/pypa/pip/issues/5599 for suitable ways of upgrading pip or pip3.


Unfortunately, this doesn't work on Ubuntu 18.04LTS
For me, it doesn't show the install location (on Linux Mint 19.3)
The pip installed using sudo apt install python-pip or sudo apt install python3-pip is 9.0.1. Check github.com/pypa/pip/issues/5599 for suitable ways of upgrading pip or pip3.
Calling pip directly may not always give the correct answer. Calling pip with the specific python executable is better, e.g. python -m pip list -v See stackoverflow.com/questions/29980798/…
R
Ryan Pederson

Easiest way is probably

pip3 -V

This will show you where your pip is installed and therefore where your packages are located.


Calling pip directly may not always give the correct answer. Calling pip with the specific python executable is better, e.g. python -m pip -V See stackoverflow.com/questions/29980798/…
C
CognizantApe

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

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


Linux: Installing packages in python3 as root - not recommended - will go in /usr/local/lib/python3.9/site-packages/. As normal user, they will go in /home/normaluser/.local/lib... I wonder what is / root/.local/lib.. for.
/root/.local/lib occurs when you do sudo pip uninstall --user package. I.e. it happens when you do a user installation while sudo-ing. Which makes sense since /root is the user folder of the root user.
f
flow2k

In a Python interpreter or script, you can do

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

and

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

For locations third-party packages (those not in the core Python distribution) are installed to.

On my Homebrew-installed Python on macOS, the former outputs

['/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:

$ 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


It seems that all packages do not implement the functions getsitepackage() and getusersitepackages(). At least those functions do not exist for package statsmodels
@GaelLorieul There is no such thing. These two functions are part of the site module; it does not make sense to talk about packages "implementing" them. These are the default locations for any package installed using pip, including, in particular, statsmodels.
w
wisbucky

The safest way is to call pip through the specific python that you are executing. If you run pip show pip directly, it may be calling a different pip than the one that python is calling. Examples:

$ python -m pip show pip
$ python3 -m pip show pip
$ /usr/bin/python -m pip show pip
$ /usr/local/bin/python3 -m pip show pip

Here's an example showing how they can differ:

$ pip show pip

Location: /usr/local/lib/python3.9/site-packages

$ python -m pip show pip

Location: /Library/Python/2.7/site-packages

G
Gael Lorieul

One can import the package then consult its help

import statsmodels
help(sm)

At the very bottom of the help there is a section FILE that indicates where this package was installed.

This solution was tested with at least matplotlib (3.1.2) and statsmodels (0.11.1) (python 3.8.2).


OMG. I wish I had known about help() months ago! I even wrote my own code to dump out the __doc__. Now I'm help addicted. I'm doing help(everything) 20 times a day. Help!