应该在哪里创建virtualenvs?

问题:应该在哪里创建virtualenvs?

我对应该在哪里放置我的虚拟环境感到困惑。

在我的第一个django项目中,我使用以下命令创建了该项目

django-admin.py startproject djangoproject

然后,我进入djangoproject目录并运行命令

virtualenv env

在与内部djangoproject目录相同级别上创建了虚拟环境目录。

这是为特定项目创建virtualenv的错误位置吗?

我给人的印象是,大多数人将所有virtualenv放在一个完全不同的目录中,例如~/virtualenvs,然后使用virtualenvwrapper在它们之间来回切换。

有正确的方法吗?

I’m confused as to where I should put my virtualenvs.

With my first django project, I created the project with the command

django-admin.py startproject djangoproject

I then cd’d into the djangoproject directory and ran the command

virtualenv env

which created the virtual environment directory at the same level as the inner djangoproject directory.

Is this the wrong place in which to create the virtualenv for this particular project?

I’m getting the impression that most people keep all their virtualenvs together in an entirely different directory, e.g. ~/virtualenvs, and then use virtualenvwrapper to switch back and forth between them.

Is there a correct way to do this?


回答 0

许多人使用virtualenvwrapper工具,该工具将所有virtualenvs保留在同一位置(~/.virtualenvs目录),并允许在其中创建和保存它们的快捷方式。例如,您可以这样做:

mkvirtualenv djangoproject

然后再:

workon djangoproject

将virtualenv目录保留在项目本身中可能是一个坏主意,因为您不想分发它(它可能特定于您的计算机或操作系统)。而是使用pip保留一个requirements.txt文件:

pip freeze > requirements.txt

并分发。这将允许使用您的项目的其他人使用以下命令将所有相同的要求重新安装到他们的virtualenv中:

pip install -r requirements.txt

Many people use the virtualenvwrapper tool, which keeps all virtualenvs in the same place (the ~/.virtualenvs directory) and allows shortcuts for creating and keeping them there. For example, you might do:

mkvirtualenv djangoproject

and then later:

workon djangoproject

It’s probably a bad idea to keep the virtualenv directory in the project itself, since you don’t want to distribute it (it might be specific to your computer or operating system). Instead, keep a requirements.txt file using pip:

pip freeze > requirements.txt

and distribute that. This will allow others using your project to reinstall all the same requirements into their virtualenv with:

pip install -r requirements.txt

回答 1

更改virtualenv目录的位置会破坏它

这是把仓库树,例如目录以外的下一个优势~/.virtualenvsvirutalenvwrapper

否则,如果将其保留在项目树中,则移动项目位置将破坏virtualenv。

请参阅:重命名virtualenv文件夹而不破坏它

有,--relocatable但是众所周知它并不完美。

另一个次要优势:您不必这样做.gitignore

将其忽略到项目树本身的好处是:

  • 使相关的东西保持在一起。
  • 您可能永远不会在项目之间重用给定的virtualenv,因此将其放置在其他位置不会带来太多优势

Changing the location of the virtualenv directory breaks it

This is one advantage of putting the directory outside of the repository tree, e.g. under ~/.virtualenvs with virutalenvwrapper.

Otherwise, if you keep it in the project tree, moving the project location will break the virtualenv.

See: Renaming a virtualenv folder without breaking it

There is --relocatable but it is known to not be perfect.

Another minor advantage: you don’t have to .gitignore it.

The advantages of putting it gitignored in the project tree itself are:

  • keeps related stuff close together.
  • you will likely never reuse a given virtualenv across projects, so putting it somewhere else does not give much advantage

回答 2

通常放置它们的位置与virtualenvwrapper的默认安装放置它们的位置相同: ~/.virtualenvs

相关:virtualenvwrapper是一个出色的工具,可为常用的virtualenv命令提供简写。http://www.doughellmann.com/projects/virtualenvwrapper/

The generally accepted place to put them is the same place that the default installation of virtualenvwrapper puts them: ~/.virtualenvs

Related: virtualenvwrapper is an excellent tool that provides shorthands for the common virtualenv commands. http://www.doughellmann.com/projects/virtualenvwrapper/


回答 3

如果使用pyenv install Python,则pyenv-virtualenv将是最佳做法。如果设置了.python-version文件,则可以在更改工作文件夹时自动激活或停用虚拟环境。Pyenv-virtualenv还将所有虚拟环境放入$HOME/.pyenv/versions文件夹中。

If you use pyenv install Python, then pyenv-virtualenv will be a best practice. If set .python-version file, it can auto activate or deactivate virtual env when you change work folder. Pyenv-virtualenv also put all virtual env into $HOME/.pyenv/versions folder.


回答 4

根据我的个人经验,我建议将所有虚拟环境组织在一个目录中。除非有人具有非常敏锐的内存并且可以记住分散在整个文件系统中的文件/文件夹。不太喜欢仅使用其他工具来管理虚拟环境。在VSCode中,如果我的configure(python.venvPath)目录包含所有虚拟环境,则它可以自动识别所有虚拟环境。

From my personal experience, I would recommend to organize all virtual environments in one single directory. Unless someone has extremely sharp memory and can remember files/folders scattered across file system. Not a big fan of using other tools just to mange virtual environments. In VSCode if I configure(python.venvPath) directory containing all virtual environments, it can automatically recognize all of them.