1 00:00:00,000 --> 00:00:04,320 Python has these things called virtual environments, 2 00:00:04,330 --> 00:00:06,750 and it's really just a way for you to separate your code from 3 00:00:06,760 --> 00:00:09,150 other code, which at first sounds a little confusing and 4 00:00:09,150 --> 00:00:11,800 actually a little bit useless because we could just do that with folders. 5 00:00:11,860 --> 00:00:13,590 But there is a reason we want to do this. 6 00:00:13,660 --> 00:00:16,810 But actually, now that I think about it, there's a better 7 00:00:16,810 --> 00:00:18,500 way to think about virtual environments. 8 00:00:18,500 --> 00:00:22,400 It's like separating projects from other projects. 9 00:00:22,480 --> 00:00:26,050 So let's say you are working for a web development agency, 10 00:00:26,060 --> 00:00:27,370 and you have two projects. 11 00:00:27,380 --> 00:00:33,220 You have a website that is using Django version 1.11, and 12 00:00:33,220 --> 00:00:39,600 you have another website that's using Django version 3.0.5. 13 00:00:39,600 --> 00:00:43,500 That was supposed to be a 3, 3.0.5. 14 00:00:43,800 --> 00:00:46,600 Now, these are quite different. 15 00:00:46,660 --> 00:00:49,540 It's the same framework, but there are a lot of differences, 16 00:00:49,540 --> 00:00:53,100 and it's not just as easy as saying, "Hey, I want to use Django3 17 00:00:53,100 --> 00:00:56,200 instead of Django1", and all of a sudden everything just works. 18 00:00:56,230 --> 00:00:58,320 There are things you're going to have to change in your code. 19 00:00:58,420 --> 00:01:01,540 Now, if this was the case, every time you switched projects, 20 00:01:01,540 --> 00:01:06,400 you'd have to 'pip uninstall Django, and then you'd want to 21 00:01:06,400 --> 00:01:09,610 'pip install Django' with the exact version that you want, which 22 00:01:09,610 --> 00:01:14,200 is like 1.11; or if you were upgrading, 3.0. And 23 00:01:14,200 --> 00:01:16,700 every time you switch projects, you'd have to do that. 24 00:01:16,700 --> 00:01:20,850 Now, this is just one package, and this package has dependencies. 25 00:01:20,860 --> 00:01:26,050 If we do 'pip show Django', we can see it's required by 'wagtail', 26 00:01:26,060 --> 00:01:28,360 'treebeard', taggit', and 'django-debug-toolbar'. 27 00:01:28,370 --> 00:01:29,680 That's all I have on my computer 28 00:01:29,680 --> 00:01:33,200 anyways. There are a lot of other packages in the world that required Django. 29 00:01:33,260 --> 00:01:37,850 So now let's say on the project that's using Django version 30 00:01:37,850 --> 00:01:40,700 1, it's also using Wagtail CMS version 1. 31 00:01:40,780 --> 00:01:43,930 But on Django version 3, the other project, it's using 32 00:01:43,930 --> 00:01:45,700 Wagtail 2.9. 33 00:01:46,100 --> 00:01:50,600 Now, again, Wagtail 1, and Wagtail 2 have their differences. 34 00:01:50,600 --> 00:01:56,500 So that means you'd have to 'pip uninstall wagtail && pip install 35 00:01:56,540 --> 00:01:59,360 wagtail=2.9'. 36 00:01:59,600 --> 00:02:01,900 Now, okay, that's just two of these. 37 00:02:02,400 --> 00:02:06,000 But what happens if you're using an old version of 'requests'? 38 00:02:06,000 --> 00:02:07,300 'pip show requests'. 39 00:02:07,300 --> 00:02:08,900 This is a very popular package. 40 00:02:08,960 --> 00:02:12,610 What if you have an old package, or an old project that's 41 00:02:12,610 --> 00:02:16,600 using 'Requests' version 1, and it's not compatible with 2? 42 00:02:16,600 --> 00:02:22,800 Well, then again, 'pip uninstall requests && pip install 43 00:02:22,800 --> 00:02:26,500 requests=1.', whatever that version is going to be. 44 00:02:26,500 --> 00:02:28,900 [no audio] 45 00:02:28,900 --> 00:02:31,400 And you're going to have to do that every single time 46 00:02:31,400 --> 00:02:32,500 you change projects. 47 00:02:32,500 --> 00:02:35,170 Now, that's not just for web development agencies. 48 00:02:35,170 --> 00:02:38,100 That's also for any of your personal projects. 49 00:02:38,100 --> 00:02:42,220 And that sucks to work with. 50 00:02:42,230 --> 00:02:43,210 And there's a better way. 51 00:02:43,210 --> 00:02:47,100 There's a thing called a 'virtual env', or a virtual environment. 52 00:02:47,180 --> 00:02:50,090 With a virtual environment, you can have one project that 53 00:02:50,100 --> 00:02:53,700 uses Django 1.11. 54 00:02:53,720 --> 00:02:58,140 It could even, if you wanted it to, to use like Python 3.2. 55 00:02:58,860 --> 00:03:01,920 Your virtual environments can be completely different from 56 00:03:01,930 --> 00:03:03,060 other virtual environments. 57 00:03:03,420 --> 00:03:08,100 So you can have a newer website that's using Django 3.0, and 58 00:03:08,100 --> 00:03:10,100 also using Python 3.8. 59 00:03:10,140 --> 00:03:13,590 And what a virtual environment does, is it lets you keep all 60 00:03:13,600 --> 00:03:17,850 of your packages installed in one area where you can use 61 00:03:17,860 --> 00:03:20,520 it, and then you can keep all your packages for another project 62 00:03:20,520 --> 00:03:22,800 installed in another area where you can use those. 63 00:03:22,820 --> 00:03:26,690 So because on one computer we can't have Django installed 64 00:03:26,700 --> 00:03:28,800 twice, or any package installed twice 65 00:03:28,820 --> 00:03:31,550 we have these things called virtual environments, where we 66 00:03:31,560 --> 00:03:35,250 can have the package installed on one virtual environment, 67 00:03:35,260 --> 00:03:37,560 and then have that same package, but a different version 68 00:03:37,570 --> 00:03:39,540 installed in another virtual environment. 69 00:03:39,550 --> 00:03:42,540 And the problem that this solves is versioning. 70 00:03:42,550 --> 00:03:45,900 Now, if you're brand new to the idea of Python and virtual 71 00:03:45,900 --> 00:03:49,800 environments, this is a life saving feature. 72 00:03:49,860 --> 00:03:55,210 Now, there are two ways to use a virtual environment. 73 00:03:55,220 --> 00:03:58,270 There's actually several ways, but we're going to cover two 74 00:03:58,280 --> 00:04:02,620 ways. We've got one called a 'venv', and we have another one 75 00:04:03,000 --> 00:04:04,100 called a 'pipenv'. 76 00:04:04,500 --> 00:04:07,440 One is built-in with Python, and the other one is almost 77 00:04:07,450 --> 00:04:08,430 built-in with Python. 78 00:04:08,440 --> 00:04:11,250 So the one that's built-in with Python is the 'venv', and 79 00:04:11,260 --> 00:04:13,080 you activate it, or you create one 80 00:04:13,080 --> 00:04:17,300 rather. You create a 'venv', virtual environment with 'python -m 81 00:04:17,300 --> 00:04:20,779 venv' is the command, and then you give it a folder. 82 00:04:20,990 --> 00:04:25,350 So before I run that, let's run 'ls -la', and we can see the 83 00:04:25,350 --> 00:04:27,800 only files that I have in here that start with a '.' is 84 00:04:27,800 --> 00:04:32,000 '.DS_Store' because I'm on a Mac, and '.localized'. 85 00:04:32,060 --> 00:04:36,230 What I can do is type 'python -m venv', and then a folder name. 86 00:04:36,230 --> 00:04:39,300 So it could literally be 'anythingiwant', 87 00:04:39,300 --> 00:04:41,700 and it's going to create a folder with my virtual environment 88 00:04:41,700 --> 00:04:42,700 packages in there. 89 00:04:42,700 --> 00:04:49,000 I'm going to name this '.venv', and it's just going to take a second. 90 00:04:49,020 --> 00:04:51,580 And this is interesting. 91 00:04:51,590 --> 00:04:55,930 I have the Python extension installed on VS Code, and I actually 92 00:04:55,930 --> 00:04:57,900 noticed that there's a new virtual environment. 93 00:04:57,900 --> 00:04:59,600 So that's pretty cool. 94 00:04:59,600 --> 00:05:03,600 Not really related to this lesson, but still very interesting thing. 95 00:05:05,000 --> 00:05:08,700 So now if I do 'ls -la', we have a folder in here called 96 00:05:09,110 --> 00:05:11,930 '.venv', and I can 'cd' into that folder. 97 00:05:11,940 --> 00:05:15,260 I can 'ls -la', or if you're on Windows, it's 'dir'. It's 98 00:05:15,270 --> 00:05:21,950 'dir'. I've got a folder called 'lib', 'include', and 'bin', and I've 99 00:05:21,950 --> 00:05:26,000 got a file called 'pyvenv.cfg'. So I'm going to actually go up one directory. 100 00:05:26,030 --> 00:05:29,550 We don't really ever need to go into that directory, unless, 101 00:05:29,550 --> 00:05:32,100 of course, you need to edit some source code. 102 00:05:32,100 --> 00:05:36,300 Now, whenever you do 'pip install', some package name, 103 00:05:36,300 --> 00:05:39,600 the idea is to make sure that it's inside of this 'venv'. 104 00:05:39,600 --> 00:05:42,140 Right now it's on our entire computer. 105 00:05:42,150 --> 00:05:43,790 It's not inside this virtual environment. 106 00:05:43,920 --> 00:05:47,610 So we have this virtual environment installed in this folder 107 00:05:47,620 --> 00:05:52,560 called 'venv' or '.venv', and we need to get inside of this virtual 108 00:05:52,570 --> 00:05:57,500 environment, and you can think of this as an abstract 109 00:05:57,500 --> 00:05:59,900 layer away from your computer. 110 00:05:59,900 --> 00:06:06,500 So what I can do here is type 'source .venv', 111 00:06:06,500 --> 00:06:08,200 that's the folder name that I gave it. 112 00:06:08,200 --> 00:06:11,900 '/bin/activate'. 113 00:06:11,900 --> 00:06:14,100 Now, this is weird. 114 00:06:14,100 --> 00:06:16,400 This is probably one of the weirdest things you're going to see 115 00:06:16,400 --> 00:06:19,400 in Python, but I promise you'll get used to that one. 116 00:06:20,100 --> 00:06:23,000 And now you can see I'm actually inside of the virtual environment. 117 00:06:23,000 --> 00:06:24,100 It says '.venv'. 118 00:06:24,900 --> 00:06:26,300 And if I do, just for fun, 119 00:06:26,300 --> 00:06:27,700 'pip freeze'. 120 00:06:29,500 --> 00:06:30,700 There's nothing in there. 121 00:06:30,760 --> 00:06:32,430 There are no packages in here. 122 00:06:32,580 --> 00:06:36,150 And if I want to get outside of my virtual environment, 123 00:06:36,180 --> 00:06:38,980 I type the word 'deactivate', and it gets out of it. 124 00:06:38,980 --> 00:06:41,100 And if I do 'pip freeze', watch this, 125 00:06:41,100 --> 00:06:44,160 if I can spell it right, third time is the charm. 126 00:06:44,700 --> 00:06:47,600 Okay, so I did 'pip freeze'. I'm on my computer. 127 00:06:47,630 --> 00:06:49,630 I'm not inside of my virtual environment. 128 00:06:49,780 --> 00:06:52,600 These are all the packages that I have on my computer right 129 00:06:52,610 --> 00:06:59,150 now. But when I go back into my virtual environment, and type 130 00:06:59,150 --> 00:07:02,700 'pip freeze', nothing shows up. 131 00:07:02,710 --> 00:07:04,530 This is a brand new slate. 132 00:07:04,530 --> 00:07:06,100 There is nothing in here. 133 00:07:06,180 --> 00:07:09,300 And so just for fun, let's go ahead and do a thing. 134 00:07:09,600 --> 00:07:12,800 Let's 'pip install' a different version of Wagtail. 135 00:07:12,850 --> 00:07:15,510 'pip install wagtail', 136 00:07:15,510 --> 00:07:17,300 and let's do wagtail2.1. 137 00:07:17,300 --> 00:07:20,100 [no audio] 138 00:07:20,100 --> 00:07:23,300 So for some reason, I want to install an older version of 139 00:07:23,300 --> 00:07:26,500 Wagtail CMS, and it's going to go and install a bunch of 140 00:07:26,580 --> 00:07:28,430 stuff. It has dependencies in there. 141 00:07:28,430 --> 00:07:30,900 So it's going to try to install all sorts of things. 142 00:07:30,940 --> 00:07:34,300 We can see it's actually installing all sorts of things in 143 00:07:34,300 --> 00:07:38,900 here. And now let's run 'pip freeze' inside the virtual environment. 144 00:07:38,900 --> 00:07:44,100 Look at all these packages that I have now, and I have Wagtail2.1. 145 00:07:44,600 --> 00:07:49,400 So now let's 'deactivate' this, and do 'pip freeze | 146 00:07:50,180 --> 00:07:51,290 grep wagtail'. 147 00:07:51,300 --> 00:07:54,110 And all I want to do is get that list of all my packages 148 00:07:54,120 --> 00:07:56,630 that I have on my computer because I'm outside of my virtual 149 00:07:56,630 --> 00:07:59,700 environment, and I'm simply running a Unix command called 150 00:07:59,700 --> 00:08:02,030 'grep', and I'm looking for a keyword called 'wagtail'. 151 00:08:02,040 --> 00:08:04,880 And I'm going to show you that I'm not using Wagtail 2.1 152 00:08:04,880 --> 00:08:09,200 anymore. On my computer, I'm using Wagtail 2.7. 153 00:08:10,900 --> 00:08:14,800 And if I go back into my virtual environment, and run the 154 00:08:14,800 --> 00:08:18,900 exact same code, I'm using Wagtail 2.1. 155 00:08:18,940 --> 00:08:22,570 So on my computer, I have 2.7, which is outside of my virtual 156 00:08:22,570 --> 00:08:25,100 environments, but inside of this virtual environment, I have 157 00:08:25,100 --> 00:08:26,200 Wagtail 2.1. 158 00:08:26,200 --> 00:08:27,900 And so now I can use different versions. 159 00:08:27,900 --> 00:08:30,500 I'm not going to run into problems. I don't have to 160 00:08:30,540 --> 00:08:34,679 'pip uninstall wagtail 2.7 && pip install wagtail 2.1' every 161 00:08:34,679 --> 00:08:38,100 single time I switch a project, it just keeps it in there for me. 162 00:08:38,110 --> 00:08:39,960 Now, that is one method. 163 00:08:39,990 --> 00:08:42,960 And to deactivate, you just type 'deactivate'. That gets you 164 00:08:42,960 --> 00:08:46,000 back on your computer, and outside of your virtual environment. 165 00:08:46,059 --> 00:08:47,770 'ls -la' or 'dir' 166 00:08:47,780 --> 00:08:51,909 if you're on Windows. If you ever wanted to delete this virtual 167 00:08:51,919 --> 00:08:55,210 environment, this is literally just a folder, you could delete 168 00:08:55,210 --> 00:08:56,500 this very easily. 169 00:08:56,500 --> 00:09:03,600 And so I can open up my Desktop in my Explorer, or in Finder, 170 00:09:03,650 --> 00:09:09,940 or in Nautilus, whatever version you're using on your operating 171 00:09:09,940 --> 00:09:12,100 system. I use a Mac, so it's called Finder. 172 00:09:12,100 --> 00:09:15,400 You could just open that up, and you could find this folder 173 00:09:15,410 --> 00:09:17,760 and delete it, or you can do it from the command line. 174 00:09:17,770 --> 00:09:18,840 I'm going to show you the command. 175 00:09:18,900 --> 00:09:20,310 Please be careful with this. 176 00:09:20,320 --> 00:09:22,620 Do not run this unless you absolutely know what you're doing. 177 00:09:22,620 --> 00:09:28,960 'rm - rf .venv', this is going to remove, this is going to forcefully 178 00:09:28,970 --> 00:09:32,590 remove a folder, and I'm going to pass in the folder name, 179 00:09:32,620 --> 00:09:33,610 and that's just this one. 180 00:09:33,760 --> 00:09:37,810 If you cause a typo here, you could end up deleting a lot 181 00:09:37,810 --> 00:09:39,800 of stuff off of your computer permanently. 182 00:09:39,800 --> 00:09:43,000 So only do this if you are absolutely sure what you're doing. 183 00:09:43,000 --> 00:09:45,300 Otherwise, go through Finder, go through Nautilus, 184 00:09:45,300 --> 00:09:46,300 go through Explorer, 185 00:09:46,300 --> 00:09:48,220 go through some visual tool if you can. 186 00:09:48,230 --> 00:09:55,170 So I'm going to just delete this, 'ls -la', and my virtual environment 187 00:09:55,170 --> 00:09:56,400 is gone. 188 00:09:56,400 --> 00:10:02,300 The second method is with a thing called a 'pipenv', and I'm saying 'pipenv' 189 00:10:02,300 --> 00:10:05,400 Let's use 'figlet' here, 'pipenv'. 190 00:10:05,400 --> 00:10:10,200 Now, 'pipenv' is arguably an easier way to create a virtual 191 00:10:10,250 --> 00:10:14,200 environment. It's a newer way, and it actually requires you 192 00:10:14,200 --> 00:10:16,200 to 'pip install pipenv'. 193 00:10:16,200 --> 00:10:21,200 [no audio] 194 00:10:21,200 --> 00:10:23,800 There we go. 'pip show pipenv'. 195 00:10:23,890 --> 00:10:25,220 Cool beans. 196 00:10:25,220 --> 00:10:30,490 I am now using 'pipenv', and I can create a new virtual environment 197 00:10:30,500 --> 00:10:35,500 with this keyword, 'pipenv', and I can run 'pipenv --help', 198 00:10:36,800 --> 00:10:38,500 and we can see a bunch of commands in here. 199 00:10:38,560 --> 00:10:43,060 We can check clean, install, graph, lock, open, run shell, 200 00:10:43,070 --> 00:10:47,960 sync, uninstall and update. 'uninstall' and 'install' are two that 201 00:10:47,970 --> 00:10:49,400 you're definitely going to want to use. 202 00:10:49,400 --> 00:10:53,700 So instead of using 'pip install', you would use 'pipenv install', 203 00:10:53,700 --> 00:10:54,800 and then the package name. 204 00:10:54,840 --> 00:10:57,840 Now, if you want to create a new virtual environment with 205 00:10:57,840 --> 00:11:00,600 'pipenv', it's as simple as saying 'pipenv install'. 206 00:11:00,660 --> 00:11:04,680 The nice thing about 'pipenv', is you can actually tell it which 207 00:11:04,680 --> 00:11:06,500 version of Python to use as well. 208 00:11:06,560 --> 00:11:09,500 Now, if I want to get inside of the 'pipenv', I can simply do 209 00:11:09,510 --> 00:11:12,230 'pipenv shell', which is what it's telling me to do. 210 00:11:12,240 --> 00:11:15,140 Or if I want to run a command from my computer without being 211 00:11:15,140 --> 00:11:18,700 inside the virtual env, I can run 'pipenv run', and then a command. 212 00:11:18,700 --> 00:11:20,980 So let's go inside here, and you can see this one's going 213 00:11:20,990 --> 00:11:22,150 to look a little different. 214 00:11:22,150 --> 00:11:24,700 And this one says '(kalobtaulien) bash'. 215 00:11:25,900 --> 00:11:29,800 So just do 'python -V', still using the same version of Python. That's good. 216 00:11:29,890 --> 00:11:35,460 And now I can do 'pip freeze', and there's nothing in there. 217 00:11:35,460 --> 00:11:39,100 And you can still use 'pip' inside of 'pipenv' if you want to. 218 00:11:39,120 --> 00:11:42,440 And to get out of this, we don't use the 'deactivate' keyword, 219 00:11:42,440 --> 00:11:47,400 we hit 'Control + D', and that exits for us. 220 00:11:47,410 --> 00:11:49,930 Now to install, we could do 'pipenv install', 221 00:11:49,990 --> 00:11:55,660 let's say 'Django==3.0.5', and it's going to go ahead 222 00:11:55,670 --> 00:11:58,600 and install Django3.0.5. 223 00:11:59,700 --> 00:12:03,300 Now one of the downsides to using 'pipenv', is it does come with 224 00:12:03,400 --> 00:12:07,220 nice, easy commands to use, but it also takes a little while. 225 00:12:07,220 --> 00:12:09,900 So that took, I think, a little longer than it usually would 226 00:12:09,900 --> 00:12:12,100 have to install Django. 227 00:12:12,100 --> 00:12:15,400 And now we can activate this with 'pipenv shell'. 228 00:12:16,300 --> 00:12:18,200 That's just going to get inside of our environment. 229 00:12:18,200 --> 00:12:19,360 We already created it. 230 00:12:19,370 --> 00:12:22,580 All we have to do is get inside of it, and we can do a 'pip 231 00:12:22,580 --> 00:12:23,900 freeze'. 'pip freeze'. 232 00:12:23,960 --> 00:12:25,160 And we will now see that. 233 00:12:25,170 --> 00:12:25,910 Hey, look at that. 234 00:12:25,960 --> 00:12:31,120 Django, asgiref, pytz, and sqlparse are all installed 235 00:12:31,120 --> 00:12:33,700 now. Now, to get out, again, that's 'Control + D'. 236 00:12:33,770 --> 00:12:36,950 And if you wanted to delete your 'pipenv', you could simply do 237 00:12:36,950 --> 00:12:41,800 'pipenv --rm', or let's do 'pipenv --help', 238 00:12:41,800 --> 00:12:43,800 [no audio] 239 00:12:43,800 --> 00:12:45,500 and does that mention anything in here? 240 00:12:45,500 --> 00:12:47,500 [Humming] 241 00:12:47,500 --> 00:12:53,400 Yeah, here we go to, "Remove project virtualenv', and we can simply 242 00:12:53,500 --> 00:12:55,830 run 'pipenv --rm'. 243 00:12:55,830 --> 00:13:01,100 So 'pipenv --rm', and it's deleting all those files for me. 244 00:13:01,160 --> 00:13:05,650 So now, moving forward, all of your projects, everything 245 00:13:05,660 --> 00:13:09,310 you do in Python should ideally be in some sort of virtual 246 00:13:09,320 --> 00:13:11,110 environment. It doesn't matter if you use 'pipenv', 247 00:13:11,120 --> 00:13:12,670 it doesn't matter if you use 'venv', 248 00:13:12,670 --> 00:13:15,600 you could even use Docker, or Vagrant, or some sort of 249 00:13:15,600 --> 00:13:17,400 abstraction layer like that. 250 00:13:17,400 --> 00:13:20,000 As long as you have a virtual environment that's somewhat 251 00:13:20,000 --> 00:13:23,100 separated from the rest of your computer, that is a good way to go. 252 00:13:23,100 --> 00:13:24,000 [no audio]