The Complete Guide to Python Virtual Environments!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] most python projects we develop will have dependencies that's code written by other people that we can install and use so that we don't have to rewrite it ourselves for example the requests library is a super popular third party library that we can install to make http requests the flask library is another one that we use to create web applications and there are thousands upon thousands of libraries to do all kinds of things written by tens or hundreds of thousands of people so we install and use them so that we don't have to rewrite it because that would take a really really long time the libraries change over time as new and better ways to do things are discovered and implemented as such when we install a library for one of our projects it'll be a specific version and months or years down the line if we try to install it again it may well be a different version with different functionality therefore it's possible that when we update a library our code will no longer work because the way that we should use the library has changed virtual environments exists so that we can separate the dependencies of one project from the dependencies of another that way they can have different versions of the same library and that often happens when we start projects at different times but what actually is a virtual environment well there are two key parts to a virtual environment the first one is the python version every virtual environment has a python version that it runs on for example python 3.9 every virtual environment also has a folder of third-party libraries that you've installed in that virtual environment so every virtual environment we create can be created with a different python version and it can't be changed after the fact that lets us work on projects that use different python versions very easily just by switching between virtual environments similarly because every virtual environment has its own folder of third-party libraries they can have different libraries or the same libraries in the same or different versions to really understand how virtual environments work it will be helpful to look at how python imports work i've created a project here in vs code doesn't matter where you created just create an empty project in your editor of choice and i'm going to create a python file called app.pui in here we're going to do import cis and notice how vs code says that no python interpreter is selected that means that we haven't told vscode which python we're going to use when we create a virtual environment which we will do in a moment we'll tell vs code to use that virtual environment but for now we're just going to make it go away so import says and then we're going to print sees path so this is going to print out the import paths or where python will look when we try to import things so if we wanted to import the requests library then python would look for the requests library inside sees.path so now i'm going to save this we're going to go over to our console i'm using cmd.exe although vs code has a console integrated to the editor that you can use if you're comfortable with that but i'm going to use the command prompt so i've opened my command prompt here and now we're going to go into this project and the way we change directories with the command prompt is we do ch there and then we go to the desktop now you can see that my path has changed i'm now in my desktop and then we can ch there into the virtual lens project and now i'm in here if i do dar then you can see that app.py is there i know that this looks all a little bit complicated but that's essentially what's happening there so how do we run this file we do python app.py and that is going to run this code using the python program and it's going to do whatever the code does and you can see this is what sys.path contains the first path is the current folder that i'm in so that's the virtualmin project folder and then there's a few other import paths and you can see that most of those are to do with python 2.7 so if we try to import the requests library python is going to look for the request library inside this folder and obviously it's not there because there's only this file in there so then it'll move on to this other folder then it'll move on to this one and so forth and whenever it finds it it will import it and use it and it will stop searching in the future folders if it isn't found in any of these folders then python will raise an import error with a message telling you that the thing you're trying to import doesn't exist so this is going to be relevant in a moment but first let's take a look at how we can create a new virtual environment for this project so if you haven't created a virtual environment before and you're not following along do so open up a command prompt open up your editor and create a virtual environment with me so we're in the project folder and what we're going to do is we're going to ask python to create a virtual environment the way we do that is we need to use python 3.3 or later so you can see here this is all python 2.7 stuff so what i need to do is i need to grab my path to my python 3.8 installation you can see it's in my app data programs python python38 python.exe so this is my python program that runs python 3.8 and then we're going to do dash m vm and then we're going to put in our virtual environment name this is going to be a folder that gets created and inside that folder we're going to put our virtual environment so you can call it whatever you want it can be vm name or it can be vn it can be dot vm that's a pretty popular name as well i'm going to call it vm name just to show you that this is what the folder ends up being called so when we do that notice that vs code immediately notices that a new virtual environment has been created that's all well and good in our vs code folder there you can see vm name is a folder that's been created which is fantastic so inside there we now can see that there's a few things there is a library there with a bunch of things that the virtual environment comes with there's a scripts folder that has python.exe inside it and it's got pip.exe inside it as well so we can run python 3.8 just by running this python.exe program notice that if i type python-v to get the python version that i'm currently running that gives me python 2.7.16 so even though we've created a virtual environment with python 3.8 the python version this console things we want to use is 2.7 so that's not right so we've got our virtual environment created but how do we use it the first thing we have to do in order to tell the console that we want to use the virtual environment is we have to activate it and when we activate it some configuration changes happen in the console when we deactivate it afterwards the configuration changes are reverted also the changes to that configuration are only applied in the console on which we activate the virtual environment if you open a separate console it won't have those changes so what changes am i talking about well let me first print out a special variable in the console called path so in windows i need to echo and then in between percent symbols i put path then on mac and linux there's a slightly different way to print this out but i'm going to echo path you can see there's a lot of stuff that gets printed out here which is going to be a little bit confusing but this is where the console will look for programs to run when you type a program name in there so python is a program name and the console doesn't inherently know what that is so when you type python the console is going to go through each one of these paths looking for python.exe so the first path is in this program files x86 blah blah blah compiler and then you can see a semicolon so that tells us that we're moving on to the next path so this is the first path there then this is here the second path and then we've got the third path etc when the console finds python.exe in any of these paths again just like what we had here with the python imports then it's going to run it and it's going to stop searching so why are we running python 2.7 because the first python.exe that console is finding in this set of paths is python 2.7 and you can see that down here see if i can spot it and there's a python37 there so clearly python27 will be earlier on okay so here it is took me a while to find it it's in c python27 scripts so this is the first python exe that the console is finding so you you may spot python32 scripts there in front but this folder actually doesn't exist so i should probably delete it from my path and you can also see 3732 script that folder also doesn't exist so it's actually python27 scripts that gets found first that's where python exe lives there are other python 38 and 37 folders down here and those actually do exist but because they're after the python27 then it is python27 that gets found so let's activate the virtual environment and see those configuration changes i talked about you can activate the virtual environment using the source command so source vm name bin activate on mac or linux if you're using windows with wsl or git bash it's the same thing but with scripts instead of bin and if you're using cmd.exe or powershell then you do um vm name slash scripts slash activate.bat so that's what we're going to do here and you can see that i get this bracket vm name in front that is just the way of the control telling me that i've got this activated and now if i do echo path you'll see that the very first path is to my virtual and project vm name script so therefore python exe which is inside the scripts folder is going to be found first and indeed now if i do python-v you can see that we get python 3.8.2 because that is a python version that we used when we created the virtual environment so that's the version of python the virtual environment uses if you run the vm command that we ran earlier to create the virtual environment using a different python version then this would be something else but it's important to note that this python 3.8 is not exactly the same executable as the python 3.8 that we used earlier it is a copy a new python binary that gets created and put in here so it's not exactly the same one it's just the same version also now we can run pip and the pip that is going to run is this pip.exe we could run pip 3.8 and that would be this pip 3.8 exe pip3.exe etc you can see that these three here are the same these all refer to the same executable and similarly python exe is its own separate thing when we install a library using pip the library will end up installed inside the virtual environment so let me do pip install flask for example and that is going to go ahead and install the flask library and you'll see that we've got flash.exe that's landed up now on our scripts folder that's because flask has been installed in the virtual environment but not outside the virtual environment whenever we import flask now from our code as long as we've got the virtual environment activated and we use this python version here this code will be able to import the flask library alright so what do we do now with this virtual environment well often we'll create a virtual environment for every python project we create and we do that when those projects have dependencies which is basically 99 of the time we should keep track of the dependencies that a project has so that if we share that project with someone else they can install those same dependencies also if we have to come back later on down the line and reinstall the dependencies for some reason we'll know what we need to install to run the project we do that with a requirements.txt file this is the name of the file that we usually use it doesn't have to be called this though and in here we'll put the different dependencies of our project for example flask requests g unicorn these are three frequently used dependencies for web applications sometimes if you have dependencies that are only used for development but not to actually run the code for example for testing or deployment you'll create a file called dev dot requirements.dev.tx that's the name that's usually used but again you don't have to call it that when you want to install all the dependencies listed in your requirements.txt file all you have to do is activate the virtual environment and then do pip install r and then requirements.txt and that is the way to give it a file to install so it'll go through and install all the dependencies obviously the dependencies that are already installed won't get reinstalled so now we've installed requests and g unicorn as well in there inside the requirements.txt file we can also put the exact version numbers of the libraries that we used this can be useful because libraries change over time and your code may not work with the latest versions so usually we'll write down the versions that we used when developing the code for example 1.0.0 if that's the version that we used with the double equal sign there that tells the installer that it should use exactly 1.0.0 and not for example 1.0.1 more on that in a second updating the version number of any of your dependencies should be a conscious decision so don't just update to the latest version of every library every time because sometimes we'll need to make changes to the code in order for it to work with a new version of the library it's also useful to know about semantic versioning because that's what we use for python libraries so in library versioning the major version is the first number so these two would have the same major version but they are on different minor versions and patch versions oftentimes we want to install the latest version of a library while staying within a major version that way if we install the dependency in the future we'll install the same major version that we were working with in the past but we'll gain any small improvements the library has made in its minor and patch versions usually libraries don't add breaking changes to minor and patch versions that's why this is a common thing to do but do make sure to check every specific library you do this on to make sure that nothing important has changed so to install the latest miner or patch version of a library we can do this you can say flask greater than or equal to 1.0.2 for example but with a comma less than 2.0 and that is going to keep us within these two numbers always giving us the latest version one flask all right that's about everything for this video i hope you've learned a little bit about virtual environments and how they work i'm going to leave a couple of links in the description of this video to some official documentation and some guides that you can read through to learn more also you can use things other than vm to work with virtual environments for example pippen is a very common alternative but i think it's best to keep it simple and use vm so i would recommend sticking to what i've shown you in this video please like and subscribe if you want to see more videos like this and i will catch you on the next one
Info
Channel: teclado
Views: 1,958
Rating: 4.9603958 out of 5
Keywords: virtual environment, virtual environments, python tutorial, python 3, python virtual environment, python tutorials, python packages, python programming, python venv, python virtualenv, python virtual environments, python (programming language), virtual environments explained, virtual environments visual studio code, python virtual environment flask, python venv explained, python package install, python virtual environment requirements.txt
Id: KxvKCSwlUv8
Channel Id: undefined
Length: 15min 52sec (952 seconds)
Published: Tue Apr 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.