Installing Packages Present In Requirements.txt By Referencing It From A Python File
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"