4

I am looking for an alternative to

pip install -r requirements.txt

which can be used to install packages from a python module. I have used

subprocess.check_call([sys.executable, "-m", "pip", "install", package])

for installing a package, however, this command seems to work only for a single package. Thanks in advance. Any help is appreciated.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Karen
  • 401
  • 7
  • 15
  • 8
    What is the problem with `pip install -r requirements.txt` for you? – Gabio Jul 12 '20 at 12:31
  • pip install -r requirements.txt works without any issues, I just could not find an alternative way of installing multiple packages, which I guess would make the code a bit more modular in my case. – Karen Jul 12 '20 at 12:38
  • 2
    Sorry, can you unpack "I just could not find an alternative way of installing multiple packages, which I guess would make the code a bit more modular in my case"? – ChrisGPT was on strike Jul 12 '20 at 12:50
  • Sure. I could not get the following code to work for me, which works fine with a single package name specified. import subprocess import sys subprocess.check_call([sys.executable, "-m", "pip", "install", "requirements.txt"]). I am looking for an alternative to install the requirements from a python script without using the pip command. – Karen Jul 12 '20 at 12:56
  • Right, but Gabip was asking what's wrong with `pip install -r requirements.txt`. What is your motivation to install "without using the pip command"? And why from a Python script? Why do you want an alternative in the first place? – ChrisGPT was on strike Jul 12 '20 at 12:59

1 Answers1

8

Don't forget to replace "package" with "-r requirements.txt":

subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
JBen
  • 108
  • 4
  • This approach somehow does not work for me even with the correctly specified directory. – Karen Jul 12 '20 at 12:46
  • @karen-gishyann What kind of error message do you get? – JBen Jul 12 '20 at 12:59
  • this is the error. Command '['/usr/bin/python3', '-m', 'pip', 'install', 'requirements.txt']' returned non-zero exit status 1. – Karen Jul 12 '20 at 13:05
  • @karen-gishyann Try capturing the output message to identify the underlying error: https://docs.python.org/3/library/subprocess.html#subprocess.check_output – JBen Jul 12 '20 at 13:10
  • @karen-gishyann Like such: https://pastebin.com/KHWpTusM – JBen Jul 12 '20 at 13:33
  • JBen it says no matching distribution found for requirements.txt, if I get it to work, will update. thanks. – Karen Jul 12 '20 at 13:38
  • 2
    @karen-gishyann You are still omitting the "-r" switch from after "install", without the switch it tries to install a package called "requirements.txt" instead of installing its contents. – JBen Jul 12 '20 at 14:13
  • Jben, I think you are right, thus the answer is the solution. I had not looked at it carefully, thanks!! – Karen Jul 12 '20 at 16:21