How can I install packages using pip according to a requirements.txt file?

How can I install packages using pip according to a requirements.txt file?

Here’s the issue:

I have a requirements.txt file with the following contents:

BeautifulSoup==3.2.0
Django==1.3
Fabric==1.2.0
Jinja2==2.5.5
PyYAML==3.09
Pygments==1.4
SQLAlchemy==0.7.1
South==0.7.3
amqplib==0.6.1
anyjson==0.3
...

I also have a local archive directory that contains all these packages plus additional ones.

I created a new virtual environment using:

bin/virtualenv testing

After activating the virtual environment, I attempted to install the packages from requirements.txt using my local archive directory:

source bin/activate
pip install -r /path/to/requirements.txt -f file:///path/to/archive/

The output seemed to indicate that the installation was successful:

Downloading/unpacking Fabric==1.2.0 (from -r ../testing/requirements.txt (line 3))
  Running setup.py egg_info for package Fabric
    warning: no previously-included files matching '*' found under directory 'docs/_build'
    warning: no files found matching 'fabfile.py'
Downloading/unpacking South==0.7.3 (from -r ../testing/requirements.txt (line 8))
  Running setup.py egg_info for package South
....

However, upon checking later, I found that none of the packages were installed properly. I cannot import the packages, and they are not present in the site-packages directory of my virtual environment. What went wrong?

Hi Isha,

Please try the below method:

pip install -r /path/to/requirements.txt
  • -r, --requirement <filename>: Installs packages listed in the specified requirements file. This option can be used multiple times to install from multiple requirements files.

This method works for me:

$ pip install -r requirements.txt --no-index --find-links file:///tmp/packages

Explanation:

  • --no-index: Ignores the package index (only looks at URLs specified with --find-links).
  • -f, --find-links <URL>: If <URL> is a URL or path to an HTML file, it parses for links to archives. If <URL> is a local path or a file:// URL that points to a directory, it looks for archives in the directory listing.

First, create a virtual environment.

For Python 3.6:

virtualenv --python=/usr/bin/python3.6 <path/to/new/virtualenv/>

For Python 2.7:

virtualenv --python=/usr/bin/python2.7 <path/to/new/virtualenv/>

Next, activate the environment and install all the packages listed in the requirements.txt file:

source <path/to/new/virtualenv>/bin/activate
pip install -r <path/to/requirements.txt>