4

I have the following requirements.txt file :

beautifulsoup4=4.8.2==py37_0
urllib3=1.25.8=py37_0
pyopenssl=19.1.0=py37_0
openssl=1.1.1d=h1de35cc_4
pandas=1.0.1=py37h6c726b0_0
tqdm=4.42.1=py_0

I need to install all these packages, or make sure they are installed from within a python script. How can I accomplish this ?

Suraj
  • 2,253
  • 3
  • 17
  • 48
  • Does this answer your question? [How to install packages using pip according to the requirements.txt file from a local directory?](https://stackoverflow.com/questions/7225900/how-to-install-packages-using-pip-according-to-the-requirements-txt-file-from-a) – JoshG Jun 24 '20 at 12:30
  • No it does not answer my question as I need to install packages from the file and can't execute the commands from the local directory. – Suraj Jun 24 '20 at 12:32
  • 1
    Does this answer your question? [Installing python module within code](https://stackoverflow.com/questions/12332975/installing-python-module-within-code) – sinoroc Jun 24 '20 at 13:06
  • The code installs a single package whereas I needed to install all packages from requirements.txt file. – Suraj Jun 24 '20 at 13:41
  • @Subramanian I know, you just need to adjust the _pip_ command to your needs. See my full answer. – sinoroc Jun 24 '20 at 14:55

3 Answers3

3

Use the following:

import subprocess
import sys

command = [
    sys.executable,
    '-m',
    'pip',
    'install',
    '--requirement',
    'requirements.txt',
]

subprocess.check_call(command)

It is very important to use sys.executable to get the path to the current running Python interpreter and use it with -m pip (executable module) to make 100% sure that pip installs for that particular interpreter. Indeed calling just pip (script) delivers absolutely no guarantee as to what Python interpreter will be called, pip could be associated with any Python interpreter on the system.

Additionally subprocess.check_call ensures that an error is raised if the process does not end successfully (i.e. the installation didn't succeed).

Advice such as the following is unreliable, if not dangerous:

  • os.system('pip install -r requirements.txt')

References:

sinoroc
  • 18,409
  • 2
  • 39
  • 70
2

One way can be like this:

import os
import sys
os.system(f'{sys.executable} -m pip install -r requirements.txt') #take care for path of file

More controls (and corner case handlings) over calling the command can be taken by subprocess as @sinoroc said, and in docs too.

One command that docs suggest is:

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'my_package'])

which is a wrapper over subprocess.call.

Vicrobot
  • 3,795
  • 1
  • 17
  • 31
  • This works, although I think `os` has to be installed beforehand. Thanks a lot. – Suraj Jun 24 '20 at 12:39
  • 1
    No it doesnt have to be installed it is a built-in module – RaoufM Jun 24 '20 at 12:40
  • This is wrong and will most likely not give the expected results in some cases. Unreliable. -- Use the following instead: `subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])` -- [Reference](https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program) – sinoroc Jun 24 '20 at 12:53
  • @sinoroc What are the cases? – Vicrobot Jun 24 '20 at 12:54
  • `pip` is not always guaranteed to be associated with the current running Python interpreter. – sinoroc Jun 24 '20 at 12:56
  • @sinoroc And why is that? Also, how is running the command from subprocess preventing it? – Vicrobot Jun 24 '20 at 12:59
  • Because of [this](https://snarky.ca/why-you-should-use-python-m-pip/). This is the `[sys.executable, '-m', 'pip']` part that prevents this from happening, it make sure that _pip_ is run with the exact same Python interpreter executable as the one currently running. `subprocess.check_call` is better than `os.system` because it checks that the executed program ended successfully, among other things. – sinoroc Jun 24 '20 at 13:04
  • @sinoroc Thanks for giving information. I added that part in the answer. You were right, as docs say here: https://docs.python.org/3/library/os.html#os.system – Vicrobot Jun 24 '20 at 13:18
  • 1
    Thanks for adding this, and I'd gladly change my vote if you remove the first part completely as it is simply bad advice. – sinoroc Jun 24 '20 at 13:38
  • 1
    @sinoroc I think reader of this answer will get to know both ways and will know the pros and cons thing, which seems to be good at its place. – Vicrobot Jun 24 '20 at 13:47
  • @Vicrobot At least fix the first suggestion to something like `os.system(f'{sys.executable} -m pip -r requirements.txt')`. – sinoroc Jul 02 '20 at 13:40
  • 1
    @sinoroc yes, that's an improvement. Edited. Your comment is missing 'install' part though. – Vicrobot Jul 02 '20 at 14:02
  • 1
    Thanks for making the change. Thanks for noticing the missing `install`. – sinoroc Jul 02 '20 at 14:05
0

You can run the command pip install -r requirements.txt when in the same directory as the txt file

Or in python using

import os
os.system ('pip install -r path/to/requirements.txt')
RaoufM
  • 525
  • 5
  • 22
  • I need to run it from the python script itself. I need to send it to my team lead and he just wants to execute the python script. – Suraj Jun 24 '20 at 12:31
  • This is wrong and will most likely not give the expected results in some cases. Unreliable. -- Use the following instead: `subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])` -- [Reference](https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program) – sinoroc Jun 24 '20 at 12:53