简体   繁体   English

Django和'virtualenv' - 适当的项目结构

[英]Django and 'virtualenv' - proper project structure

I have a dilemma setting up a local development projet structure. 我设置了一个本地开发项目结构的困境。 Here is my setup: 这是我的设置:

  • Python 2.7 Python 2.7
  • Django 1.9 Django 1.9
  • Mac OSX El Capitan 10.11 Mac OSX El Capitan 10.11
  • MySQL 5.7 MySQL 5.7

I made a "Mistake" of setting my project globally instead of in a virtual environment (using ' pip ' to install everything in /> ). 我犯了一个“错误”,即全局设置我的项目而不是虚拟环境(使用' pip '在/>安装所有内容)。 After reading this article I still don't get all the steps. 阅读本文后,我仍然没有完成所有步骤。 Is this correct: 这个对吗:

  1. I install global python ( pip, virtualenv in '/>' ) 我安装了全局python(pip,virtualenv在'/>'
  2. I then go to a location where my projects will reside, like /users/user/documents/projects/project1 and from within 'project1' I use 'virtualenv' to create a virtual environment for this project (this creates a /virtual env/ folder inside /project1/ folder) 然后我去了我的项目所在的位置,比如/users/user/documents/projects/project1 'project1'中,我使用'virtualenv'为这个项目创建一个虚拟环境(这会创建一个/virtual env/文件夹里面/project1/文件夹)
  3. activate this virtual environment and pip install django 激活此虚拟环境并点击pip install django
  4. then from within newly created /virtual env/ folder I startproject which creates another /project1/ folder within /virtual env/ folder 然后从新创建的内/virtual env/文件夹我startproject它创建另一个/project1/文件夹内/virtual env/文件夹
  5. with virtual environment still activated in the current shell session, I proceed with creating my scripts, site and app files 在当前shell会话中仍然激活虚拟环境的情况下,我继续创建脚本,站点和应用程序文件

Ad 2. should the virtualenv folder be INSIDE the main "project1" folder or should it encompass it? 广告2. virtualenv文件夹应该位于主“project1”文件夹中还是应该包含它?

Ad 4. Is this correct or can I do it without activating virtual environment first? 广告4.这是正确的还是我可以在不先激活虚拟环境的情况下执行此操作?

My structure currently looks like this (starts from the root: /users/myUser/documents/projects/ ): 我的结构目前看起来像这样(从root: /users/myUser/documents/projects/ ):

/project1/
    /website1/
        /static/
        /templates/
        __init.py__
        settings.py
        urls.py
        views.py
        wsgi.py

Common solution is to keep virtual environments and projects in separate folders, eg have /users/myUser/.venvs for virtual environments and /users/myUser/documents/projects/ for projects. 常见的解决方案是将虚拟环境和项目保存在单独的文件夹中,例如,对于虚拟环境具有/users/myUser/.venvs对于项目具有/users/myUser/documents/projects/ In other aspects you got it pretty much right yourself. 在其他方面,你自己得到了正确的答案。 So: 所以:

  1. You need to install global Python and virtualenv. 您需要安装全局Python和virtualenv。
  2. Create directoriy for virtual environments, eg run mkdir /users/myUser/.venvs . 为虚拟环境创建directoriy,例如运行mkdir /users/myUser/.venvs
  3. Create the virtual environment for your project, virtualenv /users/myUser/.venvs/project1_venv . 为项目创建虚拟环境virtualenv /users/myUser/.venvs/project1_venv
  4. Activate the environment for your current shell session /users/myUser/.venvs/project1_venv/bin/activate . 激活当前shell会话的环境/users/myUser/.venvs/project1_venv/bin/activate
  5. Install django and anything else in this environment pip install django , or better use requirements.txt file to keep track of all project dependencies. 在这个环境中安装django和其他任何东西pip install django ,或者更好地使用requirements.txt文件来跟踪所有项目依赖项。
  6. Deactivate the environment, run deactivate . 停用环境,运行deactivate

Now when you'll want to run your project using created virtual environment, in your console window run /users/myUser/.venvs/project1_venv/bin/activate and then python /users/myUser/documents/projects/project1/manage.py runserver . 现在,当您想要使用创建的虚拟环境运行项目时,在控制台窗口中运行/users/myUser/.venvs/project1_venv/bin/activate ,然后运行python /users/myUser/documents/projects/project1/manage.py runserver You can activate the venv from any directory, it's activated for current shell window and any python ... run in that window after activation will use this virtual environment. 你可以从任何目录激活venv,它被激活用于当前shell窗口和任何python ...激活后在该窗口中运行将使用此虚拟环境。 The activation script modifies environment variables in a way, so that the interpreter and libraries from venv are used instead of global ones. 激活脚本以某种方式修改环境变量,以便使用来自venv的解释器和库而不是全局的解释器和库。 (Though there are options to use global ones too.) (虽然也有选择使用全球的。)

It doesn't really matter where you store your virtual environment. 存储虚拟环境的位置并不重要。 Find a project structure that works for you. 找到适合您的项目结构。

I wouldn't put the virtual env inside the project, because you shouldn't check it into version control (although you could use an ignore). 我不会将虚拟环境置于项目中,因为您不应将其检入版本控制(尽管您可以使用忽略)。 Normally, you just need to check in your requirements file, so that you can recreate the environment. 通常,您只需要检入需求文件,以便重新创建环境。

I wouldn't put the project inside the virtual env, because virtual envs are disposable. 我不会把项目放在虚拟环境中,因为虚拟环境是一次性的。 You might want to destroy the virtual env without destroying the project. 您可能希望在不破坏项目的情况下销毁虚拟环境。 Also, you might want to run the same project under different virtual envs eg test your code on Django 1.8 and 1.9 before upgrading. 此外,您可能希望在不同的虚拟环境下运行相同的项目,例如在升级之前在Django 1.8和1.9上测试您的代码。

You might find virtualenvwrapper useful. 你可能会发现virtualenvwrapper很有用。 It has some tools that make it easy to create and switch between virtual environments. 它有一些工具可以轻松地在虚拟环境之间创建和切换。 It stores all of your virtual environments in one place, so you don't have to worry about where to put them. 它将您的所有虚拟环境存储在一个位置,因此您不必担心放置它们的位置。

Is this correct or can I do it without activating virtual environment first? 这是正确的还是我可以在不先激活虚拟环境的情况下完成?

You should activate the virtual environment and install django before you create / work on your project. 在创建/处理项目之前,应激活虚拟环境并安装django。

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

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