简体   繁体   English

首先创建Django应用程序或虚拟环境?

[英]Create the Django app or the virtual environment first?

I have been trying to successfully create projects using Django however I have seen projects where the user will create the project first THEN the virtual env. 我一直在尝试使用Django成功创建项目,但是我已经看到用户将首先创建项目的项目,然后是虚拟环境。 I have also seen instances where the user creates the virtual env and THEN the django app. 我还看到了用户创建虚拟环境的实例,然后是django应用程序。 Both sides argue that their method is better, but now I am confused. 双方都认为他们的方法更好,但现在我很困惑。 Pls help 请帮忙

It depends on your usage. 这取决于您的使用情况。 Let's say you have Django 2.1 installed globally, then you have a project where you need let's say Django 1.9, here you need to set-up your virtual environment first 假设您已经在全球安装了Django 2.1,那么您有一个项目,您需要让我们说Django 1.9,在这里您需要首先设置您的虚拟环境

it is better to create the virtual environment first and start working in that environment. 最好先创建虚拟环境并开始在该环境中工作。 ie use python from that environment. 即使用该环境中的python。

advantage: 优点:

a. 一种。 environment will contain all the package required by the project 环境将包含项目所需的所有包

b. can switch between multiple env( testing purpose) 可以在多个env之间切换(测试目的)

c. C。 easy to keep a record of the required packages 容易记录所需的包

d. d。 will not affect another project where u need python 3.5 and in django project u require python 3.6 不会影响你需要python 3.5的另一个项目,而在django项目中你需要python 3.6

disadvantage: need to keep track of each env in case if you have many virtual env ( all virtual env are store in same place just like anaconda one, else if store in project folder then no issue for1 env) 缺点:如果你有很多虚拟env需要跟踪每个env(所有虚拟env都像anaconda一样存储在同一个地方,否则如果存储在项目文件夹中则没有问题for1 env)

When you have a virtual environment you can track packages for each project. 拥有虚拟环境时,您可以跟踪每个项目的包。 When virtual environment is activated you can create requirements.txt file with command 激活虚拟环境后,您可以使用命令创建requirements.txt文件

pip freeze > requirements.txt

So when you want to run the django project to a different os you can install your packages from the requirements file you have created. 因此,当您想将django项目运行到其他操作系统时,您可以从您创建的需求文件中安装包。

pip install -r requirements.txt

An other scenario is when your os has django 1.11 and you have a django project created with that version. 另一种情况是你的操作系统有django 1.11并且你有一个用该版本创建的django项目。 When you upgrade the django version in your os the the django application will break. 当您在os中升级django版本时,django应用程序将会中断。

So i think that for each django project a good way is to have its own virtual environment 所以我认为对于每个django项目来说,一个好方法就是拥有自己的虚拟环境

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

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