Skip to content Skip to sidebar Skip to footer

Installing Packages Present In Requirements.txt By Referencing It From A Python File

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

Solution 1:

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.

Solution 2:

Use the following:

import subprocess
importsyscommand= [
    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:

Solution 3:

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

Or in python using

import osos.system ('pip install -r path/to/requirements.txt')

Post a Comment for "Installing Packages Present In Requirements.txt By Referencing It From A Python File"