3

I created a folder via virtualenv command, but the result isn't what I wanted.

[root@localhost opt]# virtualenv my_env
New python executable in /opt/my_env/bin/**python2.6**
Also creating executable in /opt/my_env/bin/python
Installing setuptools, pip, wheel...done.

My system is CentOS 6.5. Before I created my folder I upgraded my python 2.6 to python 3.6. Then I wanted to create an isolated environment to practice Django. Unfortunately, the folder has python 2.6, it should be python 3.6. Can someone tell me what happened?

Arunmozhi
  • 1,034
  • 8
  • 17
Walle Luo
  • 39
  • 4

4 Answers4

2

I upgraded my python 2.6 to python 3.6

In the Python universe, Python 2 and Python 3 are two different things. So what you actually did was to install Python 3 alongside Python 2. Now your system has both Python 2 (which can be run by the command python) and also Python 3 (which can be run by the command python3)

So when you ran virtualenv my_env it fired the default Python interpreter which is Python 2 in CentOS 6.x. That's the reason for Python 2.6 in your virtual environment.

Setting up Virtual Environment with Python 3

To get a Python 3 interpreter in your virtual environment run:

virtualenv -p python3 my_env
Arunmozhi
  • 1,034
  • 8
  • 17
1

You could go with python3 -m venv env No need for virtualenv

What you did

virtualenv my_env

is invoking the python2 virtualenv command to prepare a python2 environment.

Besides, you should have a look at Pipenv

Dodge
  • 3,219
  • 3
  • 19
  • 38
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
0

Did you try using -p option? as in:

$ virtualenv -p /usr/bin/python3.5 venv
-1

Since you are on linux and many programs will start to use python3 as default, you can create an alias for python2 and 3 in order to virtually switch the default python to python3 in your system. To do that, just add these lines to your ~/.bashrc or ~/.bash_aliases file:

alias python='/usr/bin/python3'
alias python2='/usr/bin/python'

This will make it easy to work with the latest Django version while not breaking any current python program from running. And it's easy to undo. Scripts can start with #!/usr/bin/python3 or #!/usr/bin/python to respectively use python3 or python2 directly.

Luís Fernando
  • 133
  • 2
  • 12