创建虚拟环境python_理解和创建python虚拟环境的最小指南

创建虚拟环境python

Most serious data science projects should take place in a Docker container or a virtual environment. Whether for testing or dependency management, it’s just good practice, and containerizing gives you greater power for debugging and understanding how things work together in a unified scope. This post is about creating a virtual environment in Python 3, and while the documentation is seemingly straightforward, there are few major points of clarity I’d like to point out, especially when using Jupyter notebooks and managing kernels and dependencies.

大多数严肃的数据科学项目应在Docker容器或虚拟环境中进行。 无论是用于测试还是依赖管理,这都是一个很好的实践,而容器化为您提供了更大的功能,可用于调试和了解如何在统一范围内协同工作。 这篇文章是关于在Python 3中创建虚拟环境的, 虽然文档看似简单 ,但我想指出的主要要点很清楚,尤其是在使用Jupyter笔记本以及管理内核和依赖项时。

什么是venv? (What is venv?)

The Python 3 module ‘venv’ comes with Python 3’s standard library. This means it’s built in when you download Python 3, and so you shouldn’t have to worry about installing it. Creating an actual virtual environment is quite simple:

Python 3模块“ venv”随附于Python 3的标准库。 这意味着它是在您下载Python 3时内置的,因此您不必担心安装它。 创建实际的虚拟环境非常简单:

python3 -m venv choose_your_venv_name

The first command is ‘python3’. This instructs the command line to run Python 3 scripts. Followed by ‘-m’, this option further instructs Python3 to look for a module name, in this case ‘venv’. The last argument is the name of your virtual environment, which should be relevant to the context of your intended project (e.g. ‘statsbox’ for a statistics-based virtual environment).

第一个命令是“ python3”。 这指示命令行运行Python 3脚本。 后面跟有“ -m”,此选项进一步指示Python3查找模块名称,在本例中为“ venv”。 最后一个参数是您的虚拟环境的名称,该名称应与您想要的项目的上下文相关(例如,基于统计的虚拟环境的“ statsbox”)。

Running this command will create a directory with your chosen name inside whatever directory you’re currently in, so be careful to cd your way to your intended directory before running it. After a few seconds, your virtual environment should be created. For example, I ran the following command inside my home directory:

运行此命令将创建里面你的任何目录目前在您所选择的名称的目录 ,所以要小心cd运行前用自己的方式与您想要的目录。 几秒钟后,应该创建您的虚拟环境。 例如,我在主目录中运行以下命令:

python3 -m venv tutorial_box

After a few seconds, I ran ls to check my home directory, and alas, ‘tutorial_box’ is there. If we cd into any virtual environment’s directory upon immediate creation, we find that it’s not empty!

几秒钟后,我跑了ls检查我的主目录,a,“ tutorial_box”在那里。 如果我们cd到后立即创建任何虚拟环境的目录中,我们发现,它不是空!

bin    include    lib    pyvenv.cfg

Great. These are actually your basic ‘blank slate’ files which are necessary in order to get your virtual environment up and running. Just because you created a virtual environment doesn’t mean you’re truly inside of it (activated) yet. This is an important conceptual distinction that I find can confuse intro learners. Even if you cd into the virtual environment directory (like above), your command-line commands, your Jupyter notebooks, and your packages will still be determined by your global environment’s PATH variable. That means that any Jupyter notebook you open will still have access to any packages or modules you’ve installed in your global environment, and if you’ve unwittingly produced a genius machine-learning model which is dependent on a package version which cannot be compiled in a different environment, then your code will break in that environment. This is common when, say, attempting to deploy a data science app with Heroku, Streamlit, Flask, Dash, etc.

大。 这些实际上是您的基本“空白”文件,这些文件对于启动和运行虚拟环境是必需的。 仅仅因为您创建了虚拟环境并不意味着您就已经真正地处于其中(已激活) 。 我发现这是一个重要的概念区别,可能会使入门学习者感到困惑。 即使你cd到虚拟环境目录(如上),你的命令行命令,你Jupyter笔记本电脑和你的包仍然会通过您的全球环境的PATH变量决定。 这意味着您打开的任何Jupyter笔记本仍然可以访问您在全局环境中安装的任何程序包或模块,并且如果您不经意间生成了一个天才的机器学习模型,该模型依赖于无法编译的程序包版本在不同的环境中,那么您的代码将在该环境中中断 。 例如,当尝试使用Heroku,Streamlit,Flask,Dash等部署数据科学应用程序时,这很常见。

It’s a good metaphor to think of a virtual environment (or Docker container) as a literal computer, a mini-computer inside your computer. In other words, just because you have the new mini-computer inside your computer doesn’t mean you’ve turn it on yet.

将虚拟环境(或Docker容器)视为文字计算机,即计算机内部的微型计算机,是一个很好的隐喻。 换句话说,仅仅因为您的计算机中装有新的微型计算机,并不意味着您已经打开了它

To do so, activate your virtual environment in the command line:

为此,请在命令行中激活您的虚拟环境:

source bin/activate

That’s it! To know it’s working, you should see the name of your virtual environment in the bottom left with parentheses, preceding your usual computer’s current directory path. In this case, it’s (tutorial_box)

而已! 要知道它正在工作,您应该在平时计算机当前目录路径之前的左下角用括号看到虚拟环境的名称。 在这种情况下,它是(tutorial_box)

Finally, we’ve created and activated our virtual environment. If you run Python 3 manually in the terminal, you’ll be operating in a fresh environment, and you’ll have to pip install any modules and packages you need for a new project from scratch. But remember, this is a good thing! It will save you from dependency hell down the line.

最后,我们创建并激活了我们的虚拟环境。 如果您在终端中手动运行Python 3,则将在全新的环境中运行,并且必须从头开始安装新项目所需的所有模块和软件包。 但是请记住,这是一件好事! 它将使您从依赖地狱中解脱出来。

…但是为什么我的Jupyter笔记本仍在使用我的全局软件包? (…but why are my Jupyter notebooks still using my global packages?)

This is the fundamental point of clarity I’d like to make in this tutorial.

这是我想在本教程中阐明的基本要点。

So, we’ve activated our virtual environment. Any Python 3 commands we run will be limited in scope to our fresh virtual environment. So what happens when we open a new Jupyter notebook?

因此,我们已经激活了虚拟环境。 我们运行的任何Python 3命令都将限于新的虚拟环境。 那么,当我们打开一个新的Jupyter笔记本时会发生什么?

jupyter notebook

Well, Jupyter should still open in the browser, but if we attempt to create a new notebook, we should see the following kernel option:

好了,Jupyter应该仍然在浏览器中打开,但是如果我们尝试创建一个新的笔记本,我们应该看到以下内核选项:

Image for post
In the bottom right, we have only one option
在右下角,我们只有一个选项

Under the ‘New’ notebook tab, we see ‘Python 3’. This is quite confusing: we have an option to create a new Jupyter notebook, but we have a potential list of kernels to choose from. In a way (somewhat different from virtual environments), these kernels are themselves virtual Python environments which will run independently of your virtual environment, even if it’s activated!

在“新建”笔记本选项卡下,我们看到“ Python 3”。 这非常令人困惑:我们可以选择创建一个新的Jupyter笔记本,但是我们有潜在的内核列表供您选择。 在某种程度上(与虚拟环境有些不同),这些内核本身就是虚拟Python环境,即使被激活,它们也将独立于您的虚拟环境运行!

The default Python 3 kernel which appears will actually be your global Python 3 environment all over again, which completely defeats the point of using a virtual environment, since we want to control exactly which packages and modules we’re using from a blank slate.

出现的默认Python 3内核实际上将再次成为您的全局Python 3环境,这完全使您无法使用虚拟环境,因为我们希望从空白状态精确控制要使用的软件包和模块。

In order to add a new kernel to your Jupyter environment which matches the virtual environment you’ve just created with venv, you need to run the following command inside of an already activated virtual environment:

为了向Jupyter环境添加新内核, 使其与您刚刚使用venv创建的虚拟环境匹配 ,您需要在已激活的虚拟环境中运行以下命令

pip install --user ipykernel

This will install the kernel-creator in your virtual environment, and you’ll always need to run it in a newly-created virtual environment since it’s a blank slate and will not have ipykernel installed. Then, in order to finally add the kernel to your Jupyter notebooks’ kernel option list, run the following:

这将在您的虚拟环境中安装内核创建器,并且始终需要在新创建的虚拟环境中运行它,因为它是空白板,并且不会安装ipykernel 。 然后,为了最终将内核添加到Jupyter笔记本的内核选项列表中,请运行以下命令:

python -m ipykernel install --user --name=whatever_you_want

There are some important points here. First, the whatever_you_want name can literally be whatever you want, and it doesn’t have to match the name of your virtual environment. All that matters is that the kernel you create with this command will contain all of the packages and modules inside of your currently-activated virtual environment. It, therefore, makes sense to give it a name that relates to your virtual environment, but wisely with a version number attached. Over time you may add more packages to your virtual environment, and then you’ll add that virtual environment state as a new modified kernel with ipykernel . Eventually, something will break, or some installed or updated package will give you a slightly different machine learning model, and you’ll want to revert to a previous Jupyter kernel. Appending your own version numbers to these kernels solves this problem, and is a virtual environment’s kind of localized version control.

这里有一些要点。 首先, whatever_you_want名称实际上可以是您想要的whatever_you_want名称,并且不必与您的虚拟环境的名称匹配。 重要的是,使用此命令创建的内核将包含当前激活的虚拟环境中的所有软件包和模块。 因此,给它一个与您的虚拟环境相关的名称是合理的,但明智地加上版本号 。 随着时间的推移,您可能会向虚拟环境中添加更多软件包,然后使用ipykernel将虚拟环境状态添加为新的修改内核。 最终,某些东西会中断,或者某些已安装或更新的软件包将为您提供稍微不同的机器学习模型,并且您将需要恢复到以前的Jupyter内核。 将您自己的版本号附加到这些内核可以解决此问题,并且是虚拟环境的一种本地化版本控制。

Alternatively, you can always create a requirements.txt file with the command:

或者,您始终可以使用以下命令创建requirements.txt文件:

pip freeze > requirements.txt

pip freeze > requirements.txt

That .txt file can then be installed elsewhere (everything inside of it, rather), such as a different virtual environment or Docker container.

然后,该.txt文件可以安装在其他位置(而是其中的所有内容),例如不同的虚拟环境或Docker容器。

Alternatively, I would recommend using pip-compile , which is a pip-tools package. It’s incredibly helpful for automatically generating a requirements.txt file from a manually-typed requirements.in file. It’s incredibly useful, because pip-compile automatically detects all of the required dependencies for packages listed in the requirements.in file.

另外, 我建议使用 pip-compile ,这是一个pip-tools软件包。 从手动键入的requirements.in文件中自动生成requirements.txt文件非常有用。 这是非常有用的,因为pip-compile自动检测中列举的软件包,所有必需的依赖requirements.in文件。

That covers it all. Using venv , ipykernel , and pip-compile are all you really need to create fresh virtual environments, and hopefully, this post has clarified how they are tied together with Jupyter notebooks, kernels, and dependency management.

涵盖了所有内容。 使用venvipykernelpip-compile都是创建新的虚拟环境所需的全部,希望这篇文章阐明了它们如何与Jupyter笔记本,内核和依赖项管理结合在一起。

Thanks for reading!

谢谢阅读!

翻译自: https://towardsdatascience.com/a-minimal-guide-to-understanding-and-creating-python-virtual-environments-38ba3eac1bc7

创建虚拟环境python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值