This is how I start my Python Web Development projects. Actually these steps apply to Python projects in general.
You can use these steps when setting up web development project using frameworks like Django, Flask, bottle.py, Sanic and Tornado.
1. Make a top-level Directory
I usually keep all my “Virtual Environments” in a directory named “virtualenvs”
2. Create the python Virtual Environment
python3 -m venv env
(You may use any name for the diretory, other than “env”.)
You can check out some information about the virtual environment you created if you’re curious:
Virtual Environments help in keeping all the packages required for one project at one place. So there’s less issues with conflicting dependencies. Notice that you may even run different Python versions like 2.7, 3.5, 3.6 on the same system, inside different virtual envs.
3. Activate the virtual env
Important: Activate the virtual environment everytime you want to build/run the code inside a Virtual Environment. You may edit the code files without activating the venv though.
deactivate to leave the virtual environment or simply close the session/Terminal.
4. Create the project directory
Optionally, Initialise git inside
your_proj_name directory (you don’t need to commit the other files of
env directory into git.):
OR if you have the project code already,
git clone the code inside
git clone [url]
5. Install packages/dependencies
If you’ve a requirements.txt file:
pip install -r requirements.txt
or install the packages individually:
pip install django or
pip install django==2.1 # For a particular version
You can also install packages from source (say you don’t have internet connectivity on a system or you need a pre-release package). For example download Django into a directory named
pip install -e django
pip install --editable django
Note that you may run
pip from any of
env‘s subdirectories: the installed code will always go into the
6. Finish and Run
We’re set to start working on the project now.
For example, if you’re using Django you would typically start Django’s development server with:
python manage.py runserver
If you’re satisfied with the setup, you most probably want to keep a list of dependencies you just installed.
pip freeze > requirements.txt
Keep the requirements file in git.
7. Setup an efficient workflow
I made this small shell script to automate the task of entering and activating the venv.
#!/bin/bash cd virtualenvs/env/ source bin/activate # Configure any other environment variables here cd your_proj_name/
Paste the above code in a file name
From now on, to get started, you just have to do this: