Python — setting up Django App
The following guide is on how to get started with Django for beginners.
pre-requisite
Make sure that you have python installed on your machine. Installation instructions for the latest python version can be found here.
The following instruction will show you how to get started with django using three different methods:
- using virtualenv
- using pipenv
- using poetry
Before we proceed…
you need to understand what a virtual environment is and why we need it.
When you install python packages/modules they are stored globally by default. Every project you create with python will therefore reference the same global directory to retrieve the packages. This is all good and all but, what if you want to have different packages for different projects? what if you want to create a private package directory for each project so that they don’t conflict? This is where the virtual environment comes in.
A virtual environment is private/local environment separate from the global environment. The virtual environment will manage all the packages/modules privately for your specific project.
1. using virtualenv
By default, python comes with virtualenv to manage the packages for different environment. There is no need for installing it.
open your terminal and navigate to where you want to create your Django project
cd <project-folder>Create a new virtual environment for the project. The virtual environment gives you an independent environment separate from the host operating systems environment. You will receive the message created virtual environment .... don't forget the period sign at the end
virtualenv -p python . | python3 -m venv <nameofyourvirtualenv>Activate the virtual environment we just created
source bin/activate | source <nameofyourvirtualenv>/bin/activateand install Django using the pip command. pip is a package manager for python.
pip install djangoCreate a new Django project. Again, don’t forget the period else an extra folder will be created for you.
django-admin startproject <name-of-your-project> .Finally, run your Django project
python manage.py runserver2. using pipenv
Pipenv is a combination of PIP — python installer for python and virtualenv, hence the name “pip-env”. It handles the creation of a new virtual environment and the installation of packages for you. Just like yarn or npm in javascript, pipenv will create a lockfile and a file to manage the dependencies/packages/modules.
Get Jamal Guyo’s stories in your inbox
Join Medium for free to get updates from this writer.
install pipenv — (on Linux)
sudo apt install pipenvnavigate to your project folder
cd <project-folder>Install Django using pipenv. Pipenv will create the virtual environment for you and also install Django.
pipenv install djangostart the created virtual environment
pipenv shellCreate your django project
django-admin startproject <name-of-your-project>.run your project
python manage.py runserver3. using poetry
poetry is a package manager for python. Poetry is to python as npm or yarn is to javascript.
install poetry on your machine using the poetry official docs here.
navigate to your project folder
cd <project-folder>Initialize poetry using the command below. This will create pyproject.toml a file that will have details about our project and its dependencies.
poetry initstart the created virtual environment
poetry shelland install Django. Poetry will also create the virtual environment and start it for you and installs Django
poetry add djangocreate your Django project
poetry run django-admin startproject <name-of-your-project>.Finally, run your project
python manage.py runserverNOTE: to deactivate the virtual environment irrespective of the method you’ve used to create your django project type the following:
deactivate





