Setting up a Python Web-Development Project with Virtual Environment

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,, Sanic and Tornado.

1. Make a top-level Directory

I usually keep all my “Virtual Environments” in a directory named “virtualenvs”

mkdir virtualenvs
cd virtualenvs

2. Create the python Virtual Environment

Inside the virtualenvs directory:
python3 -m venv env
cd 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:

cat pyvenv.cfg
python —version
pip —version

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

On Mac: source bin/activate
On Linux: ./bin/activate
On Windows: .\Scripts\activate

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.
Use deactivate to leave the virtual environment or simply close the session/Terminal.

4. Create the project directory

mkdir your_proj_name

Optionally, Initialise git inside your_proj_name directory (you don’t need to commit the other files of env directory into git.):

git init
OR if you have the project code already, git clone the code inside env:
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 django, then:

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 env/lib and env/bin directories.

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:
cd your_proj_name
python 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.

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:
or ./

Leave a Reply