0

I see in this article: making and automatic python installer that you can use:

subprocess.run('pip install module_name')

or

subprocess.run('pip install -r requirements.txt')

in this format to install modules either individually or form a file. But when i run this command I get this error:

FileNotFoundError: [Errno 2] No such file or directory:

is there a way to run this like that without having to do this:

subprocess.run(['pip', 'install', 'module_name'])
KZiovas
  • 3,491
  • 3
  • 26
  • 47
  • You are in the wrong ``path``. Check that you are really in the path where the file ``requirements.txt`` exists. – programandoconro Sep 27 '21 at 11:48
  • No I am in the same directory. Does it work for you? – KZiovas Sep 27 '21 at 11:49
  • Maybe try: `subprocess.run("python -m pip install -r requirements.txt")` or `os.system("python -m pip install -r requirements.txt")`. Atleast that's what works for me. – RiveN Sep 27 '21 at 11:50
  • I want to use the subprocess library, since it is the recommended higher level solution. So you mean with double quotes instead of single? – KZiovas Sep 27 '21 at 11:52
  • @KZiovas try with ``os.system`` – programandoconro Sep 27 '21 at 11:52
  • Hmm. I usually use `os.system` and it works really well and I just tried the subprocess one and it also works for me... – RiveN Sep 27 '21 at 11:54
  • I want to avoid os.system the subprocess libary is the recomended one – KZiovas Sep 27 '21 at 11:54
  • @RiveN it does? What python version and what subprocess version do you use? – KZiovas Sep 27 '21 at 11:55
  • run ``subprocess.run("ls")`` or ``subprocess.run("dir")`` to double check that the file is there. – programandoconro Sep 27 '21 at 11:58
  • I use Python 3.9.6 and it was my first time trying subprocess, so I just imported it, but I tried it on Windows. Let me check how it behaves on Linux. – RiveN Sep 27 '21 at 12:00
  • @programandoconro you know what vs code run the script in the open directory BUT that is not hte problem because even if i try this subprocess.run('pip install pandas') for example I still get the same error – KZiovas Sep 27 '21 at 12:13
  • 1
    https://stackoverflow.com/questions/12332975/installing-python-module-within-code, esp. this answer: https://stackoverflow.com/a/50255019/7976758 – phd Sep 27 '21 at 12:15

2 Answers2

2

I suggest you stick to subprocess.run(['pip', 'install', '-r', 'requirements.txt']). To quote the subprocess docs.

Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).

Avoiding the use of shell=True will save you a lot of trouble if you ever want to run the script on a different system. In general, though, I recommend avoiding reliance on a shell. That is, after all, why you are using Python and not, say, Bash.

ogdenkev
  • 2,264
  • 1
  • 10
  • 19
  • Then don't you think that `os.system` might be a good idea too? It works on Windows and Linux and I think there's not that much difference between `subprocess` and `os.system` at least when it comes to installing pip modules. – RiveN Sep 27 '21 at 12:20
  • I have always used `subprocess` perhaps because of the comment in the [`os.system` docs](https://docs.python.org/3/library/os.html#os.system): "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." `os.system` seems similar to using `shell=True` in `subprocess.run`, which makes me uneasy due to passing to the shell (e.g. bash or cmd.exe) – ogdenkev Sep 27 '21 at 12:26
  • Thanks for telling me about that! I'll for sure read more about the `subprocess` library. – RiveN Sep 27 '21 at 13:58
1

To make it work on Linux I used the shell=True parameter. On Windows, it worked perfectly, without it.

subprocess.run("python -m pip install -r requirements.txt", shell=True)

Found the answer here.

RiveN
  • 2,595
  • 11
  • 13
  • 26