I am very new to django. I just have a very basic question about project layout using virtualenv. When we create virtualenv and install all the dependencies-django etc, do I need to switch my directory to the virtualenv and then create a project there? Or do I need to create my project outside of virtualenv. I apologize if it is a very basic question.
No, the directory where you create the virtual environment is completely separate and is not where you would go and create your django project.
In fact, you would usually put all your virtual environments in a separate directory; for me I put them in $HOME/work/.envs
(note the .
, this makes the directory hidden by default), so then my workflow becomes:
$ virtualenv $HOME/work/.envs/new_env
$ source $HOME/work/.envs/new_env/bin/activate
(new_env)$ pip install django
(new_env)$ cd ~/projects
(new_env)/projects$ django-admin.py startproject atestproj
So you see, you don't actually do anything with the virtual environment directory; it is completely managed by virtualenv
and pip
.
The virtualenvwrapper
project makes this easier by managing your virtual environments in a central location.
Directory structure for use with virtualenv should be as follows:
|-- project_name
|-- django
|-- project_name
|-- virtualenv
|-- project_name
|-- bin
This of course is not the definitive answer to how your project directory structure should be laid out—it has however worked for me, and others I know, over the years.
I highly recommend "twoscoops's" django project directory structure and tutorial for beginners: https://github.com/twoscoops/django-twoscoops-project
I also recommend virtualenvwrapper, to make managing virtual environments easier: http://virtualenvwrapper.readthedocs.org/en/latest/
This goes to the very heart of how you use virtualenv: cd to the virtualenv directory, then activate it (or the other way around - it doesn't really matter). The usual way to do this on linux (or cygwin) is to source ./bin/activate/
from inside the virtualenv.
At that point, if you use pip
or python
they will be local to that virtualenv. You should only perform your installations, and run your stuff after activating the virtualenv.
So, to answer your question: switch and activate before you start to install or do anything. Do everything inside the virtualenv, with it activated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With