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:
'/>'
)/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)pip install django
/virtual env/
folder I startproject
which creates another /project1/
folder within /virtual env/
folderAd 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
So the answer is yes. The virtualenv is not a requirement to set up a Django project.
The Django developer team itself recommends that you use Python virtual environments!
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:
mkdir /users/myUser/.venvs
.virtualenv /users/myUser/.venvs/project1_venv
./users/myUser/.venvs/project1_venv/bin/activate
.pip install django
, or better use requirements.txt
file to keep track of all project dependencies.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.)
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.
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