Moon Cheesez Moon Cheesez - 1 year ago 48
Python Question

Virtualenv gives different versions for different os

I am working on a django project on two separate systems, Debian Jessie and Mac El Capitan. The project is hosted on github where both systems will pull from or push to.

However, I noticed that on my Debian, when I run

python --version
, it gives me
Python 3.4.2
but on my Mac, it gives me
Python 2.7.10
despite being in the same virtual environment. Moreover, when I run
django-admin --version
on my Debian, it gives me
while on my Mac,

This happens even when I freshly clone the projects from github and run the commands.

Why is it that the virtual environment does not keep the same version of python and django?

Answer Source

Now you understand that virtual environments can't be transferred easily from machine to machine. It's common to use the

pip freeze`

command and store its output in a file called requirements.txt. Then anyone else can rebuild your envirnment on their machine by running

pip install -r requirements.txt

When you create a new virtual environment you can say which Python interpreter you want to use with the -p or --python switch, which should be followed by the path to the correct executable.

I'd personally recommend against modifying the system Python in any way, because system maintenance routines often rely on its integrity. If relatively simple to install new copies for Python 2 and 3 somewhere like /usr/local/bin (Mac users often use brew for this) and have virtual environments that rely on different Python interpreters.