gpt4 book ai didi

python - Conda python=2 环境使用Python 3

转载 作者:行者123 更新时间:2023-12-01 08:38:18 25 4
gpt4 key购买 nike

我已经安装了 Miniconda3(默认为 Python 3),并使用 conda 创建了 Python 2 虚拟环境:

~$ 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'

最佳答案

问题是别名:

alias python='python3'

此别名将设置在 shell 启动脚本中的某个位置。如果您使用的是 bash,则为 .bashrc.bash_profile.profile。找到它并将其删除。

别名扩展优先于 PATH 查找(别名扩展首先发生)。如果您找不到别名的设置位置,可以在 .bashrc(或 .profile,或两者)中显式取消别名 python:

unalias python

无论如何,当您想通过引用 python token 来运行 Python 时,您都可以避免使用别名:

\python hello2.py

关于python - Conda python=2 环境使用Python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53602984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com