Learn Python 1: First install and Virtual Environments - Windows 10

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is Joe Neville and welcome to this video about Python virtual environments so I wanted to start a new playlist for people that are new to - so aimed at beginners and I was thinking to myself what would I teach people that were just starting out and you may think that starting with virtual environments which is more of an intermediate topic is a strange place to start but let me just explain what virtual environments are and then I'll give my reasoning so virtual environments what are they when you install Python to Windows or if you're using Mac OS or Linux which pythons already installed on you have a system-wide install so for the whole OS and through your Python career what you will do is most probably install packages and maybe different versions of Python the problem is that over time if you're just installing everything onto the system-wide install as you can imagine with if you're using lots of different packages and different in versions of Python it can get pretty messy I think most people that are experienced with Python would agree that some very interesting choices have been made with Python so this is one of the one of my personal bug bears it's one of the weaknesses of Python the way that there's so many different ways to create environments and install packages etc because it's like they people have tried to solve this problem in different ways okay but that's getting into the wit and what is a virtual environment this is an environment that is separate from your system wide so it's isolated and it has its own directory structure and what you can do is so if you were going to set up a new project rather than using all the packages that you've already installed you can just have isolated environments with its dedicated binaries and then you install the packages that you want so like requests or keychain or whatever you want to you you just install it into that virtual environment and then when you don't need it anymore you can just deactivate it and it doesn't affect the system-wide so when you the next day when you're doing a different object and you need a different version of Python etc you can just have a different virtual environment that you build install the packages into there and activate it and deactivate it as you wish so it's a nice way of keeping your different installs and your environments separate from the system-wide sounds good doesn't it now the thing is this stuff isn't taught to beginners this isn't taught day one because he is quite complicated me I think you know you've got enough to learn if you want to start learning Python and you'd really don't want to be confusing the newcomer with this kind of detail and I tend to sweep it under the carpet if I'm running a workshop or setting the environment up for somebody but the thing is when you try to recreate this this is a major barrier for people being able to set up their own environment so rather than getting the boot camp or in the workshop to actually get on and build their environment and so that they can continue their Python career by themselves this is the kind of first barrier that they're going to hear and if you're using an editor like pycharm for example Python by default creates its own virtual environments for you each time so you could be happily playing around in PyCharm setting things up but then when you go jump out to the command line and you try to run Python you're not gonna be in your virtual environment and so you may wonder where all your packages have gone okay so those are some of the reasons why I thought let's tackle this head-on and that's what I want to do here what I'm going to do is I'm going to set up a virtual environment on Windows and picking windows because I saw recently on a poll on Twitter with three four thousand I think it's over three I'll show you actually have here it is over three thousand people responded which operating system do you use when writing code 50% windows so I thought what I'll do is because so many people are using Windows I'll give it a go and try to set up virtual environments on Windows for this first video if you don't know Brad he's more of web development actually runs a YouTube channel fantastic resource I've learned a lot from him check him out I'll put a link in the description okay so let us jump over he's my version of Windows 10 it is 1903 and with this edition you may have heard that if you open a command prompt and you type Python then Windows Store opens up and it offers to install Python for you see there you go 3.7 you can get that and install it now I did this so it seems very convenient doesn't it now I did this earlier and I started getting errors where the path wasn't found and then finally I hit so many errors when I was trying to install virtual environments and virtual environment wrapper etc that I just blew it away and install so I uninstalled - entirely from here and I installed it via the good old approach of going to python.org and that all seemed fine so that is what I would recommend to open up a browser go to - dot-org here we are and then if you hover over the Downloads you'll get this button 3.73 the latest version for Windows let's save that and I'll open the folder double-click there okay here's the install 32-bit fine make sure you tick this box it means that you don't have to add - into the path of course install now hit that and off it go so while that since hang on right I have to hit yes fine that's going to go off and install so while it is great thing about this installed actually it's that it's in Turtles Python adds it to your path it also installs pips so we've Python you have the standard libraries that's like your standard feature set and then you have packages that people have posted on the package index and you can import that software into your Python installation so those are the packages that I was talking about that we don't want to go in our global what they want we want them to go into our virtual environment so I will show you that because you there's a web page for the packages so it is pi PI here so it's Python package index will go there and here you can search pod projects you see nearly 200,000 projects it gives you trending projects and things like that so I will go one that I've been using recently is keyring so I'll do a search on keyring there is top hit and there we have this is the page for the keyring project that I can install by running pip install keyring it gives you all details about this I'll probably do a video about keyring pretty soon because I think it's pretty useful but let's go back then ah said it was a successful disable path length let's do that close that one down if we now now we need to Reese I'll close this down as well we need to close that off close that off open up my command prompt again if I type - good that's our install from Python org if it goes off to Windows Store just close the close that all down open up command line again and it will pick it up and you will have the python.org downloaded install so we've got - 37.3 here it is if you do you know the obligatory hello if I can spell world good fine right exit out of that because as I mentioned this is the global install so this is for the whole OS we want to create virtual environments so rather than putting our packages and our different versions of Python if we were to use those all in global we put them in these isolated environments to create a virtual environment then we enter - - M V env so virtual environment and then we give it the name of the environment I'm going to call it my VM - one takes a few seconds it goes off and it will create the virtual environment for us there's no feedback on the command prompt actually right so we're back if we do a dir there you can see that is my virtual environment but it isn't active yet this is still the global install of Python so we need to exit out that what you have to do is you have to activate the virtual environment - then move into it the way that we do that is it's my so the name of your virtual environment and it is scripts and activate so this command is specific to Windows if you using Linux or Mac its source the virtual environment been activate which I'll probably do in a different video but for now this is how we activate it into there and you'll see on this is very helpful we can see that we are command prompt has changed to the name of our virtual environment great now if we enter - this is within the virtual environment so let us overcome it I'll come out of here let's do an install close that off right let's do a pip install then so installing the package into the virtual environment and let's go for that one that I mentioned earlier keyring it downloads it into the environment if we go to - now we can do an import of keyring ok good it didn't error which means that key rings available essentially let's exit out of that clear this off so we have that package keyring in our virtual environment available the way to come out of a virtual environment is to deactivate it so I'm going to deep after Tiger direct deactivate if we go to global and I do an import keyring that you can see I get a so a module not found error because I've jumped over to the global instill rather than my virtual environment so my global is clean of the packages that I want to install into that environment so this is you know a good way of keeping your house in order keeping your main OS nice and tidy we can create multiple virtual environments so let's do that command again I call it my to off it goes again takes a few seconds and remember we still have to activate this so be my two scripts activate now we're in there as you can see my virtual environment - if we go to - import queuing what do you think is going to happen it's not available so not only in the virtual environment separate from the global but they're separate from themselves so that's the power of the isolation for you you might think okay so that's all fine and good but what if I want to copy the packages that I've got in one environment into another rather than having to go and install or deactivate this so there's a quick way you can do that actually because it at the folder level we're always at the same place even though we're moving into these virtual environments we're not actually in a different folder we're in this directory here that you can see that the two virtual environments so what we can do is we can do a pit freeze ah I will move into our parry so I don't have to keep doing this I'll move into one let's do another pip install to get some mixture of packages in we'll do requests as well if that goes to install it fine off so we're in the environment and if we want to take if we want to see the packages that we've got installed we can do a pip list so that's all the packages that we've got in our environment ok so you can see keyring I put in and we've got request in there and some others came in through as part of the requests installed now what if we want to move this list of installed packages into our new virtual environment - - we can go pip freeze into a requirements txt files this will create that the text file for us with the list of the pic packages I'll show you the dir art okay so there's our requirements and if we do what is it more requirements you can actually see the list so this is the list and also it's the level so it's the version that we have of each one of our packages that's really important for security that you keep your packages up-to-date okay so to move this into our new virtual environment we deactivate this one and we do the activation of - of course just activate that and we can do a pip install - our requirements we go and then that will go off and it will install all of the packages that are in the pic requirements list so if we do what is it pip list you'll see all of those in there great so as you can see that's the way if we deactivate that that if I can type and then you go to well I'll show you actually this is the globe if you do a pip list for the global we've only got pip installed there and set up to also case so we've got our environments with our different packages in and I've had I've got two environments and I've taken the packages from one environment and I've used pip fries and I've copied that across into the other environment it's all in the same directory so you don't have to move around folders to do this for this setup okay this is great but over time these environments can start to grow and they can start to mushroom and then they can be very confusing and this happened to me recently when I was going to do a demo on on my main OS that I run my my Mac I've got a lot of projects have got a lot of virtual environments that I've been using that have different packages installed and just before I was going to do a live demo I realized that my virtual environment was actually in a different directory than where I was sad and I was thinking thank goodness I realized that and I was able to set it up because you have to activate those virtual environments before you can run your code that isn't aligned to those packages you see so if you write some Python that relies on these packages which you would normally have in these virtual environments you need to make sure that you've activated the right virtual environment before you run that code or it's going to tell you just like if you wanted to run something with requests you know in a live demo doesn't look so great when you start having this and you're scrambling around trying to remember which one of your many virtual environments that you're you need to activate so that's a problem but there but help is at hand so this is the next thing that we're gonna do is the final part that I'm going to go into well exit out of there there's scripts that you can install from pip which help you out here and this is the virtual environment wrapper which essentially will put all of your virtual environments in the same place and it will give you some command line tools that you can use to access and view which virtual environments you have and then to activate the the ones that you want to work on so it makes it that much easier when you're starting out you may think you know you probably go through these different grades where it's like I just install everything globally then when you get a bit more advanced you're you would be thinking I'm gonna install different virtual environments and then when you move on you've got all of these different virtual environments and these really start to get out of hand that's when you really need to turn to something to give to hold your hand with this stuff and that's where wrapper comes so we're just jumping straight there and I'm recommending that you use something like wrapper to help you out here so what now this is a package that you want to install globally okay not into a virtual environment because it's it's for the whole OS so on global we do a pip install and it is virtual em wrapper and that's the name if you were going to use Mac OS or Linux for this I believe but well this is Windows and it's slightly different so we need to do - win for this is it it's a different project okay I can't I'll show you it actually let's go here so it is what did I say virtual and wrapper - win it should come up there we are okay so this is the project specifically for Windows for a virtual environment wrapper that makes it your life that much easier okay so we are going to run the install there we are so I've installed virtual wrapper now I don't believe it will pick up the virtual environments that we've already created so we'll just have to delete those off but for now what I'm going to do is I'm going to create a couple of new virtual environments using this tool and the way that we do that so it's a different command the way that we do that is make virtual so it's m'kay virtual M if you hear that you can see there that's to check that it's installed properly and I should say this we've - stored this is what was failing with the install of Python store it didn't have it in the path etcetera here it isn't still straight away and it's on the path so it's all good so it's m'kay virtue M make virtue a.m. and then we give it a name so I'll call it new VM let's give it a completely different name so we call it I'll call it a that's a new environment it goes off and creates that puts it in a centralized place and it actually activates it for us there you can see that it does it all in one go so we don't have to do that activation which is really nice you know it's like I said it's kind of makes virtual environments that much more manageable for you you deactivate them the same way I don't need to do anything with that right now but imagine then oh I should show you this actually if you look it's not in the folder where we installed them when we ran them manually when we manually created them with Python - iMix VM it's in a centralized place why that's really useful is imagine if you were going to which you will do with your projects create something like a folder like code and then move into there my called project and you think okay in my project I'm going to create a virtual environment a virtual environment now you can see the folder at the moment is empty if you were to create it via the - - MVM it would be created in here so if then you move somewhere else it would be stored in that directory so it may be like me with the demo it may be quite hard to find so if you instead what you do is you go virtual em and we can create a new one so what is it you VM be create that in the centralized place that's the base this is where they're going to be stored and again it's activated the environment for us great and they work exactly the same way with the pip install etc etc so pip install or keyring - import keyring there we are excellent right but now if we look different from before I don't have the environment here so how do I know which environments that I've got I've got a couple of minutes until my demo and need to turn on my virtual environment what do I do don't panic we do a work on command and it will list the virtual environments that we got and noticed it not the ones that we created with Polly friend - MVM fine but if you use this tool it will have all your virtual environments that you create with the MK virtual environment and it will list them here and that means it's nice and easy to jump between them so if we would you know we can deactivate this one come out of there and the way that you move between them is you just do work on does it complete them for you I think maybe we have to type the whole thing they would yeah well looks like we have to type it out so if I want to go to a I can just do work on excellent can I go be from here yeah I can there we are right so you can jit did you see that you can jump between your virtual environments and then you can just list them as well fantastic stuff okay I hope you found that useful so like I said is quite a complex more of an intermediate it's not exactly a beginner's topic virtual environments but very powerful and very useful and one of the big hurdles that I think a lot of beginners hit when they're starting out for themselves so they done that bootcamp done the workshop etc trying to set up different environments for themselves and this is really easy to get unstuck with because you don't have those packages that you wanted to install because they're in a virtual environment or you go start installing everything into your global and you end up with if quite a mess we've all been there it's nothing to be ashamed of it's one of the major problems with - I believe that's it for this video though please do give us a like or a dislike you know he was good leave us a comment if you've got different approaches to virtual environments then please let me know always great to hear from others and if you're starting out on your Python journey then let me know I've only been learning part I'm a networker so I've only been learning Python for about two and a bit three years maybe on and off so I know the pain of getting started with this stuff and that's essentially why I wanted to tackle this topic but that's all for now my name is Joe Neville thanks for watching and goodbye [Music]
Info
Channel: Airheads Broadcasting
Views: 34,069
Rating: 4.886158 out of 5
Keywords: python, tutorial, coding
Id: x1cbYa2SSlE
Channel Id: undefined
Length: 26min 48sec (1608 seconds)
Published: Wed Jun 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.