0:00
hello everyone today we're going to
0:02
learn about python virtual environment
0:04
specifically on a windows 10 machine
0:06
so what are we covering well first we're
0:08
going to take a step back and understand
0:10
what a virtual environment is and why it
0:11
matters then we're going to log into our
0:13
windows machine and create operate and
0:16
remove in a virtual environment
0:18
essentially we're going to cover all the
0:20
commands that you need to know as a new
0:22
programmer to manage and maintain your
0:26
so what is a virtual environment well
0:28
virtual environments enable you to
0:29
manage multiple programs with different
0:33
you know python includes this thing
0:34
called pip which allows you to install
0:37
and manage dependencies that are not
0:38
part of the standard library now a
0:40
dependency is any library or software
0:43
that our program depends on right that's
0:45
where it gets the word dependency
0:47
depends on that are not part of the
0:49
standard library and that really if it
0:51
was part of the standard library we
0:53
would need to download it because we
0:55
let's create you know give it make this
0:58
more concrete let's say we're creating a
1:00
project and we need to use the pandas
1:02
library the pandas library is one of my
1:04
favorite data science libraries
1:06
um but all we need to do to install
1:09
pandas is type pip space install
1:12
base pandas and that will install pandas
1:14
into our global environment
1:16
now that enables all of the programs
1:19
that we write to access this pandas
1:22
library so we can use those you know
1:23
pandas tools well that's you're saying
1:25
well that's great leo right well it
1:27
introduces a pretty immense problem
1:33
what happens if you have two projects
1:35
that use different versions of the same
1:37
software so let's say we download the
1:39
pip install pandas but this time we
1:41
specify a different version well that
1:43
might break our original project right
1:46
so the good news is you know virtual
1:48
environments solve this challenge you
1:50
know virtual environments or virtual m's
1:53
create an environment that's isolated or
1:55
encapsulated well encapsulated is just
1:58
the fancy programmer term for saying
2:00
it's a container right it's contained
2:02
and isolated so all we install is
2:05
only the exact software with the
2:08
appropriate version in each
2:10
for in each virtual environment for each
2:12
project so each project has its own
2:14
little bubble where it has everything
2:16
that it needs that's what a virtual
2:17
environment does and we can you know if
2:19
we have a hundred projects we'd have a
2:20
hundred different virtual environments
2:23
right and that's you know now and we
2:25
have confidence that all of our projects
2:26
will run with the appropriate
2:30
and the great news is as a python
2:31
version 3.3 there's nothing additional
2:34
we need to do because the virtual
2:35
environments are built into the standard
2:40
log into our windows machine here are
2:42
the commands that we're going to be
2:43
covering today feel free to take a
2:45
screenshot of this or just feel free to
2:48
you know follow along as i work through
2:50
the commands on the windows machine
2:57
okay now that we're logged into our
2:59
windows machine the first thing we're
3:00
going to want to do is check to see if
3:03
python is installed we can do this by
3:05
typing python-version
3:09
receive a version number not an error
3:11
python is indeed installed
3:13
so if you don't have python installed
3:16
feel free to watch my previous video
3:18
i'll link it on how to install python on
3:20
the windows 10 machine
3:22
so with python installed
3:24
to create a virtual environment we
3:26
simply type python-m for module v-e-n-v
3:30
for virtual environment and then we can
3:32
name our virtual environment anything
3:34
with that we want you could say leo's
3:37
that's a good name but a better name is
3:39
actually v-e-n-v so why is v-e-n-v a
3:42
better name well it's conventional for
3:44
virtual environments to be named the env
3:48
our project folder and then within our
3:50
project folder we have our virtual
3:57
and that's you know if we had 50 project
3:59
folders they'd all be named venv
4:01
okay we can type dir now when we see
4:04
that our virtual environment is indeed
4:07
in a folder within our project folder
4:09
now we're not using our virtual
4:11
environment yet the first thing that we
4:13
need to do is activate our virtual
4:15
environment we do that by typing v-e-n-v
4:20
backslash activate.bat
4:23
now when we hit enter we'll notice that
4:25
the venv is pre-pended to our path
4:28
this allows us or it's a visual
4:33
our virtual environment is active now we
4:35
can type all of our standard commands
4:37
like pip list to see what are the
4:39
packages that are in our virtual
4:41
environment so right now we just have
4:43
the scaffolding packages the packages
4:45
that come with every virtual environment
4:49
and now let's install our
4:54
data science library we discussed
4:55
previously panda so pip install pandas
4:58
and this will install pandas numpy and
5:00
everything you know that pandas you know
5:02
all the dependencies for pandas
5:05
so that way we can use pandas
5:07
you know for any of our prod
5:14
and you'll notice when i type dir
5:17
there's nothing different right
5:19
about within everything is contained
5:21
within the venv folder now that's
5:24
something i want to bring up so virtual
5:25
environments are made to be disposable
5:28
right you should never put any of your
5:30
project files into a virtual environment
5:34
because we don't save virtual
5:36
environments to our repository right so
5:40
obviously you know whenever we're coding
5:42
we're saving our files to you know
5:44
github or git lab or you know any
5:46
version control of your choice
5:49
we don't want to commit this stuff to
5:51
version control because they can get
5:53
pretty big i mean you know we can
5:54
install hundreds if not thousands of
5:56
packages over time there's no reason to
5:58
you know save you know that information
6:01
all we really need to know
6:03
are the packages or the dependencies and
6:05
their version numbers right and with the
6:08
packages and version numbers we can
6:10
recreate the virtual environment so
6:11
that's exactly what we're going to do
6:17
and then we pipe that to requirements
6:24
requirements.text and you'll notice
6:28
now we have the exact versions
6:30
of the dependent and the dependencies
6:32
with their exact versions listed in this
6:34
requirement.txt file now this is the
6:36
file that we would save to our
6:40
repository and then if we you know
6:43
move to a different computer or one of
6:45
our colleagues needs to recreate this
6:48
environment they can do so from the
6:51
that file so let's do that actually do
6:53
that so we'll type deactivate
6:56
to deactivate our virtual environment
6:58
because if we delete the virtual
7:00
environment and then we try to run
7:04
we're going to be trying to run python
7:06
from a virtual environment that's
7:07
deleted we'll get a bunch of errors so
7:09
we'll type deactivate
7:13
okay we can see that now we have
7:15
our requirements text file
7:17
and our virtual environment we can
7:24
and then we'll denv for instance denv
7:32
dir so now our virtual environment no
7:35
longer exists now clear that screen to
7:37
make things a little bit easier to read
7:40
how do we recreate our virtual
7:41
environment simple we type
7:45
so we're using the virtual environment
7:46
module v-e-n-v we'll name it v-e-n-v
7:51
and now after the v-env is created we'll
7:54
have to activate it because if we don't
7:56
then we just we'd end up installing all
7:59
of you know we'll send up installing
8:01
pandas into our global environment which
8:02
is not what we want to do so we type the
8:05
env backslash scripts backslash activate
8:10
again now it lets us know that we're in
8:13
our virtual environment and not our
8:17
install dash r requirements dot text
8:22
and we'll hit enter and you'll notice
8:23
we're pulling the exact same packages
8:26
that were installed previously
8:29
so we've now successfully recreated our
8:37
and notice that all of our packages
8:42
uh indeed there now so that's it so this
8:44
is these are the the essential commands
8:47
that you need to know when you know
8:49
managing a virtual environment you know
8:51
how to create one you need to know how
8:53
to activate it need to know how to
8:56
export out the you know the dependencies
8:58
with their versions using pip freeze and
9:01
then you need to know how to deactivate
9:03
it and that's pretty much it there are
9:06
again 99 of the time is a new program
9:09
this is exactly what you need to need to
9:11
know so if you like this video please
9:13
give me a thumbs up and i wish you the
9:15
best of luck on your python programming
9:17
journey thanks everyone