Hi, in this video, you will learn how to
create and use a Python virtual environment.
So what is a Python virtual environment?
Well, it is just a copy of your Python installation.
So in other words, a copy of your Python interpreter.
So when you download Python from Python.org or
from Anaconda, you are downloading a Python interpreter.
Now we make a copy of it.
Why do we make a copy?
Well, because with Python, we may write different
programs, and for each program we might need
to install different packages, so different libraries.
And usually we install these libraries
in our main Python installation.
So in the interpreter that we got
from Python.org or Anaconda, that is also
referred to as a global interpreter.
But by making a copy for each program
that we built, then we install libraries only
for that copy of the interpreter.
The benefit of this is especially for web applications,
because when you create a web application, you will
deploy it later, in some online servers.
And when you make a copy of the interpreter and
then you install the libraries you need in that copy.
So in that virtual environment, then you have a
clear list of the libraries that you have installed.
So you can generate a list and then
you can give that list to the server
where you will deploy your web application.
So that server will get the list of
libraries from that list and install them.
Now, the benefit here is that if you had
your global Python installation, you'd have many, many libraries.
So that server would have to install all those
many libraries, which is not a good idea. So,
so why not having one
interpreter for each Python project?
So now that we are about to create
our Django app, we are going to create
a virtual environment first, on Visual Studio Code.
This is how you do it.
So first of all, open Visual Studio Code.
So this is the welcome page.
I haven't created any project yet, so I'm going to
go and create a project which is basically you just
open a folder, a directory, in Visual Studio Code.
So go to File and then go to Open Folder
or Open, depending on what operating system you are.
So this can either be Open or Open Folder.
Click there and then locate an empty
folder, which you should have created before.
So I created this empty
folder, django_blog_translator.
You see, it's completely empty.
So I'm going to press on Open,
[No Audio]
and, that will open that folder. Here
you see here, in my EXPLORER tree on Visual Studio Code.
Now that we have the folder.
So in this folder, we are going to
put all the files for our Django project,
including, the copy of the Python interpreter,
so the virtual environment. To create this
copy, this is how you do it.
So first of all, you need to open a
Terminal, and you have to do a check first.
So make sure what Python you are using.
So in my case, I use a
command python 3.9, and that opens Python.
So this is a global Python installed on my computer.
So I'm sure that this command is working fine.
In your case, it could be just
Python without 3.9 or Python 3.
It depends what you have been using.
So wherever you have been using, I'm
going to exit this interactive shell.
So this was just to try out the python command.
Now I know this is working, so
I can go and do python3.9.
So no spaces here.
Then I space the m flag, then venv then env.
So what is this?
Well, venv is a library that is installed, by default.
It's a standard library for Python.
It comes shipped with Python.
So I am using that library.
So when you use that command python 3.9 or python,
and then you use that flag, that means that you
are about to execute that library as Python scripts.
And then this end is the folder where, the
copy of the Python interpreter will be placed.
So press Enter and you see immediately
that env was created, this folder here.
And if I expand this folder, you'll see
there are several other folders in here.
And inside this bin here, you will
see that we have the Python executable.
So that is a copy of Python.
It can either be used as just
python or python3 or python3.9.
But I'll show you how to use that Python
instead of a global Python now, so don't do
anything with that folder, because what we want to
do now is we want to select a default
Python interpreter, for this particular Visual Studio Code project.
To do that, you want to go to the command palette.
You can do that with shortcuts
actually, on Mac, it would be Cmd+Shift+P.
Press the three of them at the same time.
On Windows, it would be CTRL+Shift+P.
So that will show this box.
And then you can type in there, select interpreter.
So this is what we want to
use Python: Select Interpreter, that command.
Click on there and you will see this list.
So, I have different Pythons in my system.
One of them is this virtual environment
here, that I have just installed.
And that is the one that I want to choose,
so press there.
And now you should see down here that
Python 3.9.6, in my case with this folder.
So env is the folder, venv
is the Virtual Environment library.
This now will be used as default Python.
What does that mean?
Well, that means that if you now type in Python here,
but first, close this Terminal, and open a new one.
So to have a refresh of the
Terminal, New Terminal, you'll see that,
now, before this tag here, that tells me to
write something here, I have this env, in parentheses.
That means that this Python
Virtual Environment is being used.
So now if I just type in python, Python
3.9.6, of that Virtual Environment is being used.
Let me exit.
I can also type python3.9 and the same will be used.
So any of these commands here,
python, python3 or python3.9.
[No Audio]
So make sure you have that env there.
So how does this work?
Because it looks like magic.
Well, it works like this.
Let me close this Terminal again and open a new one.
New Terminal. So you see that, here we have a command being executed
by Visual Studio Code, when you open a New Terminal.
So it's a source, which is a
command to activate the virtual environment.
So source and then the path to the activate file,
you see it's django_blog_translator, which is this folder.
/env/bin/acitvate so, /env/bin/acitvate.
This activate file is being executed,
and that is the command that sets the
environment, to this virtual environment.
That means also that you can use python both,
from here as an interactive Python shell, but also
if you want to write a file now.
[No Audio]
So let's say create a file.
So be careful here, I'm going to write hello.py.
I'm saying be careful because, what happens is that
this file was created inside my env folder.
So that is not a good thing.
You don't want to touch the env folder.
So I'm going to delete that file and recreate it.
[No Audio]
To avoid the file being inside the env file,
you want to first press Escape, and that will
take the focus out of that env folder.
And then you can go here and create the file.
[Author Typing]
Now you see that the file
is created in that root directory.
So DJANGO_BLOCK_TRANSLATOR, hello.py.
And if I say print Hello here, right?
And now I can execute this
file, using this green triangle here.
So the run button, press that, and the file
will be executed, using the virtual environment python.
So you see, see here, what happened on the background.
So again, to wrap it up, the virtual environment
was used when we opened the Python shell.
It was also used when we executed the Python file.
And it's also used if we install, third party packages.
So let's say pip install django, to install
Django, that Django will be installed for this
particular Python, not for our global Python.
And I can prove you that, by
going to env and then to lib.
[No Audio]
And you will see that, Django was just
added to the third party packages in here.
So Django, this is what we just installed.
[No Audio]
To get rid of this warning, you may want to execute this command as being suggested,
[No Audio]
python3.9 or python directly.
-m pip install --upgrade pip, to upgrade the
local, the virtual pip, so to say, right?
And now if I close this project
[No Audio]
and open it again,
then I can go back to my previous project, either by
using this recent section here, so clicking on that, or by
going to File, Open Recent, and locate their project there.
Or a third way would be again to go
to File, Open and go again to that folder.
So to the root directory.
So those are env and hello.py and press
Open, and the same project will open again.
But you have to close the current Terminal and
open a new one, in order to get the
virtual environment active, in the new Terminal.
You see, now we see this env here.
And of course, now you can also execute
that file again, and the virtual environment will
be used, to execute and get the output.
So that's how you can use
virtual environments in Visual Studio code, thanks.