Explain Codes LogoExplain Codes Logo

Installing Python packages from local file system folder to virtualenv with pip

python
pip-installation
virtualenv
package-management
Anton ShumikhinbyAnton Shumikhin·Dec 12, 2024
TLDR

To activate your virtualenv and install packages locally, use the pip install command. Make sure you're pointing it to the correct directory or .whl file.

# Activate virtualenv (it's like waking up the beast) $ source /path/to/virtualenv/bin/activate (virtualenv) $ pip install /path/to/local_package

In case of wheel (.whl) files:

(virtualenv) $ pip install /path/to/package_name.whl

If you have a requirements.txt file with a list of packages:

(virtualenv) $ pip install -r /path/to/requirements.txt

Ensure you're in the right environment and your paths are correct for a successful landing, like in Mars rover landing!

Package installation strategies

Developing with editable mode

Here's how to install packages in editable mode, which is like keeping the instruction manual with you while fixing things. It allows instant reflection of changes.

# It's like making a reversible bet, you can always backout! (virtualenv) $ pip install -e /path/to/local_project

When you have multiple packages in the same directory, use this neat trick to install them just like dominos in a row.

# You're the boss of the game, you make the rules! (virtualenv) $ pip install --find-links=/path/to/packages_dir package_name

Going completely local

To turn off pip's internet and force it to look only in local directories:

# Start an unplugged session, no internet! (virtualenv) $ pip install --no-index --find-links=/path/to/packages_dir package_name

Correcting path with file://

Format your local paths with file://:

# Give pip the "correct" glasses to read the situation better (virtualenv) $ pip install file:///absolute/path/to/package/package_name.whl

Upgrading responsibly

Where there's a newer version waiting to replace the old one:

# Shiny new toy? Just remember to keep the old one safe, though! (virtualenv) $ pip install --upgrade /path/to/new_package_version

Inspecting with verbose logging

When things go south, you can turn on the lights on pip:

# Remember, verbosity doesn't always mean wisdom, except here! (virtualenv) $ pip install -vv --log /path/to/logfile.log package_name

Simplifying life with scripts

When the neighborhood is friendly, local scripts can help:

# The one script to "install" them all! import subprocess import sys subprocess.check_call([sys.executable, '-m', 'pip', 'install', '.'])

Handling pre-releases

Sometimes, it's all about joining the party early:

# Who doesn't like being the first to know the latest gossip? (virtualenv) $ pip install --pre package_name

Practical examples and deep dives

Package installation from local version control

When your package is in version control:

# Now you can see your project's "past"! (virtualenv) $ pip install -e git+file:///path/to/repo.git#egg=package_name

Safe removal of packages

To safely uproot unwanted packages:

# Allowed to undo but with a 'yes' confirmation (virtualenv) $ pip uninstall -y package_name

Offline installation from a zipped source

Fully offline working environment, anyone? Just pass the zipped package file to pip:

# Who needs the internet when you've got zipped files? (virtualenv) $ pip install /path/to/zipped_package.zip

Local package index as a PyPI replica

Generate a simple HTML page-with links to package files and create your private PyPI server:

# Goal: To become the PyPI of your own project! (virtualenv) $ pip install --index-url=file:///path/to/local_index.html package_name