Virtual Environments are helpful in keeping all the dependencies (packages), required for a Python project, in one place. So you can avoid issues with conflicting dependencies when working on different projects simultaneously. For instance, one major use case is when you want to run different Python versions like 2.7, 3.5, and 3.6 on the same system.
You can use the following steps when setting up any Python project (including web development project using frameworks like Django, Flask etc.)
1. Make a top-level Directory
Create a directory named “virtualenvs” (a parent directory to keep all your virtual environments). All your Python projects will live inside this directory.mkdir virtualenvs
2. Create the Python Virtual Environment
cd virtualenvs
python3 -m venv env
You may use any name for the directory, other than “env”. If you’re curious about what goes inside the auto-generated “env” directory, you can check here:cd env
cat pyvenv.cfg
python —version
pip —version
3. Activate the virtual env
On Mac: source bin/activate
On Linux: ./bin/activate
On Windows: .\Scripts\activate
Important: You need to activate the virtual environment every time you want to build or run the code. You may edit the code files without activating the venv.
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 a. Install packages/dependencies using pip
If you’ve a requirements.txt file:pip install -r requirements.txt
or install the packages individually, for example for Django:pip install django
orpip install django==2.1
# For a particular version
5 b. You can also install packages from Source Code
For example download Django pre-release code from their git repo, and unzip it into a directory named django
, then:
pip install -e django
ORpip 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
You’re set to start working on the project now.
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 if you’ve initialised the git repo.)
7. Optimizing your daily workflow
I made this small shell script to automate the task of entering and activating the venv every-time I start:
#!/bin/bash cd virtualenvs/env/ source bin/activate # Configure any other environment variables here - # for example, private API keys that you don't want to check in to git. cd your_proj_name/
Paste the above code in a file name start_your_project_name.sh. From now on, to get started, you just have to do this:
source start_your_project_name.sh or ./start_your_project_name.sh
Leave a Reply