|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This page describes the steps needed to get the Pacific Connect Community Database set up on your local machine for development. The |
| 4 | +approach presented here is the simplest approach, an alternative approach is briefly mentioned under [Alternative |
| 5 | +Approach: Pyenv](#alternative-approach-pyenv) at the end. |
| 6 | + |
| 7 | +It assumes that you have already cloned this repository to your own computer (as outlined in the `README.md` file) and |
| 8 | +that you have a recent version of Python installed on your local computer. |
| 9 | + |
| 10 | +Python installation instructions for several operating systems can be found at |
| 11 | +http://pacific-coding.commoncode.io/getting-started/py-install/ - although you should install version 3.10 if possible |
| 12 | + |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- A Git client (such as GitHub Desktop) |
| 17 | +- This repository _cloned_ or your local computer. |
| 18 | +- Python 3.8 or later installed |
| 19 | + |
| 20 | +## Setup Steps |
| 21 | + |
| 22 | +To carry out these steps, you will need to be using your computer's _command line_. If you are not familiar with the |
| 23 | +concept of the command line an introduction can be found at |
| 24 | +http://pacific-coding.commoncode.io/getting-started/terminal/ |
| 25 | + |
| 26 | +On the command line, you will need to be in the base directory of this repository on your computer - that is, the |
| 27 | +directory that has the `README.md` file in it. |
| 28 | + |
| 29 | +### 1. Create and activate a virtual environment |
| 30 | + |
| 31 | +A _virtual environment_ (or _virtualenv_) in Python keeps the different _libraries_ you install separate for different |
| 32 | +projects. That means that if you're working on two different projects, they can have different versions of the same |
| 33 | +library (e.g. Django) without affecting each other. |
| 34 | + |
| 35 | +Create the virtual environment for this project: |
| 36 | + |
| 37 | +If using Windows: |
| 38 | +```powershell |
| 39 | +python -m venv venv |
| 40 | +``` |
| 41 | + |
| 42 | +If using MacOS or Linux: |
| 43 | +```bash |
| 44 | +python3 -m venv venv |
| 45 | +``` |
| 46 | + |
| 47 | +Once you have created your virtual environment, you need to _activate_ it so that Python knows to use the libraries in |
| 48 | +your virtual environment. |
| 49 | + |
| 50 | +To activate your virtual environment: |
| 51 | + |
| 52 | +If using Windows: |
| 53 | +```powershell |
| 54 | +venv\Scripts\activate |
| 55 | +``` |
| 56 | + |
| 57 | +If using MacOS or Linux: |
| 58 | +``` |
| 59 | +source venv/bin/activate |
| 60 | +``` |
| 61 | + |
| 62 | +If your virtual environment has been activated, you should see the prefix `(venv)` before the prompt on the command |
| 63 | +line. |
| 64 | + |
| 65 | +**NOTE: You will need to activate your virtual environment every time you want to work on this project**. |
| 66 | + |
| 67 | +For more information about virtual environments, see: |
| 68 | +- [Why do we need to use virtualenvs](http://pacific-coding.commoncode.io/python-intro/virtualenvs/) |
| 69 | +- [Create a Virtual Environment](http://pacific-coding.commoncode.io/python-intro/create-a-venv/) |
| 70 | + |
| 71 | + |
| 72 | +### 2. Install the Python requirements |
| 73 | + |
| 74 | +This project has a `requirements.txt` file that lists all the Python libraries required to run the project. Once you |
| 75 | +have activated your virtual environment, you can install the python libraries with: |
| 76 | + |
| 77 | +```bash |
| 78 | +pip install -r requirements.txt |
| 79 | +``` |
| 80 | + |
| 81 | +This will install all requirements listed in the `requirements.txt` file, and as new requirements are added to the |
| 82 | +project they will be added to the `requirements.txt` file. |
| 83 | + |
| 84 | + |
| 85 | +### 3. Run migrations |
| 86 | + |
| 87 | +Migrations are the way that Django manages changes to the structure of the database over time. |
| 88 | + |
| 89 | +Before running migrations, you will need to change to the directory (`src`) that contains the code. Different projects |
| 90 | +have different conventions about the name of the directory where the code is kept, but `src` is a common name. |
| 91 | + |
| 92 | +```bash |
| 93 | +cd src |
| 94 | +``` |
| 95 | + |
| 96 | +To set up your database the first time, run: |
| 97 | + |
| 98 | +Windows: |
| 99 | +```bash |
| 100 | +python manage.py migrate |
| 101 | +``` |
| 102 | + |
| 103 | +MacOS or Linux: |
| 104 | +```bash |
| 105 | +python3 manage.py migrate |
| 106 | +``` |
| 107 | + |
| 108 | +The first time you run this, you will see a series of messages that look like: |
| 109 | + |
| 110 | +``` |
| 111 | +Operations to perform: |
| 112 | + Apply all migrations: admin, auth, contenttypes, sessions |
| 113 | +Running migrations: |
| 114 | + Applying contenttypes.0001_initial... OK |
| 115 | + Applying auth.0001_initial... OK |
| 116 | + Applying admin.0001_initial... OK |
| 117 | + Applying admin.0002_logentry_remove_auto_add... OK |
| 118 | + ... |
| 119 | +``` |
| 120 | + |
| 121 | +This indicates that the migrations have run and the database is set up. |
| 122 | + |
| 123 | +**NOTE: You will need to run migrations fairly regularly in order to keep your database structure up to date** |
| 124 | + |
| 125 | +A summary about database set-up in Django can be found at: |
| 126 | +- [Pacific Coding: Set up a |
| 127 | + Database](http://pacific-coding.commoncode.io/django/your-first-django-project/#set-up-a-database) |
| 128 | + |
| 129 | +More information in Migrations can be found at: |
| 130 | +- [Django's Migrations documentation](https://docs.djangoproject.com/en/4.0/topics/migrations/) |
| 131 | + |
| 132 | +More information on Models can be found at: |
| 133 | +- [MDN's Django Tutorial Part 3](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Models) |
| 134 | +- [Django's Models documentation](https://docs.djangoproject.com/en/4.0/topics/db/models/) |
| 135 | + |
| 136 | + |
| 137 | +### 4. Continue to _Running the Application_ |
| 138 | + |
| 139 | +Now that you have installed all the prerequisites and run migrations for the first time, you can move to |
| 140 | +[running-the-application]. |
| 141 | + |
| 142 | +## Alternative Approach: Pyenv |
| 143 | + |
| 144 | +This approach is only recommended if you need to run multiple different versions of Python (such as Python 3.8 _and_ |
| 145 | +Python 3.10) on the same computer. |
| 146 | + |
| 147 | +[Pyenv](https://github.com/pyenv/pyenv) is a tool that allows you to have multiple Python versions installed and switch |
| 148 | +between them. When combined with [Pyenv-virtualenv](https://github.com/pyenv/pyenv-virtualenv) it also provides the |
| 149 | +ability to easily switch between named virtual environments. |
| 150 | + |
| 151 | +To install, see the instructions at [Pyenv Installation](https://github.com/pyenv/pyenv#installation) and |
| 152 | +[Pyenv-virtualenv Installation](https://github.com/pyenv/pyenv-virtualenv#installation) |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +[running-the-application]: running-the-application.md |
0 commit comments