725

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • 6
    Can the directory in which to install packages be overridden via CLI args, environment variables or a config file? – John Carrell Jun 22 '18 at 14:42
  • 2
    @JohnCarrell yes you can with --target libs e.g. to install to libs/ – Anders Rune Jensen Oct 15 '22 at 09:29
  • 2
    LOL. Well, @AndersRuneJensen in the 4 years it took you to answer my question, my Python Fu has improved significantly. I wish I'd remembered I posted this adorable question and come to answer it myself but you beat me to it! Thank you, though, for not answering a very naive question rudely. +1 to you , sir! – John Carrell Oct 17 '22 at 22:30
  • Does this answer your question? [How do I find the location of my Python site-packages directory?](https://stackoverflow.com/questions/122327/how-do-i-find-the-location-of-my-python-site-packages-directory) – rofrol Apr 14 '23 at 12:07

10 Answers10

1110

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gdbj
  • 16,102
  • 5
  • 35
  • 47
  • 29
    On mac this showed me a directory with the awscli source code, but no binary. :/ – Cory Klein Dec 07 '17 at 20:34
  • Worked on my Mac. Directory had a .py file in it – Cinghiale Dec 21 '17 at 23:53
  • 12
    Correct, but to be fully compliant with the question asked, it should be `pip3 show ` – Jorge Torres Feb 16 '18 at 01:07
  • 5
    i'm using python 3.6 on windows and "pip" refers to pip for python 3. i don't need to specify "pip3". – FistOfFury Aug 07 '18 at 16:46
  • 18
    @CoryKlein Rather than `site-packages/`, I found it in `~/Library/Python/3.7/bin/` – Matt Fletcher Jan 08 '19 at 15:26
  • 1
    I ask a more boundary but useful question: if you haven't install any package then how can you know where will pip3 will put the downloaded package? – cloudscomputes Feb 20 '19 at 03:51
  • 5
    Worked very well for ubuntu 18.04 – Mohammed Baashar Apr 14 '19 at 11:52
  • what about with pip3? pip3 still seems put site-packages in lib/python2.7? – Alexander Mills May 22 '19 at 21:27
  • `pip show pip` works. On the off chance you don't have any packages installed. Default (on Windows) seems to be `%PYTHONHOME%\lib\site-packages`. (In case others are having weird issues installing due to obnoxious corporate firewalls and certificates and every tool on the planet using different cacert bundle locations and formats.) – Captain Man Dec 06 '19 at 19:01
  • Finally found the binary folder for pip (Anaconda via brew). In my case it was in `~/.local/bin/` I just had to threaten to uninstall it with `pip uninstall`, then it revealed the binary location. – Gandalf Saxe Sep 23 '20 at 14:00
  • `pip show ...` may not always give the correct answer. Calling `pip show` with the specific `python` executable is better, e.g. `python -m pip show...` See https://stackoverflow.com/questions/29980798/where-does-pip-install-its-packages/69035259#69035259 – wisbucky Sep 02 '21 at 18:18
  • 1
    On windows `pip show x` will give you a `.......\local-packages\Python311\site-packages/` which contains python code. For me I found the executables under `.......\Scripts` instead. – Alex Telon Mar 20 '23 at 10:08
196

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leoly
  • 8,468
  • 6
  • 32
  • 33
  • 6
    Unfortunately, this doesn't work on Ubuntu 18.04LTS – Sumax Dec 26 '19 at 10:19
  • 7
    For me, it doesn't show the install location (on Linux Mint 19.3) – Nicolai Weitkemper Mar 25 '20 at 19:51
  • 2
    The `pip` installed using `sudo apt install python-pip` or `sudo apt install python3-pip` is 9.0.1. Check https://github.com/pypa/pip/issues/5599 for suitable ways of upgrading `pip` or `pip3`. – leoly May 19 '20 at 13:03
  • 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 https://stackoverflow.com/questions/29980798/where-does-pip-install-its-packages/69035259#69035259 – wisbucky Sep 02 '21 at 18:19
175

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.

khampson
  • 14,700
  • 4
  • 41
  • 43
  • 8
    Sometimes you may not know the virtualenv name/path, such as using with Jupyterhub/binderhub spawned resources. I found way #1 [here](https://blog.revathskumar.com/2011/10/python-list-all-packages-installed.html) 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. – Wayne Jul 13 '18 at 17:59
  • 3
    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 – cloudscomputes Feb 20 '19 at 03:53
  • 2
    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. – khampson Feb 21 '19 at 01:52
  • Some of the other answer provide more generic ways to locate a package location. – kontur Dec 10 '20 at 12:28
  • but if you have two versions of python: 3.8 and 3.7, where does it install it? – Nathan B Feb 07 '23 at 20:06
62

Easiest way is probably

pip3 -V

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

Ryan
  • 729
  • 5
  • 3
  • 2
    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 https://stackoverflow.com/questions/29980798/where-does-pip-install-its-packages/69035259#69035259 – wisbucky Sep 02 '21 at 18:20
  • `pip3 -V` fails if package was installed without `sudo`. – Pierre ALBARÈDE Feb 14 '23 at 18:29
26

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

flow2k
  • 3,999
  • 40
  • 55
  • It seems that all packages do not implement the functions `getsitepackage()` and `getusersitepackages()`. At least those functions do not exist for package `statsmodels`… – Gael Lorieul Jul 22 '20 at 17:23
  • @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`. – flow2k Jan 24 '21 at 12:22
25

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
wisbucky
  • 33,218
  • 10
  • 150
  • 101
24

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.

CognizantApe
  • 1,149
  • 10
  • 10
  • 3
    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. – Timo Nov 25 '21 at 19:21
  • 1
    `/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. – CognizantApe Mar 01 '22 at 22:10
13

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).

Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
  • 3
    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!** – not2qubit Nov 24 '20 at 16:51
4
% python -c "import sysconfig; print(sysconfig.get_path('purelib'))"
/opt/homebrew/lib/python3.11/site-packages
% pip show yt-dlp
Name: yt-dlp
Version: 2023.3.4
Summary: A youtube-dl fork with additional features and patches
Home-page: https://github.com/yt-dlp/yt-dlp
Author:
Author-email:
License:
Location: /opt/homebrew/lib/python3.11/site-packages
Requires: brotli, certifi, mutagen, pycryptodomex, websockets
Required-by:

How do I find the location of my Python site-packages directory?

or shorter python -m pip show pip

Where does pip install its packages?

rofrol
  • 14,438
  • 7
  • 79
  • 77
0

If you have installed packages via pip and are running the code on Windows, the package should be located in one of the following directories:

User Site Packages: C:\Users\USERNAME\AppData\Roaming\Python\PythonXX\site-packages
Global Site Packages: C:\Program Files\PythonXX\Lib\site-packages
Note that "USERNAME" and "XX" will depend on your system configuration and the version of Python you are using. Also, if you have installed Python in a different location than the default, the paths may differ.

If you're not sure where the package is installed, you can open a command prompt and type pip show 'package-name'. This will show you the installation location of the package.