我有两个不同的Ubuntu VM,首先是14.04秒16.04。在第一个我用pyhton3创建virtualenv由于某种原因python27也被安装
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
在第二个VM上完成同样的工作
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
即使我键入python命令,我也需要virtualenv来运行python3。有什么想法怎么做?
[UPDATE]
这是我见过的最奇怪的事情
(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
[更新2]
我完全迷失了...甚至strace python
命令指向正确的文件
(py3-venv) user@ubuntu:~/venv$ strace python
execve("/home/user/venv/py3-venv/bin/python", ["python"], [/* 24 vars */]) = 0
brk(0) = 0x204f000
[更新3]
如果我从/home/user/venv/py3-venv/bin
python命令中删除PATH
以外的所有内容都无法执行...请帮助!!
(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
答案 0 :(得分:1)
我同意关于虚拟环境的观点。你可以做一些其他的事情:
首先我在.bashrc中删除python别名(来自其他答案)
然后我创建了一个Python 3虚拟环境:
$ 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 已成功安装为virtualenv。
您可以将以下行添加到.bash_profile:
source~ / venv / python3_virtualenv / bin / activate
我现在会记录 out ,然后再次登录 :
$ 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.
>>>
源〜/ venv / python3_virtualenv / bin / activate 命令已自动运行,当我输入 python 时,Python 3.5.0 找到了。
第二个选项是创建python 3的python符号链接:
我在/ usr / local / bin目录中手动安装了Python 3。
Python 2位于/ usr / bin目录中:
$ 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/
我创建了我的符号链接:
[vagrant@localhost bin]$ sudo ln -s python3 ./python
我退出:
[vagrant@localhost bin]$ exit
logout
Connection to 127.0.0.1 closed.
我再次登录:
rhubarb:scratch milesd$ vagrant ssh
Last login: Sat Jun 3 00:41:11 2017 from 10.0.2.2
/ usr / local / bin中的 python现在"首先在路径中选择":
[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 ~]$
答案 1 :(得分:0)
我发现我的虚拟环境出了什么问题。显然,有人在用户的主目录中创建了一个.bash_aliases
文件,并创建了别名,以便在执行python
命令时使用python27并覆盖我的venv的python。
(py3-venv) user@ubuntu:~$ python -V
Python 3.4.3
答案 2 :(得分:-1)
您需要在.bashrc文件中添加别名(别名python ='/ usr / local / bin / python3.5')或者您的python版本。
将此附加到您的.bashrc文件:
$ echo "alias python='/usr/local/bin/python3.5'" >> .bashrc
你可以看到我现在退出/退出。当我再次ssh和python --version时 - 别名将我带到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 ~]$