Django + git + virtualenvwrapper

How to start a Django project + virtualenvwrapper + Git

Django is a cool python web framework, but it’s even cooler if you take advantage of other complementary tools and get a good environment to start a new project. So this is how I combine virtualenvwrapper + Django + git in a successful way.

These are the steps:

1. Install pip

pip is a python package manager system, is highly recommended for installation, update and management of all python packages inside in a project, there are different ways to install it, it depends of your operating system, so you should find the right way for you.

2. Install virtualenvwrapper

virtualenvwrapper is a set of extensions related to virtualenv, so the big advantage are the wrappers for create and delete environments, it makes easier the workflow.

pip install virtualenvwrapper (easiest way to install it)

After the installation you just need add these three lines in your shell startup file (.bashrc, .profile, etc). I created ~/Code folder before to add the lines

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Code
source /usr/local/bin/virtualenvwrapper.sh

After adding those lines, you need to reload the startup file. (e.g source ~/.bashrc)

3. Create the virtual environment for the project

Now, we have virtualenvwrapper ready to use, this line makes a new project

mkproject project_name (replace project_name with the name of your project)

A new virtual environment and a new folder name_of_project will be created, the path of the project will be ~/Code/project_name and the shell will be redirect to that path, you can validate it, if you run pwd command you will see

~/Code/project_name (~ will be replaced with you home directory)

You can use workon, deactivate and other virtualenv commands from now, also you can use other virtualenvwrapper stuff to accomplish productive tasks, follow the virtualenvwrapper documentation for more info.

4. Clone existing repo with Git or create a new one.

Many current projects use Github, Bitbucket or other hosting services, so you can create a new repository on those services and clone it on your new project folder or you can create a new local one. Don’t forget create a .gitignore file.

5. Install Django and create the requirements.txt file

Use pip to install the latest Django package version or the version you want:

pip install django

Create the requirements.txt file

pip freeze > requirements.txt

This requirements.txt file is very useful if you want to replicate the development environment in other computers, you can install the requirements like this:

pip install -r requirements.txt

You can commit the changes

6. Create Django project

To create the django project you just need to use django-admin.py startproject and two parameters: the project name and the location, so inside the ~/Code/project_name you can type:

django-admin.py startproject project_name .

or

django-admin.py startproject project_name ~/Code/project_name

Notice in the first one I’m using a period after the project’s name, which is a way to indicate that I want a new project in my current directory.

 

You can commit the initial project files and that’s enough to start working in a new project with Django web framework. 🙂

 

Advertisements