0

I am trying to run the following python script named test.py. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:

import os

os.system('echo install virtualenv')
os.system('sudo pip install virtualenv')

os.system('echo create virtual environment')
os.system('virtualenv my_virtualenvironment')

os.system('echo activate virtual environment')
os.system('source my_virtualenvironment/bin/activate')

I am running the Python script using the following in the terminal:

python3 test.py

The problem that I have is that the commands do not run the same way as they would on a Linux terminal. The output is the following error when trying to execute the last line of the Python script:

sh: 1: source: not found

The last command source my_virtualenvironment/bin/activate normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does sh: 1: mean and why does it not work with my code? I would expect to get something starting with bash: .

Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the os library (if there is a simpler solution without os, I am also open for that): https://stackoverflow.com/a/62355400/11535508

owmal
  • 173
  • 1
  • 13

1 Answers1

1

source is a bash built-in command, not an executable.

Use the full path to the python interpreter in your commands instead of venv activation, e.g. os.system('<venv>/bin/python ...').

The second option is to write your commands into a separate bash script and call it from python:

os.system('bash script.sh')
Alexander Volkovsky
  • 2,588
  • 7
  • 13
  • Thanks for your comment! I will take a deeper look into the solution with the separate bash script. I am not sure if I understood your first solution right. I tried `activate = os.path.join(os.getcwd(), 'my_virtualenvironment/bin/activate')` and then `os.system('source '+activate)` which did not work for me (same error). – owmal May 18 '21 at 13:01
  • I mean that you don't need an activation at all. `$ venv/bin/python main.py` is an alternative of `$ source venv/bin/activate; python main.py`. – Alexander Volkovsky May 18 '21 at 13:29
  • When I try to use your second solution, everything seems to run fine but it does not actually activate the virtual environment. I get no error. The content of the `scipt.sh` file is only the commands (without `import os` and `os.system('')` of course) – owmal May 18 '21 at 13:32
  • I meant `script.sh` – owmal May 18 '21 at 13:37
  • First solution: what if I don't want to run a `main.py` file in the virtual environment? I just want to activate the virtual environment in order to be able to do different things manually (for example `pip install` and `pip uninstall`) – owmal May 18 '21 at 13:40
  • The same thing: just use an absolute path to pip. `/bin/pip`. There is magic here. "activation" is all about modifying PATH. – Alexander Volkovsky May 18 '21 at 14:13