Search code examples
pythonpipvirtualenv

Where does pip install packages with a virtual environment?


Situation: I have created a virtual environment and use an explicit path to run pip (without sourcing activate). Does it install packages in the global dist-packages or does it install them in the virtual environment's site-packages.

Details: As Where does pip install its packages? explains, pip installs packages in <virtualenv_name>/lib/<python_ver>/site-packages when used with a virtual environment. From my experience, this is true when I activate the virtualenv. I have an existing bash script that paths directly to the pip executable without activating the virtualenv. Does this still install packages in the virtualenv's site-packages? Or does it install them in /local/lib/<python-version>/dist-packages?

Note: I'm on Ubuntu 16.04


Solution

  • This very much depends on which version (not in the semantic version sense, but of the having-multiple-"versions" of pip installed when you create a venv) of pip your script is using, as well as its configuration (including possibly your environment).

    Assuming your script has a line like

    /some/path/to/pip install <some package>
    

    and assuming that that pip has installed at least one package, you can use

    /some/path/to/pip show <that package>
    

    and it'll give you output that looks like:

    $ pip show numpy
    Name: numpy
    Version: 1.14.5
    Summary: NumPy: array processing for numbers, strings, records, and objects.
    Home-page: http://www.numpy.org
    Author: Travis E. Oliphant et al.
    Author-email: None
    License: BSD
    Location: /usr/lib/python3/dist-packages
    Requires:
    

    The location line second to last should help answering your question.