Installing packages present in requirements.txt by referencing it from a python file

Viewed 1449

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 ?

3 Answers

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.

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:

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')
Related