WDTUTORIALS

Python 3 - How To Create Virtual Environments

Tutorial on how to create virtual environments with Venv and Virtualenvwrapper and install Django package.

With Virtual Environments you can manage projects and related packages separately without interfering with each other.

For example you might have a virtual environment for project A that uses Python 2.7 and Django 1.9, and another virtual environment for project B that uses Python 3.6 and Django 1.10. If you update project B to Django 1.11, that doesn’t affect the Django version in project A.

Using venv module

venv module creates virtual environments. It was introduced in Python 3.3.

Create a virtual environment and activate it:

cd ~/MyProjects
mkdir venvs
python3 -m venv venvs/myproject02
source venvs/myproject02/bin/activate

You don’t have to create virtual environments in any particular location. In this case I created a folder venvs in the ~/MyProjects folder. You can also put the virtual environment folder inside the project folder and use .gitignore to ignore it from the version control.

You should see (myproject02) in the terminal to indicate that the virtual environment is active.

Install Django:

pip install django

Because the virtual environment is now active, Django package will be installed in ~/MyProjects/venvs/myproject02/lib/python3.6/site-packages/django.

Create a Django project:

django-admin startproject myproject02

Now you should have this kind of folder structure:

MyProjects
├── myproject02
│   ├── manage.py
│   └── myproject02
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py

Run the server:

cd myproject02
./manage.py runserver

Access site in http://127.0.0.1:8000/

You can deactivate the virtual environment like this:

deactivate

Using Virtualenvwrapper

Install virtualenvwrapper:

pip install virtualenvwrapper

virtualenvwrapper makes it easier to manage virtual environments. It’s an extension to the virtualenv tool.

Edit your bash startup file (like .bashrc or .zshrc) and put these lines in it:

export WORKON_HOME=$HOME/.venvs
export PROJECT_HOME=$HOME/MyProjects
source /usr/local/bin/virtualenvwrapper.sh
Name Description
WORKON_HOME The directory for all virtual environments. ~/.venvs in this case.
PROJECT_HOME The directory where all projects will be stored. ~/MyProjects in this case.
source .. virtualenvwrapper.sh Runs the script installed by the virtualenvwrapper project.

source the startup file for these to take effect:

source ~/.bashrc

source reads and executes commands from the given file argument: Gnu Bourne Shell Builtins

Run mkvirtualenv:

You can use this command to identify python3 location: which python3

mkvirtualenv --python=/usr/local/bin/python3 myproject03

This creates a virtual environment in ~/.venvs/myproject03 and activates it.

You should see (myproject03) in the bash to signal that this particular virtual environment is activated.

Install Django:

pip install django

Create a Django project:

cd ~/MyProjects
django-admin startproject myproject03
cd myproject03

Now you should have this kind of folder structure:

MyProjects
├── myproject03
│   ├── manage.py
│   └── myproject03
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py

Run development server:

./manage.py runserver

Access site in http://127.0.0.1:8000/

Virtualenvwrapper instructions

After you have added the lines above to your startup script (like .bashrc), you can use these commands to add new virtual environments and projects:

mkvirtualenv --python=/usr/local/bin/python3 myproject04
cd ~/MyProjects
django-admin startproject myproject04

Use deactivate to deactivate the virtual environment:

deactivate

Use workon to switch to a specific virtual environment:

workon myproject04

Samuli Natri (Entrepreneur, WDTutorials.com founder) has been building websites since the late 90's. He attended Helsinki University Of Technology (Computer Science) and Helsinki University (Social Sciences).

Latest Tutorial CKEditor Tutorial With CodeSnippet Syntax Highlighting

Copyright @ 2018 WDTutorials.com. All rights reserved. Privacy policy.Terms Of Service.