Skip to content Skip to sidebar Skip to footer

Use Python2.7 In Venv Where Python3 Is Default Python

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 t

Solution 1:

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>

Solution 2:

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 3xwhich python27  #python version 2.7which 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.

Solution 3:

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.

Solution 4:

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.

Solution 5:

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.

Post a Comment for "Use Python2.7 In Venv Where Python3 Is Default Python"