简体   繁体   English

pyenv或virtualenv对Django是必不可少的吗?

[英]Is pyenv or virtualenv essential for Django?

For the first time, I made an environment for Django with virtualbox and vagrant (CentOS 7). 第一次,我使用virtualbox和vagrant(CentOS 7)为Django创建了一个环境。

But every tutorial I saw says I need to use pyenv or virtualenv. 但是我看到的每个教程都说我需要使用pyenv或virtualenv。 I think they are used to make a virtual environment for Django. 我认为它们用于为Django创建虚拟环境。 But I don't know why I need to use pyenv or virtualenv. 但是我不知道为什么我需要使用pyenv或virtualenv。 (For example, Cakephp3 doesn't need packages like pyenv or virtualenv.) (例如,Cakephp3不需要pyenv或virtualenv之类的软件包。)

And I was using virtualbox and vagrant, which are already virtual environment, so I think I was making virtual environment inside another virtual environment. 而且我使用的是virtualbox和vagrant,它们已经是虚拟环境,所以我认为我正在另一个虚拟环境中创建虚拟环境。 I am not sure if this is meaningful. 我不确定这是否有意义。 Maybe pyenv and virtualenv are not necessary if I am using virtual environment like virtualbox or vmware? 如果我使用的是virtualbox或vmware等虚拟环境,也许不需要pyenv和virtualenv吗?

Are they essential for Django? 它们对Django是必不可少的吗? When I deploy Django in actual server, do I still need to use pyenv or virtualenv? 在实际服务器中部署Django时,是否仍然需要使用pyenv或virtualenv?

It is not essential but it is recommended to work in a virtual environment when you start working on Django projects. 不是必需的,但是建议您在开始处理Django项目时在虚拟环境中工作。

Importance of the virtual environment. 虚拟环境的重要性。

  • A virtual environment is a way for you to have multiple versions of python on your machine without them clashing with each other, each version can be considered as a development environment and you can have different versions of python libraries and modules all isolated from one another. 虚拟环境是您在计算机上拥有多个版本的python而不相互冲突的一种方式,每个版本都可以视为开发环境,并且您可以拥有彼此隔离的不同版本的python库和模块。

  • In a most simple way, a virtual environment provides you a development environment independent of the host operating system. 虚拟环境以最简单的方式为您提供独立于主机操作系统的开发环境。 You can install and use the necessary softwares in the /bin folder of the virtualenv, instead of using the software installed on the host machine. 您可以在virtualenv的/bin文件夹中安装和使用必要的软件,而不是使用主机上安装的软件。

  • Many a time different projects need different versions of the same package and keeping every projects in seperate virtual environements helps a lot. 很多时候,不同的项目需要同一软件包的不同版本,并且将每个项目保持在单独的虚拟环境中会很有帮助。

It is strongly recommended to set up separate virtualenv for each project. 强烈建议为每个项目设置单独的virtualenv。 Once you are used to it, it will seem fairly trivial and highly useful for development, removing a lot of future headaches. 一旦习惯了,它对于开发来说似乎是微不足道的,并且非常有用,从而消除了很多将来的麻烦。

No, it's not essential to use virtualenv for Django but it's recommended because it isolates the multiple versions of python or libraries you are using for your projects on your system. 不,在Django中使用virtualenv并不是必须的,但建议这样做,因为它可以隔离用于系统项目的python或库的多个版本。 If you are not using virtualenv then that library will be the part of your python home directory. 如果您不使用virtualenv,则该库将成为python主目录的一部分。

For Example: If you are using version 1 of some library for one project and later on in other projects you have to use or if it's the requirement to use version 2 of that library then using specific virtualenv (if you are working on multiple projects simultaneously) for that project enables you to use multiple libraries without any problem. 例如:如果您将某个库的版本1用于一个项目,后来又在其他项目中使用,或者需要使用该库的版本2,则使用特定的virtualenv(如果您同时在多个项目上工作) ),从而使您可以毫无问题地使用多个库。

VirtualBox isolates your development operating system from your normal operating system. VirtualBox可将您的开发操作系统与常规操作系统隔离。 Virtualenv isolates your project's Python packages from the system Python packages. Virtualenv将项目的Python软件包与系统Python软件包隔离。

Many Linux distributions install Python packages as dependencies for other software into the system-wide site-packages directory. 许多Linux发行版将Python软件包作为其他软件的依赖项安装在系统范围的site-packages目录中。 Python doesn't have a way to version packages with the same name, so you will run into problems when your project depends on package==10.0.0 but your distribution already installed package==0.0.2 that you cannot upgrade without breaking something. Python没有办法用相同的名称来版本化软件包,因此当您的项目依赖于package==10.0.0但您的发行版已安装package==0.0.2时,您将遇到问题,您将无法升级而不会破坏某些内容。

Virtual environments are extremely lightweight. 虚拟环境非常轻巧。 They're really just a new entry in your PATH environment variable and some configuration changes to make Python look only in a specific place for packages. 它们实际上只是PATH环境变量中的一个新条目,并且进行了一些配置更改,以使Python仅在特定位置显示软件包。

There's no real downside to using a virtualenv beyond typing one extra command to activate it. 除了键入一个额外的命令来激活它之外,使用virtualenv并没有真正的弊端。

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

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