简体   繁体   English

虚拟环境:python -m venv VS echo layout python3

[英]Virtual Environments: python -m venv VS echo layout python3

I'm fairly new to python, but have built a few small projects. 我对python很新,但已经建立了一些小项目。 I have been taught, and have always used, the following commands to start a virtual environment: echo layout python3 > .envrc and then direnv allow . 我已经教过并且一直使用以下命令来启动虚拟环境: echo layout python3 > .envrc然后direnv allow

What are the differences or advantages to using python -m venv <virtualenv name> versus echo layout ? 使用python -m venv <virtualenv name>echo layout什么区别或优点?

Those two commands do entirely different things. 这两个命令完全不同。

venv

The python -m venv <env_name> command creates a virtual environment as a subdirectory full of files in your filesystem. python -m venv <env_name>命令将虚拟环境创建为文件系统中充满文件的子目录。 When it's done, a new virtual environment is sitting there ready for you to activate and use, but this command doesn't actually activate it yet. 完成后,会有一个新的虚拟环境准备好供您激活和使用,但此命令实际上尚未激活它。

Activating the virtual environment so you can use it is a separate step. 激活虚拟环境以便您可以使用它是一个单独的步骤。 The command to do this depends on which operating system and which shell you're using (see the "Command to activate virtual environment" table in the docs linked above). 执行此操作的命令取决于您正在使用的操作系统和shell(请参阅上面链接的文档中的“激活虚拟环境的命令”表)。

The activation command alters only your current command-line shell session. activation命令仅改变当前的命令行shell会话。 This is why you have to re-activate the virtual environment in every shell session you start. 这就是您必须在启动的每个shell会话中重新激活虚拟环境的原因。 This kind of annoyance is also what direnv exists to solve. 这种烦恼也是direnv要解决的问题。

direnv and .envrc direnv.envrc

First, about that echo command... 首先,关于那个echo命令......

In both MS-DOS and Unix / Linux (and presumably recent versions of Macintosh), echo layout python3 just emits a string "layout python3" . 在MS-DOS和Unix / Linux(以及可能是最新版本的Macintosh)中, echo layout python3只发出一个字符串"layout python3"

The > redirects the echo command's output to a file, in this case .envrc . >echo命令的输出重定向到一个文件,在本例中为.envrc The redirection creates the file if it doesn't already exist, and then replaces its contents (if any) with that string. 重定向创建文件(如果该文件尚不存在),然后用该字符串替换其内容(如果有)。 The end result is a file in your current working directory containing just: 最终结果是当前工作目录中的文件只包含:

layout python3

The .envrc file, and direnv allow .envrc文件和direnv allow

.envrc is a config file used by the direnv application. .envrcdirenv应用程序使用的配置文件。 Whenever you cd into a directory containing a .envrc file, direnv reads it and executes the direnv instructions found inside. 每当你cd到包含目录.envrc文件, direnv读取并执行direnv发现里面的说明。

direnv allow is a security feature. direnv allow是一项安全功能。 Since malicious .envrc files could be hidden almost anywhere (especially in world-writable directories like /var/tmp/ ), you could cd into a seemingly innocent directory and get a nasty surprise from someone else's .envrc land mine. 由于恶意.envrc文件可以在几乎任何地方隐藏(特别是在像世界可写目录/var/tmp/ ),你可以cd到一个看似无辜的目录,并从别人的得到一个讨厌的惊喜.envrc地雷。 The allow command specifically white-lists a directory's .envrc file, and apparently un-lists it if it discovers the .envrc file has changed since it was allow ed. allow命令专门用白名单列出了一个目录的.envrc文件,如果它发现.envrc文件因allow编辑而发生了变化,显然会将其取消列出。

Finally, back to direnv 最后,回到direnv

I don't use direnv , but layout <language> is a direnv command to adjust your environment for developing in language , in this case activating a Python 3 virtual environment. 我不使用direnv ,但layout <language>是一个direnv命令来调整您的语言开发环境,在这种情况下激活Python 3虚拟环境。 The docs hint that it's more "helpful" than just that, but they don't go into any detail. 文档暗示它比那更“有用”,但它们没有详细说明。 (Also, you could have written your own direnv function called python3 that does something completely different.) (另外,你可以自己direnv名为python3direnv函数来完成不同的工作。)

The goal of all that is to automatically enable your Python virtual environment as soon as you cd into its directory. 所有我们的目标是尽快自动启用你的Python的虚拟环境,你cd到其目录。 This eliminates one kind of human error, namely forgetting to enable the virtual environment. 这消除了一种人为错误,即忘记启用虚拟环境。 For details, see Richard North's " Practical direnv ", especially the " Automatic Python virtualenv switching section. 有关详细信息,请参阅Richard North的“ Practical direnv ”,尤其是“ Automatic Python virtualenv切换部分”。

(Dis-)Advantages and Opinions (Dis-)优点和意见

If that's the kind of mistake you've made frequently, and you trust that the direnv command will never fall prey to a malicious .envrc file (or otherwise "helpfully" mess up something you're working on), then it might be worth it to you. 如果这是你经常犯的那种错误, 并且你相信direnv命令永远不会成为恶意.envrc文件的牺牲品(或者“帮助”搞乱你正在处理的东西),那么它可能是值得的它给你。

The biggest down-side I see to direnv (aside from the security implications) is that it trains you to forget about a vital step in using Python virtual environments... namely, actually using the virtual environment. 我认为最重要的direnv (除了安全隐患)是它会让你忘记使用Python虚拟环境的一个重要步骤 ......即实际使用虚拟环境。 This goes double for any other "help" it silently provides without telling you. 对于它在没有告诉你的情况下默默提供的任何其他“帮助”,这是双倍的。 (The fact that I keep putting "help" in quotes should suggest what I think of utilities like this.) (我不断在引号中加上“帮助”的事实应该表明我对这样的实用程序的看法。)

If you ever find yourself working somewhere direnv isn't installed, the odds are good that you'll forget to activate your virtual environments, or forget whatever else direnv has been doing for you. 如果你发现自己在某个地方工作没有安装direnv ,那么你将忘记激活你的虚拟环境,或者忘记direnv为你做的其他事情的direnv很大。 And the odds are even better that you'll have forgotten how to do it. 你忘记怎么做的可能性更大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM