How to Install Packages in a Virtual Environment - Python

From WikiHTP

Once your virtual environment has been activated, any package that you install will now be installed in the virtualenv & not globally. Hence, new packages can be without needing root privileges.

To verify that the packages are being installed into the virtualenv run the following command to check the path of the executable that is being used :

(<Virtualenv Name) $ which python
/<Virtualenv Directory>/bin/python

(Virtualenv Name) $ which pip
/<Virtualenv Directory>/bin/pip

Any package then installed using pip will be installed in the virtualenv itself in the following directory :

/<Virtualenv Directory>/lib/python2.7/site-packages/

Alternatively, you may create a file listing the needed packages.

requirements.txt:

requests==2.10.0

Executing[edit]

# Install packages from requirements.txt
pip install -r requirements.txt

will install version 2.10.0 of the package requests.

You can also get a list of the packages and their versions currently installed in the active virtual environment:

# Get a list of installed packages
pip freeze

# Output list of packages and versions into a requirement.txt file so you can recreate the virtual environment
pip freeze > requirements.txt

Alternatively, you do not have to activate your virtual environment each time you have to install a package. You can directly use the pip executable in the virtual environment directory to install packages.

$ /<Virtualenv Directory>/bin/pip install requests

Since you're installing without root in a virtual environment, this is not a global install, across the entire system - the installed package will only be available in the current virtual environment.

About This Tutorial

This page was last edited on 28 January 2019, at 22:05.