This page details the steps to set up a local, non-containerized dev environment for our backend Django server.
Using a local python venv is recommended for easier and faster access to server reloads and test modules, as well as direct access to Django management commands through terminal. Verify all changes on the docker container before pushing though.
Alternatively, our Docker images innately supports hot-reloading on code changes. Refer to the IDE - Pycharm section for more info.
- Python 3.11
- Git
- PostGIS
- Redis
- Pycharm or Visual Studio Code, optional IDE
- Docker, optional containerized db and caching
MacOS:
- Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Git
brew install git
- Python 3.11 and openssl
brew install [email protected] openssl
Linux:
- Git
sudo apt install git
- Python 3.11 and development packages
sudo apt-get install python3.11 python3.11-dev libssl-dev libffi-dev
Windows:
Install PostGIS and Redis in your local environment and edit .env with the right hosts and ports.
If Docker containers are preferred instead, comment out the 'django' service in docker-compose.yml, run docker-compose up
in the root folder, and connect to containers
mapped to the host machine via localhost:5432/localhost:6379.
This section details setup for PyCharm run configurations on local Python virtual environments.
For containerized environments, see documentation on setting up debugging with VS code with python containers.
Install PyCharm Community Edition for your OS.
Checkout the repo using git command line or PyCharm VCS options. Open the project with Pycharm at the root folder DriveBC.ca.
Set sources root: in the project panel, right click Drivebc.ca/src/backend -> Mark Directory as -> Sources root. This allows PyCharm to resolve imports in the project. Alternatively, go to Pycharm -> Settings -> Project: DrivebC.ca -> Project Structure:
Set up a virtual environment. Specifying the 'Dependencies' file should install all required packages in the newly created venv. Otherwise, once the venv is created, run pip install -r ./src/backend/requirements/development.txt
in the Terminal tab on the bottom-left.
Set up run configurations for various management commands. Use /.../DriveBC.ca/src/backend/manage.py
for Script path, and the command to be ran in Parameters i.e. runserver:
We can now run/debug the server with breakpoints using the toolbar on the top-right:
Useful management commands to add as run configs. Note that these commands can also be ran manually in console:
python manage.py runserver
runserver
- Starts server at localhost:8000
sandbox
- Runs custom management command under shared/management/commands/sandbox.py. Any other custom management commands can be added and ran the same way.
test apps --keepdb
- Runs all test modules. Use apps.app_name or a full module path to run tests specific to an app or module.
The --keepdb option uses an existing test database if it already exists and saves time, but we will need to remove this option when there is a new migration to be applied.
run_huey
- starts huey worker instance and run all registered tasks on start.
makemigrations
- creates db migrations, see Django documentation
migrate
- executes db migrations, see Django documentation
In the terminal tab on the bottom left:
- Switch to the backend folder
cd src/backend/
coverage run --source='.' manage.py test apps --keepdb && coverage report