1
00:00:05,550 --> 00:00:06,630
Welcome back everyone.

2
00:00:06,680 --> 00:00:09,470
In this lecture we're going to be discussing virtual environments.

3
00:00:09,480 --> 00:00:10,880
The reason we actually need them.

4
00:00:10,920 --> 00:00:17,120
And then how we can use Anaconda's built in a virtual environment manager to quickly create one.

5
00:00:17,130 --> 00:00:20,280
So Let's first discuss the motivation behind the virtual environment.

6
00:00:20,310 --> 00:00:25,020
Imagine that you've launched your web application and you're using some sort of external Python library

7
00:00:25,350 --> 00:00:30,170
that's known as a dependency because your dependence on that external Python library.

8
00:00:30,180 --> 00:00:34,800
Now the feature that library most likely is going to get updated to a newer version and often these

9
00:00:34,830 --> 00:00:38,070
updates they have new features that you want to use in your web site.

10
00:00:38,190 --> 00:00:43,860
But they also have what's known as breaking changes where their syntax has changed slightly and your

11
00:00:43,860 --> 00:00:45,870
old code won't work anymore.

12
00:00:45,900 --> 00:00:50,490
So now you have a problem where you want to use the new features but in order to use those new features

13
00:00:50,520 --> 00:00:52,620
you're going to have to edit all your code.

14
00:00:52,620 --> 00:00:54,390
So what can I do in that aspect.

15
00:00:55,150 --> 00:00:59,330
Well what you do is you use a virtual environment to help manage these dependencies.

16
00:00:59,380 --> 00:01:04,090
And that way each version of your project can have its own distinct environment or each of your projects

17
00:01:04,090 --> 00:01:05,830
can have their own distinct environment.

18
00:01:06,040 --> 00:01:11,500
Depends on how you want to organize your workflow but the basic idea here is that we can have one environment

19
00:01:11,590 --> 00:01:16,780
with certain versions of these dependencies and then create a new environment with the newer updated

20
00:01:16,780 --> 00:01:21,930
versions of those dependencies and kind of mix and match to whichever environments we want.

21
00:01:21,940 --> 00:01:24,690
And that way we don't need to worry about breaking changes.

22
00:01:24,690 --> 00:01:29,410
We can just switch out the virtual environments that we're working with now Anaconda.

23
00:01:29,410 --> 00:01:33,970
It comes with a built in environment manager and we need to run some steps at our command line in order

24
00:01:33,970 --> 00:01:37,780
to create the environment to create an environment.

25
00:01:37,930 --> 00:01:43,210
Once you've installed Anaconda at your command line all you need to do is type Konda create dash dash

26
00:01:43,210 --> 00:01:46,080
name my EMV or whatever you want to call your environment.

27
00:01:46,300 --> 00:01:47,920
And then we'll ask you yes or no.

28
00:01:47,970 --> 00:01:49,600
You can go ahead and press y to continue.

29
00:01:49,600 --> 00:01:52,120
So let me quickly explain what's happening here.

30
00:01:52,860 --> 00:01:57,150
First off we have that Khanda that's basically a keyword telling you that you're going to be calling

31
00:01:57,150 --> 00:02:02,250
Konda from Anaconda which allows you to create and do things in virtual environments.

32
00:02:02,250 --> 00:02:05,300
And once you click on the second word They're just saying create.

33
00:02:05,310 --> 00:02:10,440
So create a new environment then you have this what's known as a flag name and that's basically saying

34
00:02:10,530 --> 00:02:16,220
the next thing that's going to come right after this name flag is the actual name of the environment.

35
00:02:16,230 --> 00:02:19,520
So here the name of the environment that we're choosing is my ESV.

36
00:02:19,770 --> 00:02:21,840
So you can edit this last box here.

37
00:02:21,840 --> 00:02:22,660
My EMV.

38
00:02:22,680 --> 00:02:26,790
To call your environment whatever you want you can call it my flask environment my first environment

39
00:02:27,600 --> 00:02:28,890
whatever you prefer.

40
00:02:30,250 --> 00:02:34,320
Now it's creating an environment with a specific package already included in that environment.

41
00:02:34,330 --> 00:02:36,440
We can do is say can create dash.

42
00:02:36,460 --> 00:02:41,440
And so it's a little shorter of a flag than dash dash name and then passing the name of your environment

43
00:02:41,620 --> 00:02:48,160
space and then passen the actual specific package you want included such as numb pie or whatever package

44
00:02:48,160 --> 00:02:52,870
you're interested in to create an environment with a specific version of Python where you can do is

45
00:02:52,870 --> 00:02:57,940
say Konda create an either or dash and or dash dash name and then the name of your environment.

46
00:02:57,940 --> 00:03:03,550
Space Python is equal to and then the version of Python you want and then kind of combine these ideas

47
00:03:03,550 --> 00:03:04,020
together.

48
00:03:04,060 --> 00:03:09,430
You can create an environment with a specific package version included you say Conda create and or dash

49
00:03:09,430 --> 00:03:11,910
dash name then the name of your environment.

50
00:03:11,940 --> 00:03:16,390
The name of your package equal to and then what version of that package you want and if you ever need

51
00:03:16,390 --> 00:03:21,660
a list of all your environments you can say the envy list OK.

52
00:03:21,830 --> 00:03:24,310
So let's quickly create our flask environment.

53
00:03:24,320 --> 00:03:30,170
I also want to point out that in the resources for this lecture there's a link to the environment documentation.

54
00:03:30,170 --> 00:03:33,190
The documentation is actually fantastic for this particular topic.

55
00:03:33,200 --> 00:03:37,070
We're also going to be showing you that in just a little bit but it has basically everything you would

56
00:03:37,070 --> 00:03:40,830
ever want to know about Conon vitamins how to manage the environments how to create environments list

57
00:03:40,830 --> 00:03:45,680
environments restore environments save environments that where you can create an environment and send

58
00:03:45,680 --> 00:03:47,530
that to your coworkers or colleagues.

59
00:03:47,570 --> 00:03:51,140
But we're going to show you that quickly walk through the documentation there and then we'll create

60
00:03:51,220 --> 00:03:54,170
our flask environment that we'll be using through this course.

61
00:03:54,170 --> 00:03:56,260
Let's get started.

62
00:03:56,260 --> 00:04:01,930
All right so very quickly I just wanted to show you the documentation page in condos actually also an

63
00:04:01,930 --> 00:04:05,830
open source package man image management system and environment management system.

64
00:04:05,950 --> 00:04:10,120
And it runs on Windows Mac Office or Linux and we're mainly going to be using the environment managed

65
00:04:10,120 --> 00:04:12,430
system that Konda provides for us.

66
00:04:12,430 --> 00:04:16,750
And if you want more information about the various tasks that you can do of Khanda when you're working

67
00:04:16,750 --> 00:04:22,000
a virtual environments click on user guide and then come down here where it says tasks.

68
00:04:22,000 --> 00:04:26,770
There's also a nice tutorial section but the task is probably what you can me referring to the most.

69
00:04:26,980 --> 00:04:30,160
If you want to manage your environments then click on managing environments.

70
00:04:30,280 --> 00:04:32,340
And here's basically all the things you would ever want to do.

71
00:04:32,350 --> 00:04:37,170
Create an environment of certain commands create an environment from a thought why M-L file which is

72
00:04:37,170 --> 00:04:40,450
a way you can send environments to other people.

73
00:04:40,450 --> 00:04:44,740
You can clone environments activate de-activate and we're going to be going through that in just a little

74
00:04:44,740 --> 00:04:49,750
bit but you can also view a list of your environments use Pipp and so on.

75
00:04:49,750 --> 00:04:53,810
So lots of things you can do here remove environments share environments and that will just be on there

76
00:04:53,830 --> 00:05:01,630
if we go back here just so I can show you go to under the user guide tasks and then managing environments

77
00:05:01,630 --> 00:05:02,280
right here.

78
00:05:02,390 --> 00:05:04,800
And that has a list of everything you can do.

79
00:05:04,810 --> 00:05:09,340
So let's now jump over to our Adam text editor and quickly create environment and you can actually do

80
00:05:09,340 --> 00:05:11,010
this at your terminal command line.

81
00:05:11,200 --> 00:05:13,750
I'll do it within the Adam text editor.

82
00:05:13,750 --> 00:05:15,500
All right so here I am at my command line.

83
00:05:15,520 --> 00:05:17,550
I'm just using the one that we've installed into Adam.

84
00:05:17,560 --> 00:05:21,870
But you can do this at your terminal or command line even outside of Adam.

85
00:05:21,940 --> 00:05:24,020
We're going to do is create a new environment.

86
00:05:24,320 --> 00:05:31,340
We will say condo space create an either that name or just dash and for short.

87
00:05:31,630 --> 00:05:34,900
Then type in the name of your environment and call us environment whatever you want.

88
00:05:34,930 --> 00:05:39,730
The only thing you can't do is if you already have an environment named this you can't repeat the names.

89
00:05:39,730 --> 00:05:41,350
So we'll say my flask.

90
00:05:41,430 --> 00:05:48,840
Ian the and then will say space flask because we want flasks to be installed along with this environment

91
00:05:49,320 --> 00:05:52,490
will hit enter and it will say solving environment.

92
00:05:52,900 --> 00:05:57,330
Eventually I'll ask if you want to install a bunch of things and notice that it's installing a lot more

93
00:05:57,390 --> 00:05:58,670
than just flasks.

94
00:05:58,680 --> 00:06:03,060
And the reason for that is because flask may have some dependencies itself that it's also going to be

95
00:06:03,150 --> 00:06:03,660
installing.

96
00:06:03,660 --> 00:06:05,370
So you notice here kind of near the top.

97
00:06:05,370 --> 00:06:06,660
It's the selling Jinja too.

98
00:06:06,800 --> 00:06:09,940
So it's something that flask needs so we'll go ahead and install this.

99
00:06:10,060 --> 00:06:10,620
Why.

100
00:06:10,710 --> 00:06:15,120
And then it's going to download and extract all these packages for you and the package is going to be

101
00:06:15,120 --> 00:06:17,520
a little different depending on your operating system.

102
00:06:17,520 --> 00:06:20,360
I will jump forward in time until this is finished installing.

103
00:06:20,580 --> 00:06:25,620
OK so we jumped forward in time and we finished installing everything and now we need to activate the

104
00:06:25,620 --> 00:06:27,600
environment in order to use it.

105
00:06:27,690 --> 00:06:32,490
So to activate this environment on a Windows computer all you need to do is like it says here say activate

106
00:06:32,670 --> 00:06:34,360
and in the name of your environment.

107
00:06:34,440 --> 00:06:39,960
Now if you're using bash and that basically means if you're on a Mac OS or a Linux computer you must

108
00:06:40,020 --> 00:06:41,750
use source activate.

109
00:06:42,030 --> 00:06:47,610
So if you're on a Mac OS or Linux computer you're going to type source activates and in the name of

110
00:06:47,610 --> 00:06:48,390
your environment.

111
00:06:48,390 --> 00:06:52,130
So my flask envy since I am on a Windows computer.

112
00:06:52,170 --> 00:06:53,720
I'm just going to type activate.

113
00:06:53,750 --> 00:07:00,810
So again source is only for people using bash which is typically Mac OS and Linux users so will select

114
00:07:00,810 --> 00:07:06,720
activate my flask in V and then once it's activated you should see over here on the left hand side that

115
00:07:06,870 --> 00:07:12,150
in parentheses it says my flask in V and that basically the notes that your environment is bent has

116
00:07:12,150 --> 00:07:13,250
been activated.

117
00:07:13,290 --> 00:07:20,640
Now notice here if I see the dot dot to go back up the directory my environment is still activated and

118
00:07:20,640 --> 00:07:25,830
that basically means that I can go around my computer find different python files in my environment

119
00:07:25,830 --> 00:07:26,950
will be activated.

120
00:07:26,970 --> 00:07:34,290
So once I call Python in any location of my computer this particular version of Python is whatever particular

121
00:07:34,290 --> 00:07:37,650
version is associated with my class envy.

122
00:07:37,680 --> 00:07:42,720
So that means if I try to import other libraries maybe I try to import some pie I'll enter.

123
00:07:42,730 --> 00:07:44,260
It's going to say module not found.

124
00:07:44,310 --> 00:07:51,470
That's because we haven't installed the numpty library in my flask environment but if I type out import

125
00:07:51,540 --> 00:07:57,000
flask it's going to have no problem because we specifically requested a flask to be installed upon creating

126
00:07:57,000 --> 00:07:57,810
that environment.

127
00:07:57,960 --> 00:08:03,900
So let's type here if you ever want to add more libraries this environment all you need to do is say

128
00:08:03,900 --> 00:08:06,840
Konda install and then the name of the library.

129
00:08:06,840 --> 00:08:12,130
So if you for whatever reason wanted no imply there you would just say can install non-pay.

130
00:08:12,150 --> 00:08:17,000
The only caveat being you'd have to make sure that your environment is activated when you installed

131
00:08:17,000 --> 00:08:17,820
this.

132
00:08:18,260 --> 00:08:21,670
OK so we won't need them Pine-Sol I won't install that.

133
00:08:21,900 --> 00:08:24,270
OK and finally to deactivate the environment.

134
00:08:24,300 --> 00:08:30,960
All we need to do is type the Activate enter and you'll notice now we no longer have the print C's indicating

135
00:08:30,960 --> 00:08:35,640
that my environment is activated and again if you're a Mac OS or Linux computers you're going to want

136
00:08:35,640 --> 00:08:42,520
to type source de-activate And then if you want to reactivate the environment it will say activate my

137
00:08:42,520 --> 00:08:46,880
flask NV enter and there we have our flask environment activated.

138
00:08:47,260 --> 00:08:47,560
All right.

139
00:08:47,590 --> 00:08:52,380
So working in virtual environments definitely a big part of web development especially if Python to

140
00:08:52,390 --> 00:08:55,390
make sure that you never have any dependency issues.

141
00:08:55,480 --> 00:09:01,300
So we're going to be working with this mask or my Flast environment later on in this course.

142
00:09:01,300 --> 00:09:03,070
Thanks everyone and I'll see at the next lecture.
