0

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.

2
  • 1
    If you need one for running a specific application, not development, you may be better off using pipsi to install the program in an isolated virtual environment. Commented Aug 22, 2017 at 5:48
  • pipsi is good tool, but we cannot use other tools, and pipsi uses python2.7, but I want to use `python3'. Although there is work around to fix that. Commented Aug 22, 2017 at 6:13

2 Answers 2

2

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.

  1. Create and populate the Python 2 virtualenv.

  2. 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.

  3. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

by console entry points in python program do you mean python-packaging.readthedocs.io/en/latest/…
Exactly that, yes. It's a simple way to build command line processors. The nice part is, it gives you an artefact you can copy into another virtual environment without losing the connection to the virtualenv it was built in.
0

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.

2 Comments

but the python2.7 virtualenv is already active in the system. while executing the command source p35-venv/bin/activate; python hello35.py is it going to exit the python2.7 virtualenv and activate the python3.5 virtualenv?
@Bidyut each virtualenv is only active within the brackets. moreover, the second command is started before the first one finishes and they still both succeed. edit: just add "import time; time.sleep(5)" to each file to see that they really run in parallel.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.