How to Start a Django Project Using Python 3 and virtualenv on a Mac

This is a quick tutorial on how to create a new Django project on a Apple Mac using Python 3 as a programming language.

Prerequisites for this tutorial are: you have a Mac, you have Python 3 installed, you have virtualenv installed and you have pip installed. You have all this? Then you are ready to go.

First, navigate to the folder where you want to create you project. Then create a new virtual environment via the terminal using the following command. The parameter -p python3 specifies that we want to use Python 3 in this virtual environment.

virtualenv -p python3 virtural_environment_name

After that we need to activate the environment. Use the following command

source virtural_environment_name/bin/activate

In the terminal you should now see the name of you virtual environment in brackets like this (virtural_environment_name). This means you are now in your virtual environment. You can now go ahead and install the latest Django version.

pip install django

After Django is installed you need to create a new project. Use any name that you like

django-admin startproject project_name

The project is now created. Navigate into the newly created project folder.

cd project_name/

Django has the concept of projects and apps. For this reason you need to create an app now. As before, please choose any name that you like.

python manage.py startapp app_name

Before you start the application lets create a requirements.txt which contains all the installed packages (e.g. Django) and their versions.

pip freeze > requirements.txt

Now we are ready to go. You can start the Django application using the following command.

python manage.py runserver 0.0.0:8000

Use the browser of you preference and navigate to http://localhost:8000/ you will see the welcome screen of the Django project.

The folder structure should look like this in the end.

base_folder/                 
 ├──virtural_environment_name/
 |   ├── bin/
 |   ├── include/
 |   ├── lib/
 |   └── pip.selfcheck.json           
 ├── project_name/ 
 | ├── app_name/
 | | ├── migrations/
 | | ├── admin.py
 | | ├── apps.py
 | | ├── models.py
 | | ├── tests.py
 | | └── views.py
 | ├── project_name/
 | | ├── settings.py
 | | ├── urls.py
 | | └── wsgi.py 
 ├── db.sqllite3
 ├── manage.py
 └── requirements.txt

That’s it. You are now ready to develop your Django application.