Explain Codes LogoExplain Codes Logo

Where does pip install its packages?

python
pip
package-management
virtual-environments
Anton ShumikhinbyAnton Shumikhin·Nov 6, 2024
TLDR

Whenever you pip install something, it finds its humble home in the site-packages directory:

# Life's persistent and most urgent question: "Where am I?" python -m site --user-site

Within an active virtual environment, the location changes:

# Aha! I'm in a different world now! python -m site

These spells reveal the lair of our pip-installed Python packages.

Pinpointing a package's residence

To locate a specific package's home within your system, orchestrate:

# Knock, knock. Where's <package name> ? pip show <package name>

Here, you’ll be presented with the name, version, and the location of your queried package.

Handling virtual environments

In a virtual environment, the packages typically unpack their bags at:

# "Please, direct me to my suite." <virtualenv_name>/lib/<python_version>/site-packages

Remember: Packages installed here won't bother packages outside this secluded space.

Tracking all package locations

For an eagle's view of all package habitats, execute:

# "Everybody, report your location!" pip list -v

Note: You're dealing with pip 10.0.0 or above here! Please refer to pip's official instructions if you need to upgrade pip.

Checking pip3's directory

To peer into pip3's world (Python 3.x) and its installation directory:

# "pip3, reveal your secrets..." pip3 -V

Digging up site module's secrets

Python's site module offers secrets about both global and user-specific package locations:

# "Alas, this treasure trick was taught by Python itself" python -c "import site; print(site.getsitepackages())"

To ensure you're walking the exact path, consider coupling this with readlink.

Zooming in on a Django package

If you have Django installed via pip and need to know its secret hideout:

# Django Unchained... from its location pip show django

Replace "django" with the package of your choice.

Unwrapping installation precedence

Just like you have a preference for a particular room in your house, pip has a preference: 🎒→🏠→🛌.

Combatting conflicts and issues

Use pipdeptree to deal with version conflicts and installation issues:

# We'll need more firepower for this pip install pipdeptree pipdeptree # Now, we're ready!

Managing package locations

Knowing package locations can come in handy during:

  • Debugging: Knowing a package's location can be a bug exterminator.
  • Web Development: Django or Flask, you name it!
  • Environment Isolation: Different projects, different requirements.
  • Dependency Management: Different stages require different versions.