Skip to content

Environment Setup: Django & Python

Vera edited this page Nov 15, 2018 · 7 revisions

Prerequisites

A modern operating system that supports Python 3.5+.

Installing Python on Windows

  1. Download Python 3.7.x for Windows, taking into account your version of Window's architecture (x86 or x64). You can identify the architecture by running control system and checking the System Type for either 32-bit or 64-bit Operating System.
  2. Run the installer, noting where the install path is. Disable the Add Python to PATH option if you have other versions of Python installed, otherwise this will make this version the default one.

Installing Python for OS X

  1. Directions for installing Python 3 via Homebrew

Installing Python for other *nix operating systems

  1. Use the package manager appropriate to your operating system to download and install the python3 package.

VSCode and Python

  1. You can select which version of Python to use for your project in VSCode by pressing F1, typing in select interpreter, then choosing a version of Python 3.5.x+ from the resulting menu.

Setting up pipenv

  1. Run pip install pipenv (Windows) or pip install --user pipenv (*nix and OS X) to install the pipenv package for python.
  2. In your app project directory (/path/to/project/backend/ in this case) run pipenv shell. This will generate the needed file structure and copy in the needed dependencies in order to have a virtual python environment for the folder you are in. Once it is done generating the virtual environment, it will put you inside the pipenv shell, identified by (backend-RaNDomLeTterS) /path/to/backend/$ (OS X/*nix) or (backend-RaNDomLeTterS) C:\path\to\backend\> (Windows).
  3. Run pipenv sync to have pipenv download all the packages in 'backend/Pipfile.lock' to your virtual environment.
  4. New packages can be added to the virtual environment (and your Pipfile's) by running pipenv install <package>. This is preferred over using pip install, which installs packages globally, and is not conducive to version management for various project.

Setting up Django

  1. Setup a local PostgreSQL environment
  2. Once you've verified you can connect to the local PostgreSQL RDBMS container with pgadmin, you will need to do a Django migration to populate the needed tables for the app. Run python manage.py migrate from the pipenv shell to kick this off.
  3. Once the migration is complete, you will need to set up an initial super user to log into the Django admin panel with. In the pipenv shell, run python manage.py createsuperuser. You will be prompted for user details.
  4. Copy the contents of backend/env.template to a file named .env in the same directory. Update the settings accordingly.
  5. From the pipenv shell, run python manage.py runserver to start up the development server. You can connect to it by browsing to localhost:8000
  6. That's it! The Django app is up and running on your machine, and you can do cursory testing against it as you update your code!

Notes

  1. When changes are made to the underlying database structure, such as new and/or changed table schema, you may need to perform operations on the database which are outside the scope of this document, up to and including dropping all the tables, performing a new Django migration, and recreating a new superuser. Development data is inherently fragile.