Django and "virtualenv" - the right project structure

I have a dilemma setting the local structure of project projects. Here is my setup:

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

I made a "mistake" to install my project globally instead of a virtual environment (using " pip " to install everything in /> ). Having read in this article , I still do not get all the steps. Is it correct:

  • I install global python (pip, virtualenv in '/>' )
  • Then I go to the place where my projects will live, for example /users/user/documents/projects/project1 and from 'project1' I use virtualenv to create a virtual environment for this project (this creates /virtual env/ inside the /project1/ folder )
  • activate this virtual environment and pip install django
  • then from the newly created folder /virtual env/ I startproject , which creates another folder /project1/ in the folder /virtual env/
  • with the virtual environment still activated in the current shell session, I continue to create my scripts, site files, and applications.

Ad 2. if the virtualenv folder is INSIDE the main folder of Project1 or should it cover it?

Announcement 4. Is this correct or can I do this without first activating the virtual environment?

Now my structure looks like this (starts from the root: /users/myUser/documents/projects/ ):

 /project1/ /website1/ /static/ /templates/ __init.py__ settings.py urls.py views.py wsgi.py 
+7
python django virtualenv
source share
2 answers

No matter where you store your virtual environment. Find a project structure that works for you.

I would not put virtual env inside the project, because you should not check it for version control (although you can use ignore). Usually you just need to check your requirements file so that you can recreate the environment.

I would not put the project in virtual env, because virtual envs are one-time. You might want to destroy the virtual env without destroying the project. In addition, you can run the same project under different virtual envs, for example. Before upgrading, check your code on Django 1.8 and 1.9.

You can find virtualenvwrapper . It has some tools that make it easy to create and switch between virtual environments. It stores all your virtual environments in one place, so you don’t have to worry about where to place them.

Is this correct or can I do this without first activating the virtual environment?

Before creating / working on a project, you must activate the virtual environment and install django.

+8
source share

A common solution is to save virtual environments and projects in separate folders, for example. /users/myUser/.venvs for virtual environments and /users/myUser/documents/projects/ for projects. In other aspects, you yourself understood this. So:

  • You need to install global Python and virtualenv.
  • Create a directive for virtual environments, for example. run mkdir /users/myUser/.venvs .
  • Create a virtual environment for your project, virtualenv /users/myUser/.venvs/project1_venv .
  • Activate the environment for the current shell session /users/myUser/.venvs/project1_venv/bin/activate .
  • Install django and everything else in this pip install django environment or better use the requirements.txt file to track all project dependencies.
  • Deactivate the environment, run deactivate .

Now, when you want to start the project using the created virtual environment, in the console window run /users/myUser/.venvs/project1_venv/bin/activate , and then python /users/myUser/documents/projects/project1/manage.py runserver . You can activate venv from any directory that is activated for the current shell window, and any python ... running in this window after activation will use this virtual environment. The activation script modifies the environment variables so that the interpreter and libraries from venv are used instead of the global ones. (Although there are options to use global).

+7
source share

All Articles