和其他大多數(shù)現(xiàn)代編程語言一樣,Python對包和 模塊的下載、存儲以及管理有其自己的一套方法。但是當我們同時開發(fā)多個項目工程的時候,不同的項目會將第三方的包存放在 相同的路徑下。這就意味著,如果有兩個工程依賴同一個包,但是所需要的版本卻不一樣,比如項目A依賴v1.0.0,而項目B依賴v2.0.0。由于Python無法根據(jù)版本來區(qū)分包的安裝路徑,那么此時,就會發(fā)生版本沖突。
學(xué)習(xí)Python中的小伙伴,需要學(xué)習(xí)資料的話,可以到我的微信公眾號:Python學(xué)習(xí)知識圈,后臺回復(fù):“01”,即可拿Python學(xué)習(xí)資料
簡介
本文會從
virtualenvwrapper(virtualenv的進階版,其提供了一系列命令使得和虛擬環(huán)境工作變得便利。)
pipenv
anaconda
三個常用的Python虛擬環(huán)境配置來匯總,如有不適之處,還望各位大佬指正。 另:
- 博客撰寫以 windows操作系統(tǒng) 為基礎(chǔ)(囊中羞澀),后續(xù)有Money后,會更新其他系統(tǒng)的操作。
- 項目使用Python版本為3.7.3
virtualenvwrapper
安裝
<pre class="prettyprint hljs cmake" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install virtualenvwrapper
pip install virtualenvwrapper-win??#Windows使用該命令
復(fù)制代碼</pre>
路徑
詳見下圖指引(從左至右,依次操作)

使用
<pre class="prettyprint hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">mkvirtualenv test_env
workon
workon test_env
deactivate
rmvirtualenv test_env
</pre>
pipenv
一個好的程序員,果斷必須熟練 閱讀各種文檔,源碼 。追根溯源方能獲取真知!
特性
- pipenv集成了pip,virtualenv兩者的功能,且完善了兩者的一些缺陷。
- pipenv使用Pipfile和Pipfile.lock,查看包的依賴關(guān)系十分方便。
- Pipfile 文件是 TOML 格式而不是 requirements.txt 這樣的純文本。一個項目對應(yīng)一個 Pipfile,支持開發(fā)環(huán)境與正式環(huán)境區(qū)分。
安裝
pip install pipenv
使用
常用命令大全:
<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pipenv --where 列出本地工程路徑
pipenv --venv 列出虛擬環(huán)境路徑
pipenv --py 列出虛擬環(huán)境的Python可執(zhí)行文件
pipenv install 創(chuàng)建虛擬環(huán)境
pipenv isntall [moduel] 安裝包
pipenv install [moduel] --dev 安裝包到開發(fā)環(huán)境
pipenv uninstall[module] 卸載包
pipenv uninstall --all 卸載所有包
pipenv graph 查看包依賴
pipenv lock 生成lockfile
pipenv run python [pyfile] 運行py文件
pipenv --rm 刪除虛擬環(huán)境
復(fù)制代碼</pre>
備注:直接在項目根目錄下,使用 pipenv install xxx 即可創(chuàng)建項目環(huán)境,并安裝xxx包
anaconda
Anaconda 是一個包含數(shù)據(jù)科學(xué)常用包的 Python 發(fā)行版本。其使用 conda 創(chuàng)建環(huán)境,以便分隔使用不同 Python 版本和不同程序包的項目。
特性
- pip僅適用于Python,conda適用于Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++等語言
- conda結(jié)合了pip和virtualenv的功能。
- pip在系統(tǒng)自帶Python中,包的**更新/回退版本/卸載將影響其他程序;而conda不會影響系統(tǒng)自帶Python。
安裝
此處跳過安裝anaconda
使用
<pre class="hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">conda --version
</pre>
<pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">conda create --name <env_name> <package_names>
示例:
conda create --n test_conda_env
復(fù)制代碼</pre>
注意:
<env_name> 即創(chuàng)建的環(huán)境名。建議以英文命名,且不加空格,名稱兩邊不加尖括號“<>”。
如果要安裝指定的版本號,則只需要在包名后面以=和版本號的形式執(zhí)行。如:conda create --name python2 python=2.7,即創(chuàng)建一個名為“python2”的環(huán)境,環(huán)境中安裝版本為2.7的python。
如果要在新創(chuàng)建的環(huán)境中創(chuàng)建多個包,則直接在<package_names>后以空格隔開,添加多個包名即可。如:conda create -n python3 python=3.5 numpy pandas,即創(chuàng)建一個名為“python3”的環(huán)境,環(huán)境中安裝版本為3.5的python,同時也安裝了numpy和pandas。
<pre class="prettyprint hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">conda info --env
activate test_conda_env
deactivate
conda remove -n test_conda_env --all
</pre>
ok,這篇關(guān)于如何創(chuàng)建Python項目環(huán)境的文章就暫時先告一段落。 初寫博客,當然會有很多不足之處,如有錯誤遺漏的地方呢,還請各位老少爺們兒多多指教
