title: How to run bash commands from Python preferably with the os library?
body: <p>I am trying to run the following python script named <code>test.py</code>. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:</p>
  <pre><code>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')
  </code></pre>
  <p>I am running the Python script using the following in the terminal:</p>
  <pre><code>python3 test.py
  </code></pre>
  <p>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:</p>
  <pre><code>sh: 1: source: not found
  </code></pre>
  <p>The last command <code>source my_virtualenvironment/bin/activate</code> normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does <code>sh: 1:</code> mean and why does it not work with my code? I would expect to get something starting with <code>bash: </code>.</p>
  <p>Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the <code>os</code> library (if there is a simpler solution without os, I am also open for that):
  <a href="https://stackoverflow.com/a/62355400/11535508">https://stackoverflow.com/a/62355400/11535508</a></p>