Environment Setup: Django & Python
Vera edited this page Nov 15, 2018
·
7 revisions
A modern operating system that supports Python 3.5+.
-
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. - 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.
- Use the package manager appropriate to your operating system to download and install the
python3
package.
- 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.
- Run
pip install pipenv
(Windows) orpip install --user pipenv
(*nix and OS X) to install the pipenv package for python. - 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). - Run
pipenv sync
to have pipenv download all the packages in 'backend/Pipfile.lock' to your virtual environment. - 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.
- Setup a local PostgreSQL environment
- 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. - 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. - Copy the contents of
backend/env.template
to a file named.env
in the same directory. Update the settings accordingly. - From the pipenv shell, run
python manage.py runserver
to start up the development server. You can connect to it by browsing tolocalhost:8000
- 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!
- 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.