- Virtualenv is a very handy tool for creating separate virtual environments for python projects.
- It is essential and probably mandatory in many cases to use Virtualenv or Virtualenv-like tools (i.e. Anaconda, Pipenv) to run a python project.
- In this tutorial, I'll show you how to create a virtualenv for a python project step-by-step.
NOTE: the commends in this tutorial is for Linux/Mac OS, Windows will be slightly different when setting up environment variables and system path
Setup virtualenv
1. Install pip
sudo apt-get install python-pip
2. Install virtualenv
sudo pip install virtualenv
3. Create a folder to install virtualenv
- Normally this folder would be created inside the project folder
- For example, inside the Projects folder
- It should be named as Envs
4. Install virtualenvwrapper
sudo pip install virtualenvwrapper
- This is a very useful tool/package to save you from the hassles of virtualenv
5. Set WORKON_HOME to your virtualenv
export WORKON_HOME=~/Desktop/Projects/Env
- If the virtualenv folder is located at the root directory, use:
export WORKON_HOME=~/.Env
6. Add virtualenvwrapper.sh
to .bashrc
(NOTE its .zshrc
on Mac)
- Navigate to the folder where the virtualenv is installed (normally under /usr/local/bin)
nano ~/.bashrc
(nano ~/.zshrc
on Mac)
- Add the following line to the folder:
source /usr/local/bin/virtualenvwrapper.sh
- If the virtualenvwrapper is installed in another directory, add the path of the directory instead of the
/usr/local/bin/
- For example,
. /Users/Carson/anaconda3/bin/virtualenvwrapper.sh
CTRL + X
to exit, and Y
to save, and hit Enter
- After its being added, use
source ~/.bashrc
(source ~/.zshrc
on Mac) to check if it is working. It should print some files on the screen with no errors
- NOTE if there is a path issue for virtualenv or python path (i.e. there are multiple versions of Python in system), use
which python
to find the path, and then add VIRTUALENVWRAPPER_PYTHON=<python path>
to the bashrc/zshrc
Setup virtual environment
1. make virtual environment
mkvirtualenv env_name --python=python3
- check python version:
python -V
2. activate environment
- the environment is activated by default when created
- to activate an environment:
workon env_name
3. get the project
- navigate to the directory that you would like to download the project
- get the project:
git clone
- From the directory you could tell which python version the project is under
4. get all the nesscary packages
- enter the project directory and nevigate to where the requirement file is located
pip install -r requirements.txt
5. adding apps for the defaul path in virtualenv
- sometimes when installing the packages errors may occor, since the apps are put inside the apps folder which is not recognized by virtualenv
- add the apps folder:
add2virtualenv apps
6. setup default startup folder for the environment
- navigate to the folder where you work all the time
- setup the folder as default:
setvirtualenvproject
- once the default folder is setup, use
cdproject
to go back from anywhere
7. install dev tools:
- If the project has a different requirements file for the devs, navigate to that folder
pip install -r /requirements/dev.txt
8. remove environment
9. tell django which setting files to run in environment:
- the goal of this step is for the django to use the customized settings in the
/project_name/settings/dev.py
- go to the folder where the
manage.py
locates
- copy the path generated:
pwd
- go to the virtual environment:
cdvirtualenv
cd bin
- to enter and edit the file:
nano postactivate
- put the following lines below the comments:
export DJANGO_SETTINGS_MODULE='project_name.settings.dev'
export PYTHONPATH=$PYTHONPATH:'path_generated_by_pwd'
- The first line allows you to use customized settings
- The second line allows you to
django-admin
commands
- close and save the file:
ctrl + x
, type y
and press enter
- to enter and edit the file:
nano postdeactivate
- put the following lines below the comments:
unset DJANGO_SETTINGS_MODULE
- deactivate and activate the environment for the settings to work
10. start django project
- if you have done step 9:
django-admin runserver
- if you haven't done step 9:
python manage.py runserver
11. check the website in browser
12. Force refresh the browser
- Sometimes when working with multiple projects and switching back and forth, the page display in browser can be scattered since there is cache built-in the browser
- to force refresh the browser:
ctrl + shift + r
13. Some useful virtualenv commands
lsvirtualenv
: List all of the environments.
cdsitepackages
: Navigate into the site-package
directory of the currently activated virtual environment
lssitepackages
: Shows contents of site-package
directory.