Reputation: 993
We are having a system with two different Python versions (2.7 and 3.5) presents.
We need to install some dependencies, on Python3.5 virtualenv
but Python2.7 virtualenv
is already activated for the user which is logged in and we cannot deactivate that virtualenv
.
We want to know, if it is possible to activate two different virtualenv
of different Python version together for the same user.
We tried by creating a virtualenv for Python2.7 with the following command:
virtualenv -p /usr/bin/python2.7 env2.7
source env2.7/bin/activate
After activating we created a virtualenv for Python3 with the following command:
virtualenv -p /usr/bin/python3 env3
source env3/bin/activate
The above command activated the env3
, we would like to know, is it going to effect our application which was running in Python2.7 virtualenv.
Upvotes: 0
Views: 2292
Reputation: 1977
Even as a single user you can still run multiple copies of your shell, each with its own set of environment variables for each program.
Thus, it is easy to run the two virtual environments at the same time. Just wrap your commands in (
and )
.
Try this:
virtualenv -p /usr/bin/python3.5 p35-venv
virtualenv -p /usr/bin/python2.7 p27-venv
echo '
(source p27-venv/bin/activate; python --version; sleep 5) &
(source p35-venv/bin/activate; python --version; sleep 5) &
' > test.sh
chmod a+x test.sh
./test.sh
This will start new bash process (the brackets around the command will run the command in a new bash), then activate the first venv in it, output the active Python version and sleep for 5 seconds. This will be all run in the background and the test.sh
script will continue to the second command before the first bracket finishes. The second bracket will start a second bash process, activate the second venv in it, output the Python version and also sleep for 5 seconds. Both new bash processes are run in the background in parallel, so you will see output from both of them before they finish the sleep command.
Upvotes: 0
Reputation: 37003
Since Python 2 and Python 3 virtual environments use the same shell variables to describe the environment, what you wish to do seems difficult. If, however, you simply want to to be able to run programs from one virtual environment while another one is active, this can be achieved as follows. This supposes you primarily want a Python 2 virtual environment, with some Python 3 programs mixed in.
Create and populate the Python 2 virtualenv.
Create the Python 3 virtualenv, defining console entry points for the Python programs you wish to use in the Python 2 virtual environment. Install the entry points by building the virtual environment.
Copy the entry-point "binaries" from the /bin subdirectory of the Python 3 environment into the /bin subdirectory of the Python 2 virtual environment.
If you look at the entry point files you will see that they have been given a shebang line that points them to the Python interpreter for the Python 3 virtualenv, and this is enough to ensure that it runs in the correct environment context.
Upvotes: 2