1 00:00:00,133 --> 00:00:02,756 Hi, in this video, you will learn how to 2 00:00:02,757 --> 00:00:06,586 create and use a Python virtual environment. 3 00:00:06,587 --> 00:00:09,882 So what is a Python virtual environment? 4 00:00:09,883 --> 00:00:14,314 Well, it is just a copy of your Python installation. 5 00:00:14,315 --> 00:00:17,994 So in other words, a copy of your Python interpreter. 6 00:00:17,995 --> 00:00:21,588 So when you download Python from Python.org or 7 00:00:21,589 --> 00:00:25,018 from Anaconda, you are downloading a Python interpreter. 8 00:00:25,019 --> 00:00:26,812 Now we make a copy of it. 9 00:00:26,813 --> 00:00:28,546 Why do we make a copy? 10 00:00:28,547 --> 00:00:32,124 Well, because with Python, we may write different 11 00:00:32,125 --> 00:00:36,540 programs, and for each program we might need 12 00:00:36,541 --> 00:00:40,022 to install different packages, so different libraries. 13 00:00:40,023 --> 00:00:43,750 And usually we install these libraries 14 00:00:43,751 --> 00:00:46,230 in our main Python installation. 15 00:00:46,231 --> 00:00:48,192 So in the interpreter that we got 16 00:00:48,193 --> 00:00:51,460 from Python.org or Anaconda, that is also 17 00:00:51,461 --> 00:00:53,946 referred to as a global interpreter. 18 00:00:53,947 --> 00:00:57,268 But by making a copy for each program 19 00:00:57,269 --> 00:01:01,172 that we built, then we install libraries only 20 00:01:01,173 --> 00:01:03,950 for that copy of the interpreter. 21 00:01:03,951 --> 00:01:08,766 The benefit of this is especially for web applications, 22 00:01:08,767 --> 00:01:12,120 because when you create a web application, you will 23 00:01:12,121 --> 00:01:16,142 deploy it later, in some online servers. 24 00:01:16,143 --> 00:01:18,908 And when you make a copy of the interpreter and 25 00:01:18,909 --> 00:01:22,722 then you install the libraries you need in that copy. 26 00:01:22,723 --> 00:01:25,948 So in that virtual environment, then you have a 27 00:01:25,949 --> 00:01:30,934 clear list of the libraries that you have installed. 28 00:01:30,935 --> 00:01:33,376 So you can generate a list and then 29 00:01:33,377 --> 00:01:36,070 you can give that list to the server 30 00:01:36,071 --> 00:01:38,832 where you will deploy your web application. 31 00:01:38,833 --> 00:01:41,892 So that server will get the list of 32 00:01:41,893 --> 00:01:45,124 libraries from that list and install them. 33 00:01:45,125 --> 00:01:47,892 Now, the benefit here is that if you had 34 00:01:47,893 --> 00:01:52,490 your global Python installation, you'd have many, many libraries. 35 00:01:52,491 --> 00:01:55,780 So that server would have to install all those 36 00:01:56,310 --> 00:01:59,768 many libraries, which is not a good idea. So, 37 00:01:59,769 --> 00:02:02,216 so why not having one 38 00:02:02,217 --> 00:02:06,088 interpreter for each Python project? 39 00:02:06,089 --> 00:02:08,395 So now that we are about to create 40 00:02:08,396 --> 00:02:10,828 our Django app, we are going to create 41 00:02:10,829 --> 00:02:15,788 a virtual environment first, on Visual Studio Code. 42 00:02:15,789 --> 00:02:17,164 This is how you do it. 43 00:02:17,165 --> 00:02:20,272 So first of all, open Visual Studio Code. 44 00:02:20,273 --> 00:02:22,080 So this is the welcome page. 45 00:02:22,081 --> 00:02:26,288 I haven't created any project yet, so I'm going to 46 00:02:26,289 --> 00:02:31,732 go and create a project which is basically you just 47 00:02:31,733 --> 00:02:36,804 open a folder, a directory, in Visual Studio Code. 48 00:02:36,805 --> 00:02:39,658 So go to File and then go to Open Folder 49 00:02:39,659 --> 00:02:42,500 or Open, depending on what operating system you are. 50 00:02:42,501 --> 00:02:45,182 So this can either be Open or Open Folder. 51 00:02:45,183 --> 00:02:48,686 Click there and then locate an empty 52 00:02:48,687 --> 00:02:51,144 folder, which you should have created before. 53 00:02:51,145 --> 00:02:52,862 So I created this empty 54 00:02:52,863 --> 00:02:55,534 folder, django_blog_translator. 55 00:02:55,535 --> 00:02:57,314 You see, it's completely empty. 56 00:02:57,315 --> 00:02:59,560 So I'm going to press on Open, 57 00:02:59,566 --> 00:03:03,460 [No Audio] 58 00:03:03,461 --> 00:03:07,766 and, that will open that folder. Here 59 00:03:07,767 --> 00:03:12,682 you see here, in my EXPLORER tree on Visual Studio Code. 60 00:03:12,683 --> 00:03:14,624 Now that we have the folder. 61 00:03:14,625 --> 00:03:16,906 So in this folder, we are going to 62 00:03:16,907 --> 00:03:20,698 put all the files for our Django project, 63 00:03:20,699 --> 00:03:24,980 including, the copy of the Python interpreter, 64 00:03:24,981 --> 00:03:27,502 so the virtual environment. To create this 65 00:03:27,503 --> 00:03:29,662 copy, this is how you do it. 66 00:03:29,663 --> 00:03:31,982 So first of all, you need to open a 67 00:03:31,983 --> 00:03:37,234 Terminal, and you have to do a check first. 68 00:03:37,235 --> 00:03:40,626 So make sure what Python you are using. 69 00:03:40,627 --> 00:03:43,938 So in my case, I use a 70 00:03:43,939 --> 00:03:48,316 command python 3.9, and that opens Python. 71 00:03:48,317 --> 00:03:51,810 So this is a global Python installed on my computer. 72 00:03:52,580 --> 00:03:55,254 So I'm sure that this command is working fine. 73 00:03:55,255 --> 00:03:57,046 In your case, it could be just 74 00:03:57,047 --> 00:04:01,082 Python without 3.9 or Python 3. 75 00:04:01,083 --> 00:04:03,018 It depends what you have been using. 76 00:04:03,019 --> 00:04:06,624 So wherever you have been using, I'm 77 00:04:06,625 --> 00:04:09,344 going to exit this interactive shell. 78 00:04:09,345 --> 00:04:12,624 So this was just to try out the python command. 79 00:04:12,625 --> 00:04:14,222 Now I know this is working, so 80 00:04:14,223 --> 00:04:18,062 I can go and do python3.9. 81 00:04:18,063 --> 00:04:19,694 So no spaces here. 82 00:04:19,695 --> 00:04:26,622 Then I space the m flag, then venv then env. 83 00:04:26,623 --> 00:04:27,806 So what is this? 84 00:04:27,807 --> 00:04:32,328 Well, venv is a library that is installed, by default. 85 00:04:32,329 --> 00:04:34,120 It's a standard library for Python. 86 00:04:34,121 --> 00:04:36,580 It comes shipped with Python. 87 00:04:36,581 --> 00:04:41,116 So I am using that library. 88 00:04:41,117 --> 00:04:45,516 So when you use that command python 3.9 or python, 89 00:04:45,517 --> 00:04:48,598 and then you use that flag, that means that you 90 00:04:48,599 --> 00:04:53,148 are about to execute that library as Python scripts. 91 00:04:53,149 --> 00:04:57,546 And then this end is the folder where, the 92 00:04:57,547 --> 00:05:00,980 copy of the Python interpreter will be placed. 93 00:05:01,560 --> 00:05:05,328 So press Enter and you see immediately 94 00:05:05,329 --> 00:05:08,302 that env was created, this folder here. 95 00:05:08,303 --> 00:05:11,758 And if I expand this folder, you'll see 96 00:05:11,759 --> 00:05:13,998 there are several other folders in here. 97 00:05:13,999 --> 00:05:16,866 And inside this bin here, you will 98 00:05:16,867 --> 00:05:20,760 see that we have the Python executable. 99 00:05:20,761 --> 00:05:22,920 So that is a copy of Python. 100 00:05:22,921 --> 00:05:24,626 It can either be used as just 101 00:05:24,627 --> 00:05:27,190 python or python3 or python3.9. 102 00:05:27,191 --> 00:05:30,572 But I'll show you how to use that Python 103 00:05:30,573 --> 00:05:34,758 instead of a global Python now, so don't do 104 00:05:34,759 --> 00:05:38,118 anything with that folder, because what we want to 105 00:05:38,119 --> 00:05:42,280 do now is we want to select a default 106 00:05:42,281 --> 00:05:47,210 Python interpreter, for this particular Visual Studio Code project. 107 00:05:47,211 --> 00:05:50,600 To do that, you want to go to the command palette. 108 00:05:50,601 --> 00:05:52,612 You can do that with shortcuts 109 00:05:52,613 --> 00:05:56,766 actually, on Mac, it would be Cmd+Shift+P. 110 00:05:56,767 --> 00:05:59,294 Press the three of them at the same time. 111 00:05:59,295 --> 00:06:03,850 On Windows, it would be CTRL+Shift+P. 112 00:06:05,760 --> 00:06:07,858 So that will show this box. 113 00:06:07,859 --> 00:06:12,400 And then you can type in there, select interpreter. 114 00:06:12,401 --> 00:06:14,578 So this is what we want to 115 00:06:14,579 --> 00:06:19,380 use Python: Select Interpreter, that command. 116 00:06:19,381 --> 00:06:24,086 Click on there and you will see this list. 117 00:06:24,087 --> 00:06:28,918 So, I have different Pythons in my system. 118 00:06:28,919 --> 00:06:32,176 One of them is this virtual environment 119 00:06:32,177 --> 00:06:34,208 here, that I have just installed. 120 00:06:34,209 --> 00:06:37,066 And that is the one that I want to choose, 121 00:06:37,067 --> 00:06:38,470 so press there. 122 00:06:39,160 --> 00:06:42,510 And now you should see down here that 123 00:06:42,511 --> 00:06:47,316 Python 3.9.6, in my case with this folder. 124 00:06:47,317 --> 00:06:49,444 So env is the folder, venv 125 00:06:49,445 --> 00:06:53,076 is the Virtual Environment library. 126 00:06:53,077 --> 00:06:56,696 This now will be used as default Python. 127 00:06:56,697 --> 00:06:57,666 What does that mean? 128 00:06:57,667 --> 00:07:01,554 Well, that means that if you now type in Python here, 129 00:07:01,555 --> 00:07:05,442 but first, close this Terminal, and open a new one. 130 00:07:05,443 --> 00:07:07,558 So to have a refresh of the 131 00:07:07,559 --> 00:07:11,750 Terminal, New Terminal, you'll see that, 132 00:07:11,751 --> 00:07:15,638 now, before this tag here, that tells me to 133 00:07:15,639 --> 00:07:19,788 write something here, I have this env, in parentheses. 134 00:07:19,789 --> 00:07:21,648 That means that this Python 135 00:07:21,649 --> 00:07:23,498 Virtual Environment is being used. 136 00:07:23,499 --> 00:07:27,152 So now if I just type in python, Python 137 00:07:27,153 --> 00:07:31,806 3.9.6, of that Virtual Environment is being used. 138 00:07:31,807 --> 00:07:33,000 Let me exit. 139 00:07:33,900 --> 00:07:39,278 I can also type python3.9 and the same will be used. 140 00:07:39,279 --> 00:07:42,382 So any of these commands here, 141 00:07:42,383 --> 00:07:44,970 python, python3 or python3.9. 142 00:07:44,970 --> 00:07:47,500 [No Audio] 143 00:07:47,501 --> 00:07:49,950 So make sure you have that env there. 144 00:07:50,960 --> 00:07:52,882 So how does this work? 145 00:07:52,883 --> 00:07:54,456 Because it looks like magic. 146 00:07:54,457 --> 00:07:56,258 Well, it works like this. 147 00:07:56,259 --> 00:08:00,038 Let me close this Terminal again and open a new one. 148 00:08:00,039 --> 00:08:05,916 New Terminal. So you see that, here we have a command being executed 149 00:08:05,917 --> 00:08:10,112 by Visual Studio Code, when you open a New Terminal. 150 00:08:10,113 --> 00:08:12,122 So it's a source, which is a 151 00:08:12,123 --> 00:08:15,984 command to activate the virtual environment. 152 00:08:15,985 --> 00:08:20,784 So source and then the path to the activate file, 153 00:08:20,785 --> 00:08:27,640 you see it's django_blog_translator, which is this folder. 154 00:08:28,166 --> 00:08:34,960 /env/bin/acitvate so, /env/bin/acitvate. 155 00:08:34,961 --> 00:08:37,666 This activate file is being executed, 156 00:08:38,666 --> 00:08:42,385 and that is the command that sets the 157 00:08:42,386 --> 00:08:46,460 environment, to this virtual environment. 158 00:08:47,940 --> 00:08:51,970 That means also that you can use python both, 159 00:08:52,740 --> 00:08:56,630 from here as an interactive Python shell, but also 160 00:08:56,631 --> 00:08:58,770 if you want to write a file now. 161 00:08:58,770 --> 00:09:00,840 [No Audio] 162 00:09:00,841 --> 00:09:03,620 So let's say create a file. 163 00:09:05,400 --> 00:09:08,688 So be careful here, I'm going to write hello.py. 164 00:09:08,689 --> 00:09:11,994 I'm saying be careful because, what happens is that 165 00:09:11,995 --> 00:09:17,188 this file was created inside my env folder. 166 00:09:17,189 --> 00:09:18,702 So that is not a good thing. 167 00:09:18,703 --> 00:09:21,588 You don't want to touch the env folder. 168 00:09:21,589 --> 00:09:25,150 So I'm going to delete that file and recreate it. 169 00:09:25,166 --> 00:09:27,200 [No Audio] 170 00:09:27,201 --> 00:09:30,648 To avoid the file being inside the env file, 171 00:09:30,649 --> 00:09:34,482 you want to first press Escape, and that will 172 00:09:34,483 --> 00:09:37,548 take the focus out of that env folder. 173 00:09:37,549 --> 00:09:41,200 And then you can go here and create the file. 174 00:09:41,201 --> 00:09:44,100 [Author Typing] 175 00:09:44,101 --> 00:09:45,548 Now you see that the file 176 00:09:45,549 --> 00:09:48,476 is created in that root directory. 177 00:09:48,477 --> 00:09:52,980 So DJANGO_BLOCK_TRANSLATOR, hello.py. 178 00:09:54,520 --> 00:09:59,674 And if I say print Hello here, right? 179 00:09:59,675 --> 00:10:02,462 And now I can execute this 180 00:10:02,463 --> 00:10:05,038 file, using this green triangle here. 181 00:10:05,039 --> 00:10:10,020 So the run button, press that, and the file 182 00:10:10,021 --> 00:10:15,656 will be executed, using the virtual environment python. 183 00:10:15,657 --> 00:10:20,140 So you see, see here, what happened on the background. 184 00:10:21,520 --> 00:10:25,016 So again, to wrap it up, the virtual environment 185 00:10:25,017 --> 00:10:27,196 was used when we opened the Python shell. 186 00:10:27,197 --> 00:10:30,444 It was also used when we executed the Python file. 187 00:10:30,445 --> 00:10:36,188 And it's also used if we install, third party packages. 188 00:10:36,189 --> 00:10:39,786 So let's say pip install django, to install 189 00:10:39,787 --> 00:10:43,306 Django, that Django will be installed for this 190 00:10:43,307 --> 00:10:46,900 particular Python, not for our global Python. 191 00:10:48,200 --> 00:10:51,706 And I can prove you that, by 192 00:10:51,707 --> 00:10:55,000 going to env and then to lib. 193 00:10:55,001 --> 00:10:58,350 [No Audio] 194 00:10:58,351 --> 00:11:01,136 And you will see that, Django was just 195 00:11:01,137 --> 00:11:04,480 added to the third party packages in here. 196 00:11:04,481 --> 00:11:07,366 So Django, this is what we just installed. 197 00:11:07,367 --> 00:11:10,133 [No Audio] 198 00:11:10,134 --> 00:11:15,600 To get rid of this warning, you may want to execute this command as being suggested, 199 00:11:15,601 --> 00:11:18,933 [No Audio] 200 00:11:18,934 --> 00:11:22,130 python3.9 or python directly. 201 00:11:22,870 --> 00:11:28,456 -m pip install --upgrade pip, to upgrade the 202 00:11:28,457 --> 00:11:33,320 local, the virtual pip, so to say, right? 203 00:11:34,250 --> 00:11:37,966 And now if I close this project 204 00:11:37,967 --> 00:11:40,466 [No Audio] 205 00:11:40,467 --> 00:11:42,100 and open it again, 206 00:11:42,750 --> 00:11:47,456 then I can go back to my previous project, either by 207 00:11:47,457 --> 00:11:52,352 using this recent section here, so clicking on that, or by 208 00:11:52,353 --> 00:11:57,156 going to File, Open Recent, and locate their project there. 209 00:11:57,157 --> 00:11:59,028 Or a third way would be again to go 210 00:11:59,029 --> 00:12:05,090 to File, Open and go again to that folder. 211 00:12:05,091 --> 00:12:06,910 So to the root directory. 212 00:12:07,510 --> 00:12:09,992 So those are env and hello.py and press 213 00:12:09,993 --> 00:12:12,980 Open, and the same project will open again. 214 00:12:13,590 --> 00:12:18,232 But you have to close the current Terminal and 215 00:12:18,233 --> 00:12:22,664 open a new one, in order to get the 216 00:12:22,665 --> 00:12:26,142 virtual environment active, in the new Terminal. 217 00:12:26,143 --> 00:12:28,936 You see, now we see this env here. 218 00:12:28,937 --> 00:12:30,958 And of course, now you can also execute 219 00:12:30,959 --> 00:12:33,568 that file again, and the virtual environment will 220 00:12:33,569 --> 00:12:37,398 be used, to execute and get the output. 221 00:12:37,399 --> 00:12:38,672 So that's how you can use 222 00:12:38,673 --> 00:12:41,666 virtual environments in Visual Studio code, thanks.