It took me forever to find this solution, so I want others to be able to see it.
I wanted to write a python script to create a virtual env and install modules inside it. Unfortunately, pip does not play nice with subprocess, as detailed here:
https://github.com/pypa/pip/issues/610
My answer is already on that thread, but I wanted to detail it below
Basically, the issue is that pip is still using the python executable that the original python called. To fix this, you need to delete it from the passed in environment variables. Here is the solution:
#!/usr/bin/python3
import os
import subprocess
python_env_var = {"_", "__PYVENV_LAUNCHER__"}
CMD_ENVIRONMENT = {name: value for (name, value) in os.environ.items()
if name not in python_env_var}
subprocess.call('./pip install -r requirements.txt', shell=True,
env=CMD_ENVIRONMENT)
Tested on Mac, ubuntu 14.04 and Windows with python 3
This same problem could easily exist for lots of situations -- I will be deleting this variable from now on, to prevent this kind of behavior when dealing with virtualenv's