创建python3虚拟环境_使用python3创建虚拟环境

本文介绍了如何创建Python3的虚拟环境,以实现项目依赖隔离。通过翻译自Medium的文章,详细步骤帮助读者理解并实践虚拟环境的建立。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建python3虚拟环境

什么是虚拟环境? (What is a Virtual Environment?)

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them

虚拟环境是一种工具,可通过为它们创建隔离的Python虚拟环境来帮助将不同项目所需的依赖项分开

它为什么如此重要? (Why is it important?)

Python “Virtual Environments” allow Python packages to be installed in an isolated location for a particular application, rather than being installed globally.

Python“虚拟环境”允许将Python 软件包安装在特定应用程序的隔离位置,而不是全局安装。

我们什么时候需要虚拟环境? (When do we need a Virtual Environment?)

Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python3.6/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

假设您有一个需要使用LibFoo版本1的应用程序,但是另一个应用程序需要版本2。您如何同时使用这两个应用程序? 如果将所有内容都安装到/usr/lib/python3.6/site-packages(或平台的标准位置是什么)中,那么很容易以无意中升级不应升级的应用程序的情况结束。

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

或更笼统地说,如果您要安装应用程序并保留原样该怎么办? 如果某个应用程序可以运行,则其库或这些库的版本中的任何更改都可能破坏该应用程序。

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

另外,如果无法将软件包安装到全局site-packages目录中怎么办? 例如,在共享主机上。

In all these cases, virtual environments can help you. They have their own installation directories and they don’t share libraries with other virtual environments.

所有这些情况下,虚拟环境都可以为您提供帮助。 它们具有自己的安装目录,并且不与其他虚拟环境共享库。

如何使用Python3安装虚拟环境? (How to Install Virtual Environment with Python3?)

Image for post

First of all, we are going to check where our ‘global’ environment currently lives through the terminal (zsh):

首先,我们将通过终端(zsh)检查“全局”环境当前所在的位置:

which pip3

which pip3

which tells us that our python install lives in /usr/bin/pip3

告诉我们我们的python安装位于/usr/bin/pip3

Now, we need to create a directory:

现在,我们需要创建一个目录:

mkdir my_python_project

mkdir my_python_project

Next, we change the directory to the newly created one:

接下来,我们将目录更改为新创建的目录:

cd my_python_project

cd my_python_project

Now, we are going to create a virtual environment inside a subdirectory of the current directory:

现在,我们将在当前目录的子目录中创建一个虚拟环境:

python3 -m venv ./venv

python3 -m venv ./venv

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. venvis available by default in Python 3.3 and later

venv模块支持使用其自己的站点目录创建轻型“虚拟环境”,并有选择地与系统站点目录隔离。 venv在Python 3.3和更高版本中默认可用

奖金: (Bonus:)

In order to view all the different files of a folder in a structured format, you can use a package calledtree

为了以结构化格式查看文件夹的所有不同文件,可以使用称为tree的包

Install: brew install tree

安装: brew install tree

You can notice the changes by checking it (e.g. tree venv/)

您可以通过检查来注意到更改(例如, tree venv/)

Image for post

如何使用虚拟环境? (How to use Virtual Environment?)

After creating the virtual environment, to use it, we need to activate the virtual environment:

创建虚拟环境后,要使用它,我们需要激活虚拟环境:

source venv/bin/activate

source venv/bin/activate

(here venv is the name of the subfolder)

(这里 venv 是子文件夹的名称)

Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal. This will let you know that the virtual environment is currently active.

激活虚拟环境后,虚拟环境的名称将显示在终端的左侧。 这将使您知道虚拟环境当前处于活动状态。

Image for post

现在,您可以在此虚拟环境中安装与项目相关的依赖项。 (Now you can install dependencies related to the project in this virtual environment.)

Good Luck!

祝好运!

参考: (Reference:)

  1. https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments

    https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments

  2. https://realpython.com/lessons/creating-virtual-environment/

    https://realpython.com/lessons/creating-virtual-environment/

翻译自: https://medium.com/@sargupta93/creating-a-virtual-environment-with-python3-1c9d9c4a1856

创建python3虚拟环境

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值