What is a Virtual Environment in Python?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is a virtual environment in python by the end of this video you're going to know what is a virtual environment how to use a virtual environment when to use a virtual environment and why you should use a virtual environment i'm going to start with the why if you're working on multiple python projects at the same time or you're using lots of different libraries from python you're going to end up with a problem of version conflicts or dependency problems or maybe if you're on a shared machine you're going to have problems where you don't have access to your global python version in all of these cases the problems can be solved by using a virtual environment take the case of version conflicts say you're doing project a and it requires numpy version x but then you move to project b and it requires numpy version y when you switch from project a to b you're going to have to uninstall numpy version x and install numpy version y assuming that those two versions are different enough that's going to be a hassle and very annoying what you can do is create isolated little python sandboxes for both project a and project b so they can keep their separate versions of numb those sandboxes are called virtual environments it's good practice to use a virtual environment for all of your python projects and also almost use a new virtual environment for every project that you make it's just one extra step and saves you a lot of headaches in the future let's look at what a virtual environment actually is over in my terminal i'm going to check my python version with python dash capital v and that's giving me python 3.10.1 this is good if you have python 3.3 or greater the whole virtual environment process gets easier with the package called vamp which is built into the standard python library if you're running python older than 3.3 then you're going to have to do python m pip install virtual m i'll explain the difference between virtual m and vem later in this video but as you can see here i already have virtual m installed and now i'm going to show you how to create this virtual environment this nice sandbox that you can create let's run the command python dash m vam m if you do have an older version of python replace vamp with virtual m m can be anything you want it can just be gibberish it can be something more informative m is nice easy and standard the virtual environment has been created and you can see the updated project directory that i'm in i'm in project right here and we have this m folder now what the heck is that if i click on m this will look a little different for mac or linux in that case instead of scripts it'll be bin so click on the scripts or bin if you're mac or linux and you're going to see python here what's that down here i'm going to type where python once again if you're doing mac or linux that's going to be which python and you can see where your default global python is right here i have this open in this terminal right here and you can see that's my default global python so if i click into lib and then i go down and click to site packages macro linux you're going to have to lib python site packages you can see all these different package names here and what's that pip list pip list shows you all your python packages installed and that's also what i have here in site packages and so all your site packages from pip list correspond to what you see here in lib site packages of your global python but now back in that environment i just created if i go lib site packages i have only a few so in my terminal why doesn't that few list show why does it show the big list that's because we haven't yet activated this virtual environment the nice sandbox how do we do that in windows it's period backslash m or whatever you call that directory scripts activate and so now we see this m right here in parentheses that's good what do we just do m and then we did the scripts and then we ran this activate command which resets our python environment variable from this global python here to our local python up here so what it does first is it looks into lib for our site packages and it ignores these site packages down here if i do pip list i'm going to see that i only have pip and setup tools installed which corresponds to what i'm seeing in this environment site packages folder now if i run pip install numpy all of a sudden this numpy folder shows up and it does not get installed in my global python it only gets installed in this local python environment that i have i'm going to quick upgrade my pip python-m pip install upgrade pip okay that's great so now we figured out how to sandbox our projects to keep different packages isolated from each other so we avoid all this versioning and dependency conflict problem what if we want to transfer an environment from one computer to another you can't just copy and paste the m directory into a github repository it doesn't work like that but we can do is copy and paste all these package versions into a text file and then load that text file and read it and install packages with the appropriate versions how do we do that we're going to look at the command pip freeze so that has what our we just installed numpy equals equals this specific version and we're going to port that into a text file pip freeze greater than requirements.txt now if i go back into my project directory i see this new text document with exactly the results of pip freeze so now if i get out of my virtual environment you do that with command deactivate i'm going to show you pip list and we've got that big list again we're back to my global python here and i'm going to make a new virtual environment python dash m vemm m2 if you have older than python 3.3 do virtual m enter see the m2 show up in this folder right here and we're going to activate that period backslash scripts activate oops i did m not m2 deactivate period backslash m2 scripts activate if you're doing mac or linux just remember that's source m or m2 bin activate and so now if i pip list we're back here and i want to install whatever is on that requirements.txt file so i'm going to run python pip install dash r requirements.txt so it's collecting everything on the requirements.txt file and installing them so now when i do pip list i see that i have numpy installed awesome there's another way to do that and that's using this thing called pip m it merits a whole other video it's a little more confusing and for any beginner requirements.txt is probably going to satisfy your needs what's the difference between virtualm venvemf m is like vemm's older brother it's a bit heavier it's compatible with all python versions or most python versions but it doesn't come standard with the python 3.3 or greater library a problem with vm is it's not upgradable via pip and it's also not extendable but for most people this is not a problem piper endorses the use of vem vovo virtual amph but coming from me i don't know much of a difference to recap a virtual environment creates a sandbox version of your python to avoid problems with versions dependencies or permissions you should use a virtual m for most of your projects and create a new one every single time you can create virtual environments using vem or virtual m although piper and i guess i do recommend using vev if you have any questions about this video leave a comment if you have any requests for future videos also leave a comment i try to go through as many as i can and happy coding
Info
Channel: Sam Westby Tech
Views: 4,195
Rating: undefined 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, virtual environments explained, python venv explained, python package install, python virtual environment requirements.txt, virtualenv, python (programming language)
Id: MfVOUSGbe60
Channel Id: undefined
Length: 8min 52sec (532 seconds)
Published: Sat Mar 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.