<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](whatnow.xhtml "13. 接下來?") | - [上一頁](stdlib2.xhtml "11. 標準庫簡介 —— 第二部分") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 教程](index.xhtml) ? - $('.inline-search').show(0); | # 12. 虛擬環境和包 ## 12.1. 概述 Python應用程序通常會使用不在標準庫內的軟件包和模塊。應用程序有時需要特定版本的庫,因為應用程序可能需要修復特定的錯誤,或者可以使用庫的過時版本的接口編寫應用程序。 這意味著一個Python安裝可能無法滿足每個應用程序的要求。如果應用程序A需要特定模塊的1.0版本但應用程序B需要2.0版本,則需求存在沖突,安裝版本1.0或2.0將導致某一個應用程序無法運行。 這個問題的解決方案是創建一個 [virtual environment](../glossary.xhtml#term-virtual-environment),一個目錄樹,其中安裝有特定Python版本,以及許多其他包。 然后,不同的應用可以使用不同的虛擬環要解決先前的沖突需求示例,應用程序A可以擁有自己的1.0版本安裝虛擬環境,而應用程序B則具有2.0版本的另一個虛擬環境。如果應用程序B要求將庫升級到3.0版本,也不會影響應用程序A的環境。 ## 12.2. 創建虛擬環境 用于創建和管理虛擬環境的模塊稱為 [`venv`](../library/venv.xhtml#module-venv "venv: Creation of virtual environments.")。[`venv`](../library/venv.xhtml#module-venv "venv: Creation of virtual environments.") 通常會安裝你可用的最新版本的 Python。如果您的系統上有多個版本的 Python,您可以通過運行 `python3` 或您想要的任何版本來選擇特定的Python版本。 要創建虛擬環境,請確定要放置它的目錄,并將 [`venv`](../library/venv.xhtml#module-venv "venv: Creation of virtual environments.") 模塊作為腳本運行目錄路徑: ``` python3 -m venv tutorial-env ``` 如果它不存在,這將創建 `tutorial-env` 目錄,并在其中創建包含Python解釋器,標準庫和各種支持文件的副本的目錄。 創建虛擬環境后,您可以激活它。 在Windows上,運行: ``` tutorial-env\Scripts\activate.bat ``` 在Unix或MacOS上,運行: ``` source tutorial-env/bin/activate ``` (這個腳本是為bash shell編寫的。如果你使用 **csh** 或 **fish** shell,你應該改用 `activate.csh` 或 `activate.fish` 腳本。) Activating the virtual environment will change your shell's prompt to show what virtual environment you're using, and modify the environment so that running `python` will get you that particular version and installation of Python. For example: ``` $ 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'] >>> ``` ## 12.3. 使用pip管理包 你可以使用一個名為 **pip** 的程序來安裝、升級和移除軟件包。默認情況下 `pip` 將從 Python Package Index <<https://pypi.org>> 安裝軟件包。你可以在瀏覽器中訪問 Python Package Index 或是使用 `pip` 受限的搜索功能: ``` (tutorial-env) $ pip search astronomy skyfield - Elegant astronomy for Python gary - Galactic astronomy and gravitational dynamics. novas - The United States Naval Observatory NOVAS astronomy library astroobs - Provides astronomy ephemeris to plan telescope observations PyAstronomy - A collection of astronomy related tools for Python. ... ``` `pip` 有許多子命令:“search”、“install”、“uninstall”、“freeze”等等。(請參閱 [安裝 Python 模塊](../installing/index.xhtml#installing-index) 指南以了解 `pip` 的完整文檔。) 您可以通過指定包的名稱來安裝最新版本的包: ``` (tutorial-env) $ 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) $ 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 ``` 如果你重新運行這個命令,`pip` 會注意到已經安裝了所請求的版本并且什么都不做。您可以提供不同的版本號來獲取該版本,或者您可以運行 `pip install --upgrade` 將軟件包升級到最新版本: ``` (tutorial-env) $ 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 ``` `pip uninstall` 后跟一個或多個包名稱將從虛擬環境中刪除包。 `pip show` 將顯示有關特定包的信息: ``` (tutorial-env) $ 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: me@kennethreitz.com License: Apache 2.0 Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages Requires: ``` `pip list` 將顯示虛擬環境中安裝的所有軟件包: ``` (tutorial-env) $ pip list novas (3.1.1.3) numpy (1.9.2) pip (7.0.3) requests (2.7.0) setuptools (16.0) ``` pip freeze` 將生成一個類似的已安裝包列表,但輸出使用 `pip install` 期望的格式。一個常見的約定是將此列表放在 `requirements.txt` 文件中: ``` (tutorial-env) $ pip freeze > requirements.txt (tutorial-env) $ cat requirements.txt novas==3.1.1.3 numpy==1.9.2 requests==2.7.0 ``` 然后可以將 `requirements.txt` 提交給版本控制并作為應用程序的一部分提供。然后用戶可以使用 `install -r` 安裝所有必需的包: ``` (tutorial-env) $ 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 ``` `pip` 有更多選擇。有關 `pip` 的完整文檔,請參閱 [安裝 Python 模塊](../installing/index.xhtml#installing-index) 指南。當您編寫一個包并希望在 Python 包索引中使它可用時,請參考 [分發 Python 模塊](../distributing/index.xhtml#distributing-index) 指南。 ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](whatnow.xhtml "13. 接下來?") | - [上一頁](stdlib2.xhtml "11. 標準庫簡介 —— 第二部分") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 教程](index.xhtml) ? - $('.inline-search').show(0); | ? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看