简体   繁体   English

如何在conda环境中将python 2.7替换为3.6?

[英]How to replace python 2.7 to 3.6 in a conda environment?

I created a conda environment named AAA with python 2.7 long ago and I want to update the python to 3.6 without any change of other packages in AAA. 我很早以前用python 2.7创建了一个名为AAA的conda环境,我想将python更新到3.6,而AAA中的其他软件包没有任何变化。 I try conda install python=3.6 in AAA, but all packages were emptied. 我尝试在AAA中使用conda install python=3.6 ,但所有软件包都被清空。 What should I do to solve it? 我该怎么解决?

you'll have to reinstall packages for python 3.7. 您必须重新安装python 3.7的软件包。 So it's better way to create new venv under 3.7. 因此,这是在3.7下创建新venv的更好方法。 you can you pip freeze to get the list of installed packages and put it into requirements.txt file. 您可以pip freeze以获取已安装软件包的列表,并将其放入Requirements.txt文件中。 then use it with pip3 install -r requirements.txt command. 然后将其与pip3 install -r requirements.txt命令一起使用。 This will install them 这将安装它们

more to that there's pip3 specially for python3.* which installs compatible packages. 除此之外,还有专门用于python3。*的pip3,它会安装兼容的软件包。

BTW be aware that python2 code is not compatible with python3 顺便说一句,请注意python2代码与python3不兼容

As @StanislavLipovenko pointed out , Python 2.7 packages can't run under Python 3.6 (they're installed to a different site-packages directory and usually aren't co-compatible), so you need a fresh install. 正如@StanislavLipovenko指出的那样 ,Python 2.7软件包不能在Python 3.6下运行(它们已安装到其他site-packages目录中,并且通常不兼容),因此您需要全新安装。

One option is to export your env configuration as a YAML: 一种选择是将环境配置导出为YAML:

conda env export -n my_py27_env --no-builds > my_env.yaml

Edit the line with - python=2.7.* to - python=3.6 . 使用- python=2.7.*- python=3.6编辑该行。 Then create a new env from this: 然后从此创建一个新的环境:

conda env create -f my_env.yaml -n my_py36_env

You may have some hiccups getting all the packages listed in the YAML to install, especially if your original env is quite old. 您可能会遇到麻烦,无法安装YAML中列出的所有软件包,尤其是当您的原始环境很旧时。 For example, a package that had a Python 2.7 version may never have released that same version for Python 3.6. 例如,具有Python 2.7版本的软件包可能永远不会为Python 3.6发布相同版本。 You can always soften the requirements by deleting the version numbers in the YAML for packages that present such barriers. 您始终可以通过删除YAML中存在此类障碍的软件包的版本号来简化要求。 This is simply the unavoidable cost of transitioning. 这仅仅是过渡的不可避免的成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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