我安装了Miniconda3 (Python3Default),并使用conda创建了Python2虚拟环境:
~$ conda create -n myenv python=2
...
~$ source activate myenv
(myenv) ~$ conda list
# packages in environment at ~/miniconda3/envs/myenv:
#
# Name Version Build Channel
ca-certificates 2018.03.07 0
certifi 2018.10.15 py27_0
libedit 3.1.20170329 h6b74fdf_2
libffi 3.2.1 hd88cf55_4
libgcc-ng 8.2.0 hdf63c60_1
libstdcxx-ng 8.2.0 hdf63c60_1
ncurses 6.1 he6710b0_1
openssl 1.1.1a h7b6447c_0
pip 18.1 py27_0
python 2.7.15 h9bab390_4
readline 7.0 h7b6447c_5
setuptools 40.6.2 py27_0
sqlite 3.25.3 h7b6447c_0
tk 8.6.8 hbc83047_0
wheel 0.32.3 py27_0
zlib 1.2.11 h7b6447c_3 但是,如果我试图运行Python,它将使用Python 3:
(myenv) ~$ python
Python 3.7.1 (default, Oct 23 2018, 19:19:42)
[GCC 7.3.0] :: Anaconda, Inc. on linux如果我试图在环境中使用Python 2代码运行脚本,也会发生同样的情况。
(myenv) ~$ python hello2.py
File "hello2.py", line 1
print "Hello World in Python 2"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello World in Python 2")?我试着删除和重新创建环境,但没有什么区别。为什么会发生这种情况?
阑尾
如评论中所要求的那样提供更多信息:
(myenv) ~$ which python
~/miniconda3/envs/myenv/bin/python
(myenv) ~$ ls ~/miniconda3/envs/myenv/bin/python -l
lrwxrwxrwx 1 user user 9 Dec 3 22:43 ~/miniconda3/envs/myenv/bin/python -> python2.7
(myenv) ~$ echo $PATH
~/miniconda3/envs/myenv/bin:~/miniconda3/bin:[rest of usual PATH]
(myenv) ~$ alias
[...]
alias python='python3'发布于 2019-03-28 14:47:39
问题是别名:
alias python='python3'此别名将在shell启动脚本中的某个位置设置。如果您使用bash,那就是.bashrc、.bash_profile或.profile。找到并移除它。
别名扩展优先于PATH查找(别名扩展首先发生)。如果无法找到设置别名的位置,则可以在.bashrc (或.profile,或两者兼而有之)中显式取消python的别名:
unalias python无论如何,您可以通过引用Python令牌来避免在运行python时使用别名:
\python hello2.pyhttps://stackoverflow.com/questions/53602984
复制相似问题