In this section of the course we're going to be creating our django
application before we can create our django app we need to create a Python
virtual environment so we can install all of the dependencies such as Python
and the Django rest framework I'm going to show you how to create a Python
virtual environment on our vagrant server
load up the terminal and change
to our project workspace courses and then our profiles REST API project
connect to the server by typing vagrant SSH and then change directory to the
forward slash vagrant directory
you create a Python virtual environment
using Python 3 with the Python venv command so if we do Python - m venv
and then you set the path where you want to create the virtual environment so the
virtual environment is a set of files which you work on and this is the
directory where it installs all of the dependencies when you install them with
the Python package manager
So we're going to create it in tilde (~) forward slash env
what this does is it creates a new file in our vagrant server home directory
called env and creates our Python environment there now the reason I do it
here is because I don't want this environment to synchronize with our
local machine so if you ever need to destroy and recreate the vagrant server
from scratch you can do that with a fresh Python virtual environment
This is why I specify this tilde because it will create the environment in the home
directory of our vagrant server as opposed to the vagrant folder which is
synchronized to our local machine
Ok so once you've typed that hit enter and this
will go ahead and create a new virtual environment for our
project
the way that virtual environments work is you need to
activate them and deactivate them so when you're activated on a virtual
environment all of the dependencies that you run in the Python application will
be pulled from the virtual environment instead of the base operating system the
way you activate a virtual environment is you type source and then the path to
the activate script within our environment so in our case this path
would be in the home directory or tilde /env/bin forward slash activate then
hit enter you know that you're working on a virtual environment because the
name of the virtual environment you're working on appears in brackets at the
prefix of the command line input
the way that you switch off a virtual
environment is you just type deactivate and then you can see that the name of
the virtual environment disappears from the prefix and you are now deactivated
from that virtual environment
I'm going to provide a cheat sheet in the
resources of this video which provides all of these commands because I know
that it can be a lot to remember when you're doing this for the first time
that's how you activate and deactivate a virtual environment with Python