[Python]在Anaconda中配置多版本Python(Multi-version Python Configuration)

Python很惱人的擁有2和3兩個大版本,左手情懷,右手夢想。

因爲Tensorflow只支持python3以及python3畢竟是大勢所趨,在機器學習的課程或應用中往往採用python3作爲應用語言(當然GraphLab是支持python2的)。而很多python的傳統應用或較早的git項目中,python2更爲常見。因此本文討論該如何在Anaconda中配置與切換多版本Python。

本文默認是採用Anaconda進行Python的安裝和配置,畢竟當前Anaconda比PIP要便捷的多,也更加普遍。關於如何使用Anaconda安裝Python本文不再贅述。可以去其官網上下載並配置,上面有很詳細的教程。https://www.anaconda.com/download/  當然百度Google也都可以,過程便捷迅速相當無腦。

在安裝好後,可以在cmd(Windows)/command(Linux/Unix/Macintosh)中輸入python --version查看當前默認python版本。假設當前默認python版本爲2.7,而我們想要python3.6,該如何便捷的配置環境呢?

過程非常簡單。

Notice:如果是在大陸地區,建議添加清華鏡像,下載速度上升幾個臺階。在cmd/command中輸入

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls yes

conda config --remove channels defaults


1. 下載

conda create --name python36 python=3.6

【注:“python36”是環境名,可以任起,比如“Hahaha”之類,隨便。“3.6”爲想要安裝的python版本,想裝python2就改成“2.7”就行】

2.激活

activate python36 (Windows)

source activate python36 (Linux/Unix/Mac)


好的結束了就這麼簡單。

之後想用什麼python版本時直接activate就好。卸載某版本命令爲 conda remove --name python36 --all, 同理python36是你之前起的環境名。


當然別忘了在新的python環境名中把該裝的包裝一裝,比如tensorflow啊numpy啊什麼的

在activate的版本中:

conda install tensorflow

conda install numpy

......


以及IDE,如果需要,要重新裝一下對應版本的。

在activate的版本中:

conda install jupyter notebook

conda install spyder

.......


就醬。


Python has two big version: python2 and python3 and both have great practical scenerios.

This article talks about how to download and configure Python version.


I suppose that your initial configuration is based on Anaconda, as it is much more convenient and widespread than customed PIP. How to install Python with Anaconda will not be mentioned in this article. If you want to know, go to Anaconda website(https://www.anaconda.com/download/), which contains all the sources and tutorials you need. The process is very easy.


With installed Python and Anaconda, we can use "python --version" on cmd(Windows)/command(Linux/Unix/Macintosh) to see the default python version in the computer.

Supposing that the default version is 2.7, while we want Python3.6, how can we solve this problem?


Notice that if you are on Mainland, China, using Tsinghua Mirror Sever would speed up much. Just input these in your cmd/command:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls yes

conda config --remove channels defaults


1.Download

conda create --name python36 python=3.6

[Notice that the "python36" is the enviroment name, you can change it to any name you like. "3.6"stands for the python version you want. If you desire for Python2.7, just change it to "2.7"]


2.Activation

activate python36 (Windows)

source activate python36 (Linux/Unix/Mac)


Done. Very easy.

That's all. And any time you want to use a certain version , just activate it. If you want to uninstall a version, use "conda remove --name python36 --all", of course the "python36" is corresponding to the name you set before.


Don't forget those packages,like numpy and tensorflow(which must be used in Python3)

(in your activated Python version)

conda install tensorflow

conda install numpy

......


And those IDEs if you want:

(in your activated Python version)

conda install jupyter notebook

conda install spyder

.......


That's all.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章