1. 程式人生 > >詳解Anaconda + 如何在Anaconda上更換python版本

詳解Anaconda + 如何在Anaconda上更換python版本

前言

本文旨在詳細介紹Anaconda 以及 如何在Anaconda上更換python版本。

1 Anaconda具體是用來幹什麼的

Anaconda - The Most Popular Python Data Science Platform
Anaconda Distribution - With over 6 million users, the open source Anaconda Distribution is the fastest and easiest way to do Python and R data science and machine learning on Linux, Windows, and Mac OS X. It’s the industry standard for developing, testing, and training on a single machine.

Anaconda是最流行的資料科學平臺。但這個還不能解釋性質上的問題。

Anaconda is a free and open source[5] distribution of the Python and R programming languages for data science and machine learning related applications (large-scale data processing, predictive analytics, scientific computing), that aims to simplify package management and deployment. Package versions are managed by the package management system conda.[6] The Anaconda distribution is used by over 6 million users, and it includes more than 250 popular data science packages suitable for Windows, Linux, and MacOS.[5]

這下就更清楚了,Anaconda是一個免費,開源的針對資料科學處理和機器學習相關應用的Python和R語言的發行版。Anaconda發行版被6百萬人使用,而且它包括了超過250個流行的資料科學處理包(適用於各個平臺)。

Anaconda distribution comes with more than 1,000 data packages as well as the Conda package and virtual environment manager, called Anaconda Navigator [7], so it eliminates the need to learn to install each library independently.

Anaconda = 超過1000個數據包 + Conda package + virtual environment manager (即 Anaconda Navigator)。
這樣的話,anaconda在管理包上是很方便的。

The open source data packages can be individually installed from the Anaconda repository [8] with the conda install command or using the pip install command that is installed with Anaconda. Pip packages provide many of the features of conda packages and in most cases they can work together.

原來pip和conda可以work together。

The default installation of Anaconda2 includes Python 2.7 and Anaconda3 includes Python 3.7. However, you can create new environments that include any version of Python packaged with conda [10].

雖然預設安裝,但是我們可以用conda來創造python版本新環境。

對anaconda navigator的介紹:

Anaconda Navigator is a desktop graphical user interface (GUI) included in Anaconda distribution that allows users to launch applications and manage conda packages, environments and channels without using command-line commands. Navigator can search for packages on Anaconda Cloud or in a local Anaconda Repository, install them in an environment, run the packages and update them. It is available for Windows, macOS and Linux.


The following applications are available by default in Navigator [11]:
JupyterLab
Jupyter Notebook
QtConsole
Spyder
Glueviz
Orange
Rstudio
Visual Studio Code

原來anaconda navigator是幹這個的,用來管理包,安裝、執行應用。

對conda的介紹:

Conda is an open source,[12] cross-platform,[13] language-agnostic[14] package manager and environment management system[15][16][17] that installs, runs, and updates packages and their dependencies.[12] It was created for Python programs, but it can package and distribute software for any language (e.g., R), including multi-language projects.[14] The Conda package and environment manager is included in all versions of Anaconda, Miniconda,[18] and Anaconda Repository.[8]

conda最開始用來配置python,但是實際上可以配置任何語言的包。很酷。

關於anaconda cloud的介紹:

Anaconda Cloud is a package management service by Anaconda where you can find, access, store and share public and private notebooks, environments, and conda and PyPI packages. Cloud hosts useful Python packages, notebooks and environments for a wide variety of applications. You do not need to log in or to have a Cloud account, to search for public packages, download and install them.


You can build new packages using the Anaconda Client command line interface (CLI), then manually or automatically upload the packages to Cloud.

維基百科是真的給力,通過上述介紹,anaconda,conda,anaconda navigator的概念和作用就很明瞭了。

2 conda,pip,anaconda,miniconda的區別

網頁https://www.quora.com/What-is-the-comparison-among-conda-vs-pip-vs-anaconda 很好的回答了四種概念的區別:

1)conda:

conda is a package management tool for installing scientific and analytical computing packages, which may be written in Python or other programming languages.


conda also creates a virtual environment, like python-virtualenv does.

2)anaconda:

conda is the package manager of Anaconda. Anaconda is a free Python distribution provided by Continuum Analytics, which includes over 195 of the most popular Python packages for science, math, engineering and data analysis.


(Anaconda is a set of about a hundred packages including conda, numpy, scipy, ipython notebook, and so on.)

3)miniconda:

You installed Miniconda, which is a smaller alternative to Anaconda that is just conda and its dependencies (as opposed to Anaconda, which is conda and a bunch of other packages like numpy, scipy, ipython notebook, etc.). Once you have Miniconda, you can easily install Anaconda into it with conda install anaconda. )

4)pip and conda:

pip is a general purpose Python package installer. In addition, python-pip and python3-pip are in the default Ubuntu repositories. In most cases you would choose pip instead of conda if you want to install a Python package management application. To create a Python virtual environment without installing conda, you can install python-virtualenv from the Ubuntu Software Center.


pip and conda use different packaging formats so they do not operate interchangeably, but you can use both tools side by side.

看完這些描述,我總結一下:
conda是anaconda下面的一個包,也是一個包管理工具。anaconda則是一個python發行版本,包含有conda在內的各種包。miniconda就是隻包含conda和conda的依賴,對於其他包,可以用conda裝。pip是python 包下載管理工具,conda的話不僅是python 包下載管理工具,還可以下載其他語言的包(比如R語言),當然conda和pip是可以一起用的。

原來如此。想知道更多關於conda的,請見[4]

3 anaconda中的幾個應用介紹

開啟anaconda navigator,發現有一些應用:


在這裡插入圖片描述

根據網頁https://www.jianshu.com/p/eaee1fadc1e9 介紹:
Anaconda Navigtor :用於管理工具包和環境的圖形使用者介面,後續涉及的眾多管理命令也可以在 Navigator 中手工實現。
Jupyter notebook :基於web的互動式計算環境,可以編輯易於人們閱讀的文件,用於展示資料分析的過程。
qtconsole :一個可執行 IPython 的仿終端圖形介面程式,相比 Python Shell 介面,qtconsole 可以直接顯示程式碼生成的圖形,實現多行程式碼輸入執行,以及內建許多有用的功能和函式。
spyder :一個使用Python語言、跨平臺的、科學運算整合開發環境。

4 如何在anaconda下改變python的版本

使用以下命令建立新環境:
conda create -n env_name list of packages

其中 -n 代表 name,env_name 是需要建立的環境名稱,list of packages 則是列出在新環境中需要安裝的工具包。

比如我現在的python版本是3.7,但是我想安裝一個python 3.6的環境,則在anaconda prompt輸入:
conda create -n py36 python=3.6

控制檯會輸出:


在這裡插入圖片描述

以及

# To activate this environment, use
#
#     $ conda activate py36
#
# To deactivate an active environment, use
#
#     $ conda deactivate

還是很酷很方便的。

現在啟用這個新配置的環境:conda activate py36
輸入python --version,可以看到:

(py36) C:\Users\dehen>python --version
Python 3.6.6 :: Anaconda, Inc.

(py36) C:\Users\dehen>

這時候開啟anaconda navigator,發現多出來一個環境選擇,太酷了。
在這裡插入圖片描述

這時候install 這個spyder就可以執行3.6版本下的程式了。

在這裡插入圖片描述

索性再配置一個python2.7的新環境:conda create -n py27 python=2.7
很快就顯示配置完成並輸出提示:

# To activate this environment, use
#
#     $ conda activate py27
#
# To deactivate an active environment, use
#
#     $ conda deactivate

所以,這樣看來確實很方便,各個環境互相獨立,都放在資料夾 D:\software\anaconda\envs 下:
在這裡插入圖片描述

還有,如果要刪除我們配置的新環境,則:
conda env remove -n env_name

顯示所有環境:
conda env list

當分享程式碼的時候,同時也需要將執行環境分享給大家,執行如下命令可以將當前環境下的 package 資訊存入名為 environment 的 YAML 檔案中。[6]
conda env export > environment.yaml

同樣,當執行他人的程式碼時,也需要配置相應的環境。這時你可以用對方分享的 YAML 檔案來建立一摸一樣的執行環境。
conda env create -f environment.yaml

參考文獻