3

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

asked Nov 4, 2014 at 23:35

1 Answer 1

3

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

answered Nov 4, 2014 at 23:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.