5

I have python2.7, python3.7, python3.6 on my machine. I am still not sure how manage and see location of all three python version.

Currently I just type python with version name to open that terminal or run script.

I did python3.6 default by aliasing in ~/.bashrc.

I have installed virtualenv using python3.6 so whenever I create venc by default it takes python3.6 in it.

To create venv with python2.7 I tried -

virtualenv -p /usr/bin/python2.7 /Users/karim/Documents/venv2.7

But here also in this venv2.7 when I see python version it give 3.6. When I type python2.7 in terminal it opens python2.7.

I can do aliasing for python2.7 being in virtual environment but I doubt that may create python2.7 default outside venv as well.

  1. How can I create virtual environment with python2.7 default?
  2. Can you refer me any article which explain how to manage multiple python version, switch from one to another, see there locations

I really checked all SO threads but no articles helped me to create venv with python2.7 in system having python3.6 default.

1
  • @saleem mentioned to install the python from software center ( i am assuming you are using ubuntu) . if python install form software center ( python 2 and python 3) you won't need to change path , just used keyword to invode python 3 and python2 . and then from below answer use any method to vreate virtual env Commented Feb 20, 2019 at 9:07

5 Answers 5

3

I did python3.6 default by aliasing in ~/.bashrc

That is the source of your problem. If I understand correctly you have created and alias for python, which causes your system to not search your PATH for your python version, but instead use your alias. This behaviour does not change when using virtualenv, because activating them will alter your PATH, but the alias still prevents other python versions from being used.

To make a python installation "default", you should alter your PATH by adding the desired python version to the front, not make aliases.

You can use the commands

which python3   #python version 3x
which python27  #python version 2.7
which python    # "default" python

to check the locations of your python installations, then add a line

export PATH="<Directory of desired python version>:$PATH"

to make your desired python version default.

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

4 Comments

Thanks for showing problem with aliases. Any help on altering PATH?
Currently when I open my bashrc it's empty. Current python is 3.6 (because when I type python, it opens python 3.6). Now suppose I want to create virtual environment with python 2.7, can you tell me what steps should I follow?
I did export, but still python2.7 is not available in venv by default
export has no bearing on aliases, and as the answer already explains, aliases bypass your PATH entirely anyway. unalias python should remove the alias from your current session.
2

from Install and run Python 3 at the same time than Python 2 you need to install (python 2 and python 3) the python from software center and then use the solution provided here to create a virtual env

if python 2.7 is in your system and installed it and you have use alias python27 in ~/.bashrc to run python 2.7

then you can create a virtual env

python27 -m virtualenv <path to venv>

3 Comments

if I do alias python27 in ~/.bashrc then it will make python27 defalut on my machine, right? I dont want python27 to be default on my machine. I just want to use it in venv
no it won't make it default, it is just keyword to run python2.7. if in bashrc python is associated with python 3.7 then using python you will associated with python 3.7 (default)
@JhonPatric you can use anaconda to work in different python environment also
1

Are you activating the virtual environment correctly? After creating the virtualenv you should issue the command

source /Users/karim/Documents/venv2.7/bin/activate

to change your environment so the python command refers to the interpreter in the virtualenv. Remember the virtualenv has nothing to do with your current working directory ...

Is it possible that the virtualenv you are using is associated with the wrong Python binary? I have a lot of Pythons on my system, including Python3.7 and Python 2.7 in /usr/local/bin :

fathead:~ sholden$ ls -l /usr/local/bin/python*
lrwxr-xr-x  1 sholden  staff   38  6 Dec 12:26 /usr/local/bin/python@ -> ../Cellar/python@2/2.7.15_1/bin/python
lrwxr-xr-x  1 sholden  staff   38  6 Dec 12:34 /usr/local/bin/python-build@ -> ../Cellar/pyenv/1.2.8/bin/python-build
lrwxr-xr-x  1 sholden  staff   45  6 Dec 12:26 /usr/local/bin/python-config@ -> ../Cellar/python@2/2.7.15_1/bin/python-config
lrwxr-xr-x  1 sholden  staff   39  6 Dec 12:26 /usr/local/bin/python2@ -> ../Cellar/python@2/2.7.15_1/bin/python2
lrwxr-xr-x  1 sholden  staff   46  6 Dec 12:26 /usr/local/bin/python2-config@ -> ../Cellar/python@2/2.7.15_1/bin/python2-config
lrwxr-xr-x  1 sholden  staff   41  6 Dec 12:26 /usr/local/bin/python2.7@ -> ../Cellar/python@2/2.7.15_1/bin/python2.7
lrwxr-xr-x  1 sholden  staff   48  6 Dec 12:26 /usr/local/bin/python2.7-config@ -> ../Cellar/python@2/2.7.15_1/bin/python2.7-config
lrwxr-xr-x  1 sholden  staff   34  6 Dec 12:22 /usr/local/bin/python3@ -> ../Cellar/python/3.7.1/bin/python3
lrwxr-xr-x  1 sholden  staff   41  6 Dec 12:22 /usr/local/bin/python3-config@ -> ../Cellar/python/3.7.1/bin/python3-config
lrwxr-xr-x  1 sholden  staff   36  6 Dec 12:22 /usr/local/bin/python3.7@ -> ../Cellar/python/3.7.1/bin/python3.7
lrwxr-xr-x  1 sholden  staff   43  6 Dec 12:22 /usr/local/bin/python3.7-config@ -> ../Cellar/python/3.7.1/bin/python3.7-config
lrwxr-xr-x  1 sholden  staff   37  6 Dec 12:22 /usr/local/bin/python3.7m@ -> ../Cellar/python/3.7.1/bin/python3.7m
lrwxr-xr-x  1 sholden  staff   44  6 Dec 12:22 /usr/local/bin/python3.7m-config@ -> ../Cellar/python/3.7.1/bin/python3.7m-config
lrwxr-xr-x  1 sholden  staff   39  6 Dec 12:26 /usr/local/bin/pythonw@ -> ../Cellar/python@2/2.7.15_1/bin/pythonw
lrwxr-xr-x  1 sholden  staff   40  6 Dec 12:26 /usr/local/bin/pythonw2@ -> ../Cellar/python@2/2.7.15_1/bin/pythonw2
lrwxr-xr-x  1 sholden  staff   42  6 Dec 12:26 /usr/local/bin/pythonw2.7@ -> ../Cellar/python@2/2.7.15_1/bin/pythonw2.7
-rwxr-xr-x@ 1 sholden  staff  230 25 Sep  2017 /usr/local/bin/pythonz*

The which (also type on MacOS) which tell you which executable a command is associated. I verified I was getting the right python3.7 with

fathead:~ sholden$ which python3.7
/usr/local/bin/python3.7

To ensure I use the correct Python's virtualenv I use the Python binary to execute it:

fathead:~ sholden$ python3.7 -m virtualenv -p /usr/local/bin/python2.7 venv2.7
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /Users/sholden/venv2.7/bin/python2.7
Also creating executable in /Users/sholden/venv2.7/bin/python
Installing setuptools, pip, wheel...
done.

It may turn out that your Python 3.7 doesn't have virtualenv installed. If so, install it with

fathead:~ sholden$ python3.7 -m pip install virtualenv
Looking in indexes: https://pypi.org/simple, https://pypi.python.org/simple
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/7e/1b/6c00d57127608793e16e8b7f813e64d58a1938505c42fe190d1386ab41e1/virtualenv-16.4.0-py2.py3-none-any.whl (2.0MB)
    100% |████████████████████████████████| 2.0MB 3.4MB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.0

After creating the virtualenv you should be able to activate it as described.

5 Comments

Yes, I do exactly to start this venv. But in virtual environment python terminal opens with python3.6
I've updated it to try and give a few more clues as to what might be going wrong here.\
I tried python3.7 -m virtualenv -p /usr/local/bin/python2.7 /Users/karim/Documents/ven2 which completed successfully but when I start it, it has 3.6
Are you positive /usr/local/bin/python2.7 isn't a 3.6 interpreter? Use the which command to see which interpreter runs when you type python.
And why 3.6 and not 3.7 - that's even weirder!
0

I exaplained in comments and Question description that I have added

alias python=python3.6

In bashrc

and tried to create python2.7 virtual environment, still it opens 3.6 in it.

Nobody talked about bash_profile file.

In some other article while looking at pyenv I saw they considered bash_profile for alias. I suddently checked mine, and

alias python=python3.6

was there in it. That is why it was taking python3.6 in python2.7 virtual environment as well.

I commented out this alias, then it does not take python 3.6 globally.

One important point to note is - You change alias and want to see it's effect, open new tab or terminal and test there. If you try in old running virtual environment it may show old python version only.

Thanks everyone for your answers and time. That helped me to learn new.

Comments

0

If you use pycharm, you can create a new virtual environment under settings > project > cog symbol, drodown menu: add.

Here you can specify which is your base python interpreter, point this to where python3 or python2 are installed and pycharm will do the hardwork for you.

Comments

Your Answer

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.