-1

My system is ubuntu 18.04. I have a pre-installed version 3 and 2 of python.

which python3
/usr/bin/python3
python3 -V
Python 3.6.9

which python
/usr/bin/python
python -V
Python 2.7.17

I need to create several virtual environments, one for python 2.7.15 and another for 2.6. how can I do that?

Jekson
  • 2,892
  • 8
  • 44
  • 79
  • 1
    Does this answer your question? [Use different Python version with virtualenv](https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv) – JeffUK Aug 12 '20 at 13:42
  • 1
    You need to install first the version of python you want to use and THEN set up a virtualenv based on that version – Marco Aug 12 '20 at 13:42
  • @Marco that's the question, how do I do it? – Jekson Aug 12 '20 at 14:02
  • https://www.python.org/downloads/ hre you find all python versions; you have to download the one you want then untar / unzip it and open README to see how to install. Once is installed you follow answer 2 – Marco Aug 12 '20 at 15:02
  • how to install python I know, I don't understand how to install 2.7 and 2.6 together for example – Jekson Aug 12 '20 at 15:10
  • Install Python 2.6; then you have access to the `python2.6` and `python2.7` commands to differentiate between them. Just make sure you don't install 2.6 in a default location, since that may likely mess up your system. – 9769953 Aug 12 '20 at 15:32

2 Answers2

0

There are different ways of creating virtual python environments. Three popular ones are

  • virtualenv
  • pipenv
  • conda

I personally like conda a lot.

virtualenv

Assuming you have pip installed, you get virtualenv with

pip install virtualenv

Once installed, you can change into a directory of your choice and create a virtual environment like this

virtualenv myenvironmentname 

If you want to use a different python version in your virtual environment, you can specify this with the --python flag.

virtualenv --python=/usr/bin/python2.6 myenvironmentname

However, please note that this requires you to have the python version you specify installed in advance, virtualenv will not take care of that for you (have a look at Use different Python version with virtualenv for more details). So you'll need local installations of the versions you desire.

You then can activate the environment with

myenvironmentname/bin/activate

and go ahead to use pip to install packages, etc. Have a look at

pip freeze --help

to find out on how to make your environment reusable.

pipenv

pipenv combines pip and virtualenv.

You can install it using

pip install --user pipenv

Pipenv takes care of dependencies on a project basis

cd myprojectfolder
pipenv install

This will create a Pipfile which will track dependencies and a virtualenv (see https://docs.python-guide.org/dev/virtualenvs/ for more details).

To create an environment using a specific version, you can do

pipenv install --python '/usr/bin/python2.6'

or

pipenv install --python 2.6

Cmp. Set python version when creating virtualenv using pipenv. If you also have pyenv installed, the second form will prompt pipenv to attempt to install non-existing versions, afaik.

conda

Anaconda Python is a python distribution (with a focus on data science) that comes with its own package and virtual environment manager named conda. Anaconda Python is not available in the official package repository of Ubuntu 18.04 LTS but needs to be installed in another way (the official documentation can be found here: https://docs.anaconda.com/anaconda/install/linux/).

To create an environment with conda, do

conda create --name myenvironmentname python=2.7.15 

In contrast to virtualenv, the environments are by default not created in the present working directory, but installed into the envs directory in your conda directory. conda will also take care to install the proper python version, that is at least as long as it is part of the default channel (see below).

You can then activate said environment with

conda activate myenvironmentname

As I wrote above, the python version you specify needs to be available from the configured conda channels. python2.6 however, was removed from the default channel. To remedy this, you can add the free channel back to your default list (see https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/free-channel.html for more details):

conda config --set restore_free_channel true

After that you can

conda create --name myotherenvironmentname python=2.6

And switch between the environments as you like

conda activate myotherenvironmentname
buddemat
  • 4,552
  • 14
  • 29
  • 49
-1

For python3 python -m venv <your_virtual_enviroment_path> for python2 virtualenv <your_virutal_enviroment_path>

The to activate source <your_virtual_environment_path>/bin/activate. And to deactivate deactivate. Finally to check what is activated echo $VIRTUAL_ENV

I strongly recommend for one virtual environment for each project.

Dharman
  • 30,962
  • 25
  • 85
  • 135
geckos
  • 5,687
  • 1
  • 41
  • 53
  • But I cant have python 2.7.15. I can only specify the path to the installed version of python 2.7.17 but that's not what I need. – Jekson Aug 12 '20 at 14:00
  • you can use the `-p` option, http://manpages.ubuntu.com/manpages/trusty/man1/virtualenv.1.html – geckos Aug 17 '20 at 16:40