简体   繁体   中英

Virtualenv with only Python3

I have two different Ubuntu VMs first 14.04 second 16.04. On the first one when I create virtualenv with pyhton3 for some reason python27 is also gets installed

user@ubuntu:~/venv$ virtualenv  --python=python3 --no-site-packages py3-venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/user/venv/py3-venv/bin/python3
Also creating executable in /home/user/venv/py3-venv/bin/python
Installing setuptools, pip, wheel...done.
user@ubuntu:~/venv$ source py3-venv/bin/activate
(py3-venv) user@ubuntu:~/venv$ python -V
Python 2.7.12

Same thing done on the second VM works as intended

user@my-box:~/venv$ virtualenv -p /usr/bin/python3 py3-venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/user/venv/py3-venv/bin/python3
Also creating executable in /home/user/venv/py3-venv/bin/python
Installing setuptools, pip, wheel...done.
user@my-box:~/venv$ source py3-venv/bin/activate
(py3-venv) user@my-box:~/venv$ which python
/home/user/venv/py3-venv/bin/python
(py3-venv) user@my-box:~/venv$ python -V
Python 3.5.2

I need the virtualenv to run python3 even though I type python command. Any ideas how to do it?

[UPDATE]

This is the strangest thing I have ever seen

(py3-venv) user@ubuntu:~/venv$ which python
/home/user/venv/py3-venv/bin/python
(py3-venv) user@ubuntu:~/venv$ python -V
Python 2.7.12
(py3-venv) user@ubuntu:~/venv$ ./py3-venv/bin/python -V
Python 3.4.3

[UPDATE 2]

I am completely lost... Even strace python command points to the correct file

(py3-venv) user@ubuntu:~/venv$ strace python
execve("/home/user/venv/py3-venv/bin/python", ["python"], [/* 24 vars */]) = 0
brk(0)                                  = 0x204f000

[UPDATE 3]

If I remove everything other than /home/user/venv/py3-venv/bin from the PATH python command fails to execute... Please help!!

(py3-venv) user@ubuntu:~$ export PATH=/home/user/venv/py3-venv/bin
(py3-venv) user@ubuntu:~$ echo $PATH
/home/user/venv/py3-venv/bin
(py3-venv) user@ubuntu:~$ python -V
Command 'python2.7' is available in the following places
 * /usr/bin/python2.7
 * /usr/local/bin/python2.7
The command could not be located because '/usr/bin:/usr/local/bin' is not included in the PATH environment variable.
python2.7: command not found

I agree with the point about the virtual env. You can do some other things:

Firstly I remove the python alias (from the other answer) in .bashrc

Then I create a Python 3 virtual environment:

$ mkdir venv
[vagrant@localhost ~]$ cd venv
[vagrant@localhost venv]$ virtualenv -p python3 python3_virtualenv
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local'
New python executable in /home/vagrant/venv/python3_virtualenv /bin/python3
Also creating executable in /home/vagrant/venv/python3_virtualenv/bin/python
Installing setuptools, pip, wheel...done.
[vagrant@localhost venv]$ source ~/venv/python3_virtualenv/bin/activate
(python3_virtualenv) [vagrant@localhost venv]$

Python 3 has been successfully installed as a virtualenv.

  1. You can add the following line to your .bash_profile:

    source ~/venv/python3_virtualenv/bin/activate

I will now log out , and then log in again :

$ vagrant ssh
Last login: Sat Jun  3 00:01:21 2017 from 10.0.2.2
(python3_virtualenv) [vagrant@localhost ~]$ python
Python 3.5.0 (default, Jun  2 2017, 18:10:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The source ~/venv/python3_virtualenv/bin/activate command has been run automatically, and when I type python , Python 3.5.0 is found.


The second option is to create a python symbolic link to python 3:

  1. The second option is to add a python symbolic link in /usr/local/bin. This will overide the python (in the /usr/bin directory).

I installed Python 3 manually in the /usr/local/bin directory.

Python 2 is in /usr/bin directory:

$ vagrant ssh
Last login: Sat Jun  3 00:39:12 2017 from 10.0.2.2

[vagrant@localhost ~]$ which python3
/usr/local/bin/python3

[vagrant@localhost ~]$ which python2
/usr/bin/python2
[vagrant@localhost ~]$ cd /usr/local/bin/

I create my symbolic link:

[vagrant@localhost bin]$ sudo ln -s python3 ./python

I log out:

[vagrant@localhost bin]$ exit
logout
Connection to 127.0.0.1 closed.

I log in again:

rhubarb:scratch milesd$ vagrant ssh
Last login: Sat Jun  3 00:41:11 2017 from 10.0.2.2

The python in /usr/local/bin is now "picked up in the path first":

[vagrant@localhost ~]$ python
Python 3.5.0 (default, Jun  2 2017, 18:10:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[vagrant@localhost ~]$

I found what was wrong with my virtual environment. Apparently, somebody created a .bash_aliases file in user's home directory and created alias to use python27 when python command was executed and that was overriding my venv's python.

(py3-venv) user@ubuntu:~$ python -V
Python 3.4.3

You need to add an alias in your .bashrc file (alias python='/usr/local/bin/python3.5') or whatever your version of python is.

Append this to your .bashrc file:

$ echo "alias python='/usr/local/bin/python3.5'" >> .bashrc

You can see I now exit / logout. When I ssh again, and python --version - the alias takes me to python3:

[vagrant@localhost ~]$ exit
logout
Connection to 127.0.0.1 closed.
rhubarb:scratch milesd$ vagrant ssh
Last login: Fri Jun  2 18:15:57 2017 from 10.0.2.2
[vagrant@localhost ~]$ python --version
Python 3.5.0
[vagrant@localhost ~]$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM