如何在 Linux 上啟用 virtualenv?
當我們談到將依賴項與邏輯程式碼分開儲存時,我們實際上是在建立虛擬環境,在 Python 中,我們通常使用術語 **venv** 來指代它。
因此,**venv** 只是一個虛擬環境,它反過來是一個工具,允許我們將專案所需的依賴項儲存在單獨的資料夾中。我們建立的這些單獨的資料夾稱為 Python 虛擬環境。
Python **venv** 是最廣泛使用的工具之一。
現在我們知道了什麼是 virtualenv 以及它的用途,讓我們看看如何在 Linux 上的 Python 中建立一個 virtualenv 以及它提供了哪些特性和功能。
簡單來說,venv 工具只是 Python 中的一個模組,它用於支援建立既輕量級又包含自身站點目錄的“虛擬環境”。這些虛擬環境與系統的站點目錄隔離。還應該注意的是,這些虛擬環境有自己的 Python 二進位制檔案,並且還可以有自己的一套已經安裝在其站點目錄中的 Python 包。
建立虛擬環境
可以使用下面顯示的命令建立虛擬環境:
python3 -m venv /path_to_new_virtual_environment
現在讓我們在 Unix 環境中執行上述命令,我正在使用 Mac OS,該命令將類似於以下內容:
python3 -m venv /Users/immukul/linux-questions-code
執行命令後,您不會收到任何訊息,終端將返回到其起始位置,現在您只需要找到建立虛擬環境的目錄,並在該目錄內,您必須擁有與以下輸出類似或相同的 檔案:
immukul@192 linux-questions-code % ls -ltr total 16 -rw-r--r-- 1 immukul staff 28 Jul 4 13:33 textfile.txt drwxr-xr-x 2 immukul staff 64 Jul 5 20:52 include drwxr-xr-x 3 immukul staff 96 Jul 5 20:52 lib -rw-r--r-- 1 immukul staff 90 Jul 5 20:52 pyvenv.cfg drwxr-xr-x 12 immukul staff 384 Jul 5 20:52 bin
這就是如何在 Linux 中建立 virtualenv 的方法。**venv** 模組允許我們使用和執行某些引數,這些引數主要可以透過向終端寫入以下命令來獲取:
python3 -m venv
此命令將輸出所有位置引數以及您可以在 **venv** 模組中使用的可選引數。
輸出
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps] ENV_DIR [ENV_DIR ...] venv: error: the following arguments are required: ENV_DIR immukul@192 ~ % python3 -m venv -h usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps] ENV_DIR [ENV_DIR ...] Creates virtual Python environments in one or more target directories. Positional arguments: ENV_DIR A directory to create the environment in. Optional arguments: -h, --help Show this help message and exit --system-site-packages Give the virtual environment access to the system site-packages dir. --symlinks Try to use symlinks rather than copies, when symlinks are not the default for the platform. --copies Try to use copies rather than symlinks, even when symlinks are the default for the platform. --clear Delete the contents of the environment directory if it already exists, before environment creation. --upgrade Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place. --without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default) --prompt PROMPT Provides an alternative prompt prefix for this environment. upgrade-deps Upgrade core dependencies: pip setup tools to the latest version in PyPI
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP