GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing python.github.io — 12. 虛擬環境與套件 — Python 3.15.2 說明文件 12. 虛擬環境與套件¶12.1. 簡介¶Python 應用程式通常會用到不在標準函式庫的套件和模組。應用程式有時候會需要某個特定版本的函式庫,因為這個應用程式可能需要某個特殊的臭蟲修正,或是這個應用程式是根據該函式庫特定版本的介面所撰寫。 這意味著不太可能安裝一套 Python 就可以滿足所有應用程式的要求。如果應用程式 A 需要一個特定的模組的 1.0 版,但另外一個應用程式 B 需要 2.0 版,那麼這整個需求不管安裝 1.0 或是 2.0 都會衝突,以致於應用程式無法使用。 解決方案是建立一個虛擬環境 (virtual environment),這是一個獨立的資料夾,並且裡面裝好了特定版本的 Python,以及一系列相關的套件。 不同的應用程式可以使用不同的虛擬環境。以前述中需要被解決的例子中,應用程式 A 能夠擁有它自己的虛擬環境,並且是裝好 1.0 版,然而應用程式 B 則可以是用另外一個有 2.0 版的虛擬環境。要是應用程式 B 需要某個函式庫被升級到 3.0 版,這並不會影響到應用程式 A 的環境。 12.2. 建立虛擬環境¶用來建立與管理虛擬環境的模組叫做 在建立虛擬環境的時候,在你決定要放該虛擬環境的資料夾之後,以腳本 (script) 執行 python -m venv tutorial-env 如果 虛擬環境的常用資料夾位置是 一旦你建立了一個虛擬環境,你可以啟動它。 在 Windows 系統中,使用: tutorial-env\Scripts\activate 在 Unix 或 MacOS 系統,使用: source tutorial-env/bin/activate (這段程式碼適用於 bash shell。如果你是用 csh 或者 fish shell,應當使用替代的 啟動虛擬環境會改變你的 shell 提示字元來顯示你正在使用的虛擬環境,並且修改環境以讓你在執行 $ source ~/envs/tutorial-env/bin/activate (tutorial-env) $ python Python 3.5.1 (default, May 6 2016, 10:59:36) ... >>> import sys >>> sys.path ['', '/usr/local/lib/python35.zip', ..., '~/envs/tutorial-env/lib/python3.5/site-packages'] >>> 請注意,已啟用的虛擬環境完全不會更動 要停用虛擬環境,輸入: deactivate
於終端機中。 12.3. 用 pip 管理套件¶你可以使用一個叫做 pip 的程式來安裝、升級和移除套件。
你可以透過指定套件名字來安裝最新版本的套件: (tutorial-env) $ python -m pip install novas Collecting novas Downloading novas-3.1.1.3.tar.gz (136kB) Installing collected packages: novas Running setup.py install for novas Successfully installed novas-3.1.1.3 你也可以透過在套件名稱之後接上 (tutorial-env) $ python -m pip install requests==2.6.0 Collecting requests==2.6.0 Using cached requests-2.6.0-py2.py3-none-any.whl Installing collected packages: requests Successfully installed requests-2.6.0 要是你重新執行此命令, (tutorial-env) $ python -m pip install --upgrade requests Collecting requests Installing collected packages: requests Found existing installation: requests 2.6.0 Uninstalling requests-2.6.0: Successfully uninstalled requests-2.6.0 Successfully installed requests-2.7.0
(tutorial-env) $ python -m pip show requests --- Metadata-Version: 2.0 Name: requests Version: 2.7.0 Summary: Python HTTP for Humans. Home-page: http://python-requests.org Author: Kenneth Reitz Author-email: [email protected] License: Apache 2.0 Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages Requires:
(tutorial-env) $ python -m pip list novas (3.1.1.3) numpy (1.9.2) pip (7.0.3) requests (2.7.0) setuptools (16.0)
(tutorial-env) $ python -m pip freeze > requirements.txt (tutorial-env) $ cat requirements.txt novas==3.1.1.3 numpy==1.9.2 requests==2.7.0
(tutorial-env) $ python -m pip install -r requirements.txt Collecting novas==3.1.1.3 (from -r requirements.txt (line 1)) ... Collecting numpy==1.9.2 (from -r requirements.txt (line 2)) ... Collecting requests==2.7.0 (from -r requirements.txt (line 3)) ... Installing collected packages: novas, numpy, requests Running setup.py install for novas Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |