Setting up an existing Django project on Windows
I created this document for helping team members who need to run a Django project but not necessarily work on the Python/Django stuff. This is also equally helpful for the developer on Linux who needs to get the project up and running on Windows.
This document is tested only with the following versions
- Windows 10
- Python 3.6+
Install git bash
Git bash is a shell which can run all git commands and act like a bash terminal in that it provides us with few familiar bash commands to put the developer at ease.
Download git for windows from here: https://git-scm.com/downloads.
Install the program with pre-selected settings. It is often better not to modify any selections in the install dialog unless you know what you are doing. Then, let git bash launch.
By default, the python
command runs extremely slow or never actually opens up the python interpreter on git bash. To fix this, we need to create an alias for the python
command.
Open or create the .bashrc
file. This will open the nano editor.
nano .bashrc
If .bashrc
already did not exist, it will be created. Add the following line to this file. Press Ctrl-X
to exit out of the file. It will ask to save the file. Type Y
for Yes and Press ENTER
to save and exit.
alias python='winpty python'
After coming out of nano, run source
. This will enable the alias for the remainder of your bash session. Note that this will also be enabled when you run git bash next time.
source .bashrc
Create an SSH Key
Generate a new SSH key pair as per the gitlab documentation. Follow steps 1 through 6. Step 5 will allow you to commit, push and pull your changes to the repository.
Clone the project
$ git clone https://git-url/myproject
You may be prompted to enter the passphrase you just created when you clone the project and for subsequent pushes and pulls.
This will create a git repository of your project inside the myproject
folder.
Change into the myproject
folder.
$ cd myproject
From here on, when I say project path, I mean the directory where you have cloned the Django project.
Install Python
This could be any current Python version. If you have a recommended version from your project, it is always better to use that.
Download and install python
- 64bit - https://www.python.org/ftp/python/3.6.3/python-3.6.3-amd64.exe
- 32bit - https://www.python.org/ftp/python/3.6.3/python-3.6.3.exe
Make sure you can run Python from a command window. If it doesn't work, create a PATH entry to the Python installation.
Install pip
To install Python libraries and Django dependencies, we need to install a package manager called pip
.
To do this, download the script get-pip.py
from this location https://bootstrap.pypa.io/get-pip.py
Open command window in the folder where get-pip.py is and run the following command. This will download and install pip
.
$ python get-pip.py
Make sure pip
is working. Run pip
in the command window and see some pip related output. You can also try pip -V
to see the version.
Create a virtual environment
We need to create a Python virtual environment for our project. A Python virtual environment (virtualenv) is a way to manage the dependencies and their specific versions in a Python project without creating trouble for other Python projects.
To create a virtual environment, run the following command in the project path.
$ python -m venv venv
We need to activate the virtual environment. Inside the project path, run the following command
$ source venv/Scripts/activate.bat
Doing this will change your prompt like this
(venv) $
OR
(venv)
$
Install dependencies
Run the following command in the project path to install dependencies.
(venv) $ pip install -r requirements/base.txt
Run the project
We are all set to run the project.
Now let us activate the environment variables. Instead of running a few commands to create environment variables each time, I create a file called xport
with environment variables.
This is just a normal file. It is created using the following command. On windows, you can run this command on git bash.
touch xport
Now, fill the file with the following lines.
set -a
MAILSERVER="https://MAILURL"
DJANGO_SETTINGS_MODULE="myproject.settings.local2"
set +a
Doing this enables variables inside this file to be exported. Run the following command in the project path.
$ source xport
Since it is a lot more trouble to get the actual database to be set up, I create another version of the settings file and configure sqlite as the database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Run the migrate command to create database structure
$ python manage.py migrate
Run the built-in webserver
$ python manage.py runserver
This will run the application on the port 8000 with the dummy sqlite database.
Missing things
There are a few things I have missed at the moment.
You may need
- some seed data provided depending on your project.
- assistance on configuring the static assets.
- a superuser created