'Writing Shell Commands to VirtualEnv?

Is there a way I can write commands to a virtual environment after it's been activated? For example lets say I have a Python or Bash script which does some, stuff i.e.

  1. Make a virtualenv
  2. Activates it.
  3. Executes the commands to the shell of the newly created virtual environment?

For example I am doing something like this:

activate_this = subprocess.call("/bin/bash --rcfile " + "/home/" + os.getlogin() + "/mission-control/venv/bin/activate", shell=True)
        process = execfile(activate_this, dict(__file__=activate_this))
        process.communicate(subprocess.call(virtualenv.create_bootstrap_script(textwrap.dedent
            ("""
                import subprocess
                subprocess.call("pip install -r " + os.environ['VIRTUAL_ENV'] + "/requirements.txt", shell=True)
            """
            ))))

I would like to install the requirements.txt file after I activate the environment however I can't get the subprocess module to communicate with the shell after the virtual environment is created. I think it might have to do with me creating a new virtual environment via execfile, which therefore is creating a new process.

Also I know shell=True is bad practice but as of right now I am not concerned with the possibility of unsanitized input.



Solution 1:[1]

. "$VIRTUAL_ENV/bin/activate"
pip install -r "$VIRTUAL_ENV/requirements.txt"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryne Everett