1732

Here is the problem:

I have a requirements.txt file that looks like:

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 have a local archive directory containing all the packages + others.

I have created a new virtualenv with

bin/virtualenv testing

Upon activating it, I tried to install the packages according to requirements.txt from the local archive directory.

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

I got some output that seems to indicate that the installation is fine:

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
....

But a later check revealed that none of the packages are installed properly. I cannot import the packages, and none are found in the site-packages directory of my virtualenv. So what went wrong?

mareoraft
  • 3,474
  • 4
  • 26
  • 62
kakarukeys
  • 21,481
  • 10
  • 35
  • 48

18 Answers18

2183

This works for everyone:

pip install -r /path/to/requirements.txt

Explanation:

-r, --requirement < filename >

Install from the given requirements file. This option can be used multiple times.

George Wright
  • 61
  • 1
  • 10
Mike Lyons
  • 22,575
  • 2
  • 16
  • 19
  • 430
    I realize this answer doesn't address the original question, but it answers the question I had when I found this on Google... – Jonathan Dec 15 '13 at 05:50
  • 108
    for those who like to know what they type, the -r in the command just means "install from a requirements file", and is a shortcut for --requirement – Florent Chatterji Oct 06 '16 at 14:04
  • 5
    When I want to upgrade a package (e.g. Django), I change the version in my requirements.txt file and then run `pip install -r /path/to/requirements.txt`. This detects the change, upgrades the package, and leaves everything else alone. – User May 01 '17 at 08:55
  • pip freeze > requirements.txt – nda Mar 26 '20 at 16:04
  • 3
    while this usually works, a couple subtleties makes it fail. If any install failed for the packages in `requirements.txt`, none of them will be installed. If a required module imports a dependency during its installation (instead of just listing it as a requirement), it will fail even if the dependency precedes it in the list of dependencies, causing all modules to fail. It is probably a bug to import a dependent module during installation, but it is also possibly unexpected that the dependencies listed in `requirements.txt` aren't installed sequentially, but all at once. – iggie May 11 '20 at 19:30
  • In an app that has a `Django` backend and `Angular` frontend, should the `requirements.txt` file be in the root directory (and run there with `py -m pip install -r requirements.txt`) or should it be in the backend folder where files such as `manage.py` are located? – Reem Al-Assaf Dec 11 '21 at 11:35
  • 1
    @ReemAl-Assaf it doesn't matter that much. Some people prefer to keep a `package.json` and `requirements.txt` in the top level directories for the entire project, or in the sub directories where language specific things take over. It's your choice at this point. – Mike Lyons Mar 14 '22 at 23:12
  • Does anyone know the behavior of this command when the requirements.txt file is empty? Would it throw an error or just simply not do anything. I am unable to verify from my system. – flying_fluid_four Jul 11 '22 at 17:47
1100

This works for me:

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

--no-index - Ignore package index (only looking at --find-links URLs instead).

-f, --find-links <URL> - If a URL or path to an HTML file, then parse for links to archives.

If a local path or file:// URL that's a directory, then look for archives in the directory listing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codeape
  • 97,830
  • 24
  • 159
  • 188
  • 9
    **Information on `--no-index` from command pip help install** `--no-index` Ignore package index (only looking at `--find-links` URLs instead). **Information on `--find-links` from command pip help install** `-f`, `--find-links ` If a url or path to an html file, then parse for links to archives. If a local path or file:// url that's a directory, then look for archives in the directory listing. – AWrightIV Apr 03 '15 at 18:54
  • 2
    // , This could be a very elegant solution, especially given the eternal struggle with vendorizing: http://bitprophet.org/blog/2012/06/07/on-vendorizing/ – Nathan Basanese Apr 04 '16 at 06:40
  • One caution with this is you may `pip install ` without using `requirements.txt` but that will not update `requirements.txt`. An alternative might be updating a docker such that it lists all the pip install commands that are run to install dependencies. – claude computing Feb 22 '18 at 23:05
  • 1
    i tried this `python -m pip install -r requirements.txt` when inside the activated venv enviroment. things installed smoothly but when i do pip list it does not show that packages, when I am in the active venv or even after deactivate venv. also not able to use that packages. dont know what's wrong here – Shreyan Mehta Feb 12 '19 at 09:53
  • Are you sure ``python`` refers to the activated environment's python executable (``$ which python``)? – codeape Feb 12 '19 at 13:27
  • 2
    Just a heads up: This will work as long as there's not `git+ssh` requirement in the `requirements.txt`. For the `git+ssh` pip will still try to fetch the package – John Paraskevopoulos Jul 20 '20 at 15:18
  • same problem with git+https://... is there any workaround for that? – user3504575 Jan 03 '23 at 19:44
186

For virtualenv to install all files in the requirements.txt file.

  1. cd to the directory where requirements.txt is located
  2. activate your virtualenv
  3. run: pip install -r requirements.txt in your shell
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
78

I had a similar problem. I tried this:

    pip install -U -r requirements.txt

(-U = update if it had already installed)

But the problem continued. I realized that some of generic libraries for development were missed.

    sudo apt-get install libtiff5-dev libjpeg8-dev zlib1g-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk

I don't know if this would help you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
enriquetaso
  • 917
  • 6
  • 8
56

Use:

pip install -r requirements.txt

For further details, please check the help option:

pip install --help

We can find the option '-r' -

-r, --requirement Install from the given requirements file. This option can be used multiple times.

Further information on some commonly used pip install options (this is the help option on the pip install command):

Enter image description here

Also the above is the complete set of options. Please use pip install --help for the complete list of options.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Py_minion
  • 2,027
  • 1
  • 16
  • 20
34

Short answer

pip install -r /path/to/requirements.txt

or in another form:

python -m pip install -r /path/to/requirements.txt

Explanation

Here, -r is short form of --requirement and it asks the pip to install from the given requirements file.

pip will start installation only after checking the availability of all listed items in the requirements file and it won't start installation even if one requirement is unavailable.

One workaround to install the available packages is installing listed packages one by one. Use the following command for that. A red color warning will be shown to notify you about the unavailable packages.

cat requirements.txt | xargs -n 1 pip install

To ignore comments (lines starting with a #) and blank lines, use:

cat requirements.txt | cut -f1 -d"#" | sed '/^\s*$/d' | xargs -n 1 pip install
Safwan
  • 3,300
  • 1
  • 28
  • 33
30

First of all, create a virtual environment.

In Python 3.6

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

In Python 2.7

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

Then activate the environment and install all the packages available in the requirement.txt file.

source <path/to/new/virtualenv>/bin/activate
pip install -r <path/to/requirement.txt>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Girish Vas
  • 680
  • 1
  • 10
  • 22
  • 2
    Thanks. For windows user, to activate environment use this command : `/Scripts/activate.bat`. To deactivate environment, use this : `/Scripts/deactivate.bat`. – ibnɘꟻ Oct 16 '20 at 04:00
25

Often, you will want a fast install from local archives, without probing PyPI.

First, download the archives that fulfill your requirements:

$ pip install --download <DIR> -r requirements.txt

Then, install using –find-links and –no-index:

$ pip install --no-index --find-links=[file://]<DIR> -r requirements.txt
mkobit
  • 43,979
  • 12
  • 156
  • 150
Jadav Bheda
  • 5,031
  • 1
  • 30
  • 28
24

Try this:

python -m pip install -r requirements.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
18

I work with a lot of systems that have been mucked by developers "following directions they found on the Internet". It is extremely common that your pip and your python are not looking at the same paths/site-packages. For this reason, when I encounter oddness I start by doing this:

$ python -c 'import sys; print(sys.path)'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']

$ pip --version
pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)

That is a happy system.

Below is an unhappy system. (Or at least it's a blissfully ignorant system that causes others to be unhappy.)

$ pip --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

$ python -c 'import sys; print(sys.path)'
['', '/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages']

$ which pip pip2 pip3
/usr/local/bin/pip
/usr/local/bin/pip3

It is unhappy because pip is (python3.6 and) using /usr/local/lib/python3.6/site-packages while python is (python2.7 and) using /usr/local/lib/python2.7/site-packages

When I want to make sure I'm installing requirements to the right python, I do this:

$ which -a python python2 python3
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python2
/usr/local/bin/python3

$ /usr/bin/python -m pip install -r requirements.txt

You've heard, "If it ain't broke, don't try to fix it." The DevOps version of that is, "If you didn't break it and you can work around it, don't try to fix it."

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
15

Installing requirements.txt file inside virtual env with Python 3:

I had the same issue. I was trying to install the requirements.txt file inside a virtual environment. I found the solution.

Initially, I created my virtualenv in this way:

virtualenv -p python3 myenv

Activate the environment using:

source myenv/bin/activate

Now I installed the requirements.txt file using:

pip3 install -r requirements.txt

Installation was successful and I was able to import the modules.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
janu777
  • 1,940
  • 11
  • 26
  • I faced this issue while installing the requirements file inside a virtual environment. I have now modified my response to address the issue – janu777 Jul 24 '18 at 06:28
  • 1
    Using Anaconda Python 3.6 on Windows, I had to do `virtualenv -p python myenv`, `myenv\Scripts\activate.bat`, `pip install -r requirements.txt` – bdforbes Aug 25 '18 at 06:13
  • 1
    Actually, on the contrary, this seems like the only answer that _does_ answer the part of the question about installing locally defined deps in `requirements.txt` to a virtualenv. Unless I am missing something? Anyway, thanks! – davnicwil Sep 19 '18 at 17:39
10
  • Create virtual environment python3 -m venv virtual-env (For windows use python instead of python3)
  • Activate your virtual environment source virtual-env/bin/activate
  • Now install requirements pip install -r requirements.txt
MD SHAYON
  • 7,001
  • 45
  • 38
9
pip install --user -r requirements.txt 

OR

pip3 install --user -r requirements.txt 
4b0
  • 21,981
  • 30
  • 95
  • 142
user1460675
  • 407
  • 4
  • 9
8

Use pip3 install -r requirements.txt But make sure the requirements.txt file has been pulled from origin and not added to .gitignore

Samuel Kamau
  • 149
  • 2
  • 4
7

In Windows, this can lead to less format-related path issues, if you have

c:\folder\subfolder\requirements.txt

cd c:\folder\subfolder 
pip install -r requirements.txt
Lorenzo Bassetti
  • 795
  • 10
  • 15
7

On windows Using this

pip install -r /path/to/requirements.txt 

On linux pc we have python2 and also python3 so most time pip is regarded as

Python2

pip install -r /path/to/requirements.txt 

Python 3

pip3 install -r /path/to/requirements.txt
Codertjay
  • 588
  • 8
  • 13
1

Almost all the previous answers are correct, but I will suggest something new. The fact is that packages can be installed not only at the stage before launching the program, but also directly in runtime. I wrote a small library called instld for this.

Try installing it:

pip install instld

Now insert a similar code directly into your program:

import installed

with installed(r='requirements.txt'):
    import some_module

What are the advantages and disadvantages of this approach? Let's start with the disadvantages, they are the most obvious:

  • This creates delays when executing the program every time. It needs time to create a temporary directory, download the library there and install it, and then "clean up the garbage behind it".
  • This creates more load on the disk (since the package is downloaded to the disk in the temporary directory every time the program is started, and then deleted) and on the network. This may not play a special role in the private use of this method, but this kind of overhead will definitely be noticeable if a large organization starts using this method.

It would seem that if this method has so many disadvantages, why might it be necessary to use it? There may be several reasons for this, here are some of them:

  • This speeds up the work. Imagine that you need to quickly try 20 different libraries that are similar to each other. You do this in REPL. To install each of them, you need to exit the interpreter session, install the desired library via pip, then open a new session and try the library in the code. After all these actions, you should exit the interpreter again and delete the installed libraries. Sounds slow, right? And if you use instld, you don't need to exit the interpreter and your work becomes very fast. After exiting the interpreter, there will be no garbage left in the system.
  • You want to share your short script with a friend who is not familiar with Python. In this case, you just need to send him just one file. You describe the entire installation of dependencies in this file.
  • You can simultaneously use 2 different versions of the same library in the same program. It is rarely useful, but when it is needed, there are simply no other ways.
  • You can use conflicting libraries inside the same program. To do this, it is enough to install each of them in a separate context and import it directly from there.

As you can see, this approach has both pros and cons, so choose wisely.

Evgeniy Blinov
  • 351
  • 3
  • 3
-3

I have solved with running the below command:

py -m pip install ./requirements.txt

the above command will install all dependencies and libraries for the Django project.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103