AI for Everyone LESSON 3: Understanding and Using Python Virtual Environments

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with episode number three in our incredible new tutorial series where you're gonna learn artificial intelligence or you're gonna die trying what i'm gonna need you to do is pour yourself a nice strong cup of iced coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're getting out your coffee as always i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look in the description below there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and talk about what we are going to learn today this series of lessons this class is about artificial intelligence and i promise you we are going to get to the fun stuff and cool stuff as quickly as we possibly can but we've got to take a couple of lessons to lay the foundation lay the groundwork so you actually understand what we're doing and are not just trying to copy the keystrokes that i'm putting on the screen i want you to understand it and so we got to take two or three lessons to set the foundation so that as we're going through the class you understand why i'm doing the things that i am doing last lesson we learned how to run multiple versions of python on the same windows computer today what we're going to learn is we're going to learn how to take it to the next level which is to run those different versions of python in virtual environments and so we are going to be running in virtual environments and i always would see when i would take python classes online they would start doing this virtual environment thing and just start pounding the keyboard and i'm trying to copy it i never really understood it so i want to tell you what virtual environments are they're little sandboxes where you can play with python without a fear of corrupting your main python installations or your main python interpreters so if you follow along in the last lesson we have installed python 3.6 3.7 3.8 and 3.9 those are over here those are our main python installations now we don't want to fool with those because this is the danger we are doing lessons on 3d animations and 3d graphics okay and let's say we install a lot of libraries and we get everything all tuned up where python is working perfectly to do just these stupendous 3d animations and 3d graphics and then we come over to this series of lessons and we start doing artificial intelligence and we install this bleeding edge library and it somehow corrupts our entire python system that would be bad we would have to go back and re-install everything from scratch so what we want to do is we want to leave our python installations over here untouched and when we install libraries we want to do them in virtual environments over here and so this virtual environment could be like a python 3.9 for the purpose of 3d graphics this could be a python 3.9 for the purpose of artificial intelligence this virtual environment could be python 3.7 for the purpose of facial recognition or something like that so we create these virtual environments and that way what i do in this one doesn't mess up what i did over here or doesn't mess up my main python installation and so believe me i have really really messed up by trying to just run everything off of the same python and so what we want to do is we want to know how to do these virtual environments does that make sense and then we'll create the virtual environment and then we will do our ai inside of that virtual environment and then if you're doing python graphics it would be good to do that in its own virtual environment as well i hope that makes sense so without further ado i will get out of your way we will hop over here to my screen capture and what i need you to do is open up a powershell and so we need to type in power and then you can see the window powershell comes up very important i need you to run it as an administrator okay when you say to run as administrator it'll probably give you a pop-up asking do you really want to allow powershell to change or whatever yeah you say yes okay so now when i come in here it looks like i came into some system folder and so i want to go to my home folder to my user folder we do that by doing a cd for change directory and then the squiggly will take me to my home directory and you can see that for me that's c users and my username is value customer if you're tom you might be in c slash users slash you know tom okay but this will take you to your home folder now if i want to see what i have in my home folder i type dir for directory and you can see that these are all the things in my home folder this is the documents folder this would be like if you're using the folder view the gui view this would be your my documents folder so what i'm going to do is i'm going to go ahead into that and so i'm going to say ah you cannot see that can you sorry about that all right it's like that so hopefully we didn't do that much we just did cd squiggly and then we did a dir and that is all that we've done so far so i think you should be okay so now what we're going to do is we're going to go down into that documents folder and i will do that by cd cd for change directory into documents and if i want to see what's in there so you see i'm in users value customer that's my username and documents and so now if i do a dir these are all the different things that i have here now you need to have a folder to kind of keep track of your python stuff and so it's where you can keep your python programs and then it's also where you put your virtual environments and so i have a folder called python inside of my documents if you don't have that then you do need to do a make directory mkdir and you need to say python when i do that i get an error because there was already a python file a python folder there and so therefore that's why i get the error but you won't get that error you should get a python directory all right now we want to go down into that python directory so we cd change directory into python like that and now i do a dir and you can see i ignore this [Music] what did i just do i'd kind of gone through a dry run of this before you guys tuned in and so i had some virtual environments already in there so i erased them so now if i do a dir i just got python files and some some programs be python v python videos just my python related folders are there okay so that kind of makes sense now what do we want to do we want to create a virtual environment all right and so there is going to be some artificial intelligence stuff that we're going to want to run in python 3.7 and some that we're going to want to run in python 3.9 so this is what you have to understand about virtual environments the virtual inv the python that you run to create the virtual environment will define what virtual and what that virtual environment's python is and so if i create the virtual environment with python 3.7 then that virtual environment is going to run python 3.7 if i create a virtual environment with python 3.9 then inside of that virtual environment it's going to run python 3.9 so you've got to be very mindful and deliberate when you create a virtual environment you've got to be very deliberate about what version of python you run to create the virtual environment so let me give you an example there is going to be there is going to be artificial intelligence work that we need to do in python 3.7 so we're going to need to have a virtual environment so what i'm going to do is i'm going to say pi okay and then dash 3.6 so i'm now running python version 3.7 this is the command that launches python version 3.7 and then what do i want to do i put minus m okay and then i want to run the program v e and v for virtual environment and i fear i better get further out of your way okay and so python dash 3.7 minus m virtual environment this is going to create my virtual environment and now i just got to give the virtual environment a name okay and i'm just going to call it pi a i for python artificial intelligence now i don't have to put a path because i'm sitting in the powershell inside my python folder it will put it in the python folder if i was sitting somewhere else i would need to give the path to the python folder but that's why we navigated here because this will put it in there and so then i'm going to put enter and it waits a second and it waits a second boom look at that okay look at that it get it did that without an error so that's a happy little thing that we didn't get an error and now if i do a dir you see that i have a folder here now called pi ai but the thing to understand is that's now like its own little sandbox of a fresh new python 3.7 environment that i can operate in without a fear of corrupting my main python 3.7 installation but we've got to turn it on if i want to work in there i've got to activate it so what i have to do is i have to say pi i and then slash scripts and guys notice the direction of the slash okay it's different than like the http it's different than a url it goes the other way inside the powershell so it's pi a i slash scripts with an uppercase s and then activate okay now this is why i wanted you to log on as a uh this is why i wanted you to log on as an administrator is the default power shell has it where you cannot run scripts okay and that's for security reasons and so you need to understand security i'm not a security expert but i'll show you how you can run these virtual environments in powershell and then you need to look into the security implications on your own but the way that you can do it is you can say set execution policy so i'm going to set the execution policy and i'm going to set it as remote signed and what that means is when i set it as remote sign let me move that up so you can see it okay when i set it as remote signed it will allow you to run your local scripts that you've written and then if it's a script from the internet it has to be signed so remote signed and then i'm going to put a scope of a scope of [Music] current user man and i just cannot get my head out of the way huh okay now you can see that so we're going to set the execution policy to remote signed and then the scope is current user so it's just uh just for me the current user and then this will allow scripts all right and then when i hit enter it says do you want to really do this i'll say a yes to all and then it looks like it likes it okay now i'll show you how to put it back the way it was set execution policy and instead of remote signed you can go to restricted and if i understand right that should put it the way it was and then a for all and so now we're right back to where we started so you can undo the thing that i'm showing you to do but let's go back and redo it i'm hitting the up arrow key to go back to my old command so i'm going to set the execution policy to remote signed and this should allow me now to activate to activate that virtual environment so remember that pi a i okay that's my python artificial intelligence virtual environment slash script slash activate and when i do that boom look at that now this is your key this is telling me here that i am operating that i am operating inside of my python virtual environment and it's python 3.7 because that is what we installed so now if i say python what's it going to run it's going to run python 3.7 because we use 3.7 to create the virtual environment so i will just say python and boom look at that we are in python 3.7.9 okay now i can do quit to quit out of that and now watch this all right so i can do a pip and i'm still inside of the virtual environment i can do a pip and i'm going to install pi serial guys why do i keep installing and uninstalling pi serial because it's a real simple little library that goes fast and so it just allows me to show that so now i do pip install pass serial and it says that it was successfully installed all right so now if i go python and then i say import import serial boom it imports serial i don't get an error why because inside of this virtual environment i have that installed but now i'm going to quit out of python 3.7 now i'm just back in my python virtual environment and i'm going to leave my virtual environment how do i do that just deactivate deactivate and now i'm just back in my plain old powershell right now like if i just type python here i go into python 3.7.9 but yours might not be configured that way so i'm going to quit so i'm just going to say to make sure that we do that i'm going to say pi and then dash 3.7 that's going to guarantee that i go into python 3.7 if i do that now i am running python 3.7 now watch this import serial and i get a module not found good why because now i'm in my main python installation and you can see that it doesn't have pi cereal because that is in the sandbox that is in that pi ai virtual environment so by installing something there i did not touch my main python environment okay so i am going to quit right now i'm going to dir and you can see there is that python ai environment all right actually that shows you how to make an environment how do you delete an environment let's say that got just so messed up you wanted to just get rid of it you just say remove directory rmdir and then what do you do pi a i i'm going to remove that directory and do i want to remove everything a for yes to all and then if i do a dir you can see that the virtual environment is gone it's like it never even happened i hate being that small i hate being a little postage stamp all right so we made a virtual environment we went in it we left it and then we deleted it well actually i want to do some artificial intelligence in python 3.7 and i want to do some artificial intelligence in python 3.9 so i'm going to create two different virtual environments and so let's start here what do i want to do i'm going to do python and if i want a 3.7 virtual environment i better create it with python 3.7 so i'm going to put dash 3.7 that makes sure that it's going to run python 3.7 and i'm going to have to go back to postage stamp size okay and then we are in the python folder still so it will put it in that folder and then what i want to do minus m i want to create a virtual environment and then what do i want to call it i want to call it pi a i 3.7 okay let's hope that works i always get nervous when it pauses like that but boom look at that no errors okay that's good and now what i want to also do is i hit the arrow key to just call that back i'm going to run python 3.9 and i'm going to create a virtual environment and it is going to be virtual environment pi ai 3.9 okay these names here aren't important you could call that virtual environment whatever you wanted but i just make it match because i choose to i make it match this just so i will keep track of what's what later it helps me remember what's what and i will create that virtual environment by hitting enter take a sip of my go juice and boom look at that we have and i do a dir and we have two virtual environments pi a i 3.7 pi a i 3.9 so let's just do a pi a i 3.99 i need to go to the scripts folder and i need to activate that okay and boom look at that we're in pi ai 3.9 and now if i put python like that it's 3.9.6 which is what we would expect if i say import serial it doesn't import it so i'm going to quit okay and now what do i want to do i want to pip install and then what do i want to pip install i want to install pi serial okay and then successfully installed all right now if i go python i am still python 3.9.6 on python 3.9.6 in my python ai 3.9 virtual environment and now if i say import serial it does it without a problem then i'm going to quit all right now i'm going to deactivate deactivate the virtual environment let's go to that other virtual environment and that is going to be python 3.7 so i'm going to activate my other virtual environment when i put python just as simple as that python it goes to 3.7.9 and import serial okay it's not there why is it not there because it just installed it in that other virtual environment it's playing in the sandbox it's not coming over here so i can quit that quit like that and then i can say pip install pi serial and then says it installed it now i go python and now i go import serial and it's happy okay now i'm going to quit all right now what am i going to do now i'm going to pi i'm going to pip uninstall pi serial just so that we go back the way we were get rid of it yes i want to get rid of it okay and now i'm going to deactivate deactivate that virtual environment i'm going to activate the 3.9 virtual environment activate 3.9 okay and now i'm in the python 3.9 virtual environment python it runs python 3.9.6 and if i say import serial it's still happy because it's serial is still in this virtual environment right so now i'm going to quit out of the python interpreter and now i'm going to de-act to let me just show you one other thing let me go back okay you see i this is like a python interpreter where you can actually put in python command so i can say x equals 7 y equal 3 z equal z equal x plus y and then print z and i get 10. okay see this is like you're in your python interpreter here if that doesn't if i didn't explain that before but now i'm going to quit out of that now i'm back to the powershell and i'm going to say pip uninstall pi cereal and so with that then what i have is i have a virtual environment 3.7 i have a virtual environment 3.9 and they have completely clean okay what i want you to do for your homework assignment now i want you to create four virtual environments because remember in the earlier lesson you installed the latest versions of python 3.6 3.7 3.8 3.9 i want you to create four virtual environments i want you to go into them i want you to confirm that you have the right version of python running in each one of those and i want you to install pi serial on each inside of each one of those virtual environments okay if you do that successfully and you verify it in the comments down below i need you to put the comment i am legend if for some reason it didn't work or you couldn't figure it out you put in the comments down below i folded up like a cheap walmart lawn chair okay one or the other there's no other in between you're either a legend or you fold it up like a cheap form or walmart lawn chair okay also we have a little game that we play here to make sure that you've watched to the end of the video i give you a magic word and what the magic word today is garden fresh salad okay that was actually my lunch today that was my lunch today and i actually grow most of my own food these eggs are from my chickens i have chickens these raspberries are from my raspberry bush i have avocado trees i have lettuce growing even the dressing we made from scratch i have like oregano and thyme and rosemary and all of that sort of stuff so we're kind of living the sustainable self-sufficient lifestyle and most of the food i eat we grow right here on the old compound so to show that you watch it always all all the way to the end and get a little thumbs up for me might even get a little heart on your comment is if you type in the comments garden fresh salad okay all right guys i hope you're having as much fun taking these lessons as i am making them and this has been some really important stuff and it looks so simple when i explain it to you kind of step by step but man i was confused for years by these virtual environments and finally i went in there and figured out what they were and once i figured them out and say i can explain this to people where they understand it now as we move forward you'll understand when i go in and say activate this virtual environment you'll understand it okay and also we can be working over here in this environment doing our graphics works and our 3d animation over here in this virtual environment we can be working on the artificial intelligence and then let's say we wanted to do something crazy like complete com combine the 3d animation work with the with the artificial intelligence we might make our own virtual environment just for that okay guys i am having a lot of fun if you like this video give me a thumbs up leave a comment down below those things really help me with the youtube algorithms also if you haven't already make sure you subscribe to the channel when you do make sure you ring that bell so you will get notifications when the old top tech boy uploads new programs paul mcquarter top tech boy.com i will talk to you huckleberries later
Info
Channel: Paul McWhorter
Views: 8,059
Rating: undefined out of 5
Keywords: Virtual Environments, Python, Lessons, Tutorial, Tutorials
Id: XCvsLMk4OHM
Channel Id: undefined
Length: 26min 45sec (1605 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.