Alexander Starbuck
Alexander Starbuck

Reputation: 1159

Django and 'virtualenv' - proper project structure

I have a dilemma setting up a local development projet structure. Here is my setup:

I made a "Mistake" of setting my project globally instead of in a virtual environment (using 'pip' to install everything in />). After reading this article I still don't get all the steps. Is this correct:

  1. I install global python ( pip, virtualenv in '/>' )
  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)
  3. activate this virtual environment and pip install django
  4. then from within newly created /virtual env/ folder I startprojectwhich creates another /project1/ folder within /virtual env/ folder
  5. with virtual environment still activated in the current shell session, I proceed with creating my scripts, site and app files

Ad 2. should the virtualenv folder be INSIDE the main "project1" folder or should it encompass it?

Ad 4. Is this correct or can I do it without activating virtual environment first?

My structure currently 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

Upvotes: 23

Views: 14202

Answers (2)

Nikita
Nikita

Reputation: 6331

Common solution is to keep virtual environments and projects in separate folders, e.g. have /users/myUser/.venvs for virtual environments and /users/myUser/documents/projects/ for projects. In other aspects you got it pretty much right yourself. So:

  1. You need to install global Python and virtualenv.
  2. Create directoriy for virtual environments, e.g. run mkdir /users/myUser/.venvs.
  3. Create the virtual environment for your project, virtualenv /users/myUser/.venvs/project1_venv.
  4. Activate the environment for your current shell session /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.
  6. Deactivate the environment, run 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. 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. The activation script modifies environment variables in a way, so that the interpreter and libraries from venv are used instead of global ones. (Though there are options to use global ones too.)

Upvotes: 26

Alasdair
Alasdair

Reputation: 308849

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 e.g. test your code on Django 1.8 and 1.9 before upgrading.

You might find virtualenvwrapper useful. 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.

Upvotes: 18

Related Questions