如何在 Linux 上激活 virtualenv?

当我们谈论将依赖项与我们的逻辑代码放在一个单独的位置时,我们实际上只是在创建一个虚拟环境,在 python 中,我们通常使用术语venv来指代它。

所以venv 只不过是一个虚拟环境,它反过来又是一个工具,允许我们将项目所需的依赖项保存在单独的文件夹中。我们创建的这些单独的文件夹称为 python 虚拟环境。

Python venv是使用最广泛的工具之一。

现在我们知道了 virtualenv 是什么以及它的用途,让我们看看如何在 linux 上用 python 创建一个,以及它提供什么特性和特性。

简单来说,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

以上是 如何在 Linux 上激活 virtualenv? 的全部内容, 来源链接: utcz.com/z/345848.html

回到顶部