How to Use Python Virtual Environment | Python Venv Module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you've just started to Learn Python Programming I'm quite sure that you're not using virtual environments and to be honest with you when I was starting out I wouldn't use them either and this is fine if you're just learning super basic stuff and not using external packages but believe me from experience virtual environments are going to save you a lot of headaches down the road so let's see how to create them and use them so let's say that I'm building a project and I want to create a virtual environment just for that project okay so here I've got the folder and the terminal inside this code but you can do that from the terminal from the Windows command prompt Powershell or whatever okay okay so to create a visual environment you just need to write this so Python and module then which is the built-in module for the virtual environments and then the name of the folder where you want the virtual environment to be created in okay so in this case I want to just write and like that okay so now as you can see you've got this folder okay you could have here instead of M you could have written an absolute path and just you know like desktop or whatever okay and that would be would have been created in the desktop for example but here I'm creating this inside my folder my projects folder because I've got everything in one place okay so here as you can see I've got a beam folder which is scripts on Windows and here I've got activate the pip Python Etc in here I got my packages on Windows it's not leave python 3.10 but it's just leave side packages and of course these packages are just for this specific project this specific user environment okay so once you've created the virtual environment you need to activate it and you've got different commands for different operating systems so for Linux and Mac you've got Source then and been activate Okay like that and for Windows you can see on the screen the two different ways okay so you can see now at the beginning of the line we have the name like M of the virtual environments the virtual environment that tells us that we use we are actually inside we're using the virtual environment so basically now in the past environment variable for the current shell which is the shell we are on okay there is a path to the python and executables in here okay when we run python we run this when we run pip we run this another system White Once okay in fact if we run pip list we just see the two default packages we're not seeing all the packages installed in my system and of course if we install something like if we install something like pandas this is going to be installed just inside of our environment not system wide as you can see using cached bundles Etc because I've already installed it earlier but you should see downloading stuff like that okay so now we've installed pandas in all the dependencies in our virtual environment and if you see in here freeze you can see all the packages that we've installed manually so freeze returns all the packages that we installed manually as you can see you don't have paper or other packages that were already installed okay so now we need to create an important requirements file you know where you can list all the packages and versions that you need for the project so that you or other people working on this project maybe in the future can install all the required packages with a single command okay so I'm gonna show you how you can create this file with a single command but keep watching because I'm going to explain you why after some research and from experience this is not the best way of doing it okay so to actually save the requirements in a file you can just do something like requirements.txt basically here you're telling save the output of freeze in this file okay as you can see you've got your requirements here but you've got all the requirements okay so in this case the requirement is just funders not all the packages that are installed automatically when installing pandas okay because this is a dependency of pandas this is a dependency Etc so these are installed basically by pandas because they are dependencies of pandas let's see an example to make things clearer okay so let's say that the version 2 of pandas comes out and the version doesn't require the package six anymore so this is not required anymore okay but of course you don't know that because the packet 6 was required by pandas 1.5 and of course it was installed and written in the requirements file as we've done it here okay because we've saved the freeze output so even if you were to create a new virtual environment or another person where to start working on this project okay and install everything from scratch the six packets will still be installed not because needed by pandas but because we've written it as a requirement of our project but this is not the case okay we don't need it and this might cause problems in the future okay so the best way way of raising the requirements file is doing it manually so every time you install something you're going to add the packets and the version manually in the requirements.txt file and like in this case we just need to add pandas like that okay so just find us because this is what we actually need okay and this would then download all the other dependencies automatically and if you update something you could add the new version in the requirements.txt file so if we use the previous example let's say that we update pandas to version two let's say that we've got version two here like that okay and then as you will still have the six installed because you've updated the pandas packet but you still have the if you freeze like that you still have six here let's see that here you've got two but you still have six so if you were to use the same method as above that's here you still have one that's 2.0.0 and then six down here but this wouldn't be required anymore okay because of course this was installed with pandas 1.5 so you should delete the whole virtual environment and create a new one install again everything okay as I show you later and by doing this you will show that you won't have packages that are not required anymore because as you only have pandas 2 as a requirement and not all the others when installing it pandas won't install the package 6 anymore because pandas doesn't need it anymore okay but if we left six in the requirements the packet 6 would still be installed okay I hope it makes sense so before I show you how to install all the requirements with one command we can deactivate the virtual environment just by writing deactivate like that as you can see you don't have amp here so this means that we're not individual environment anymore then to test what I've said just need to go up here delete everything okay yeah I need to write the right version okay but let's say that you've got version two so let's say that here you've got lots of packages you just need to create a new virtual environment so python like that then we need to activate it okay we are using the virtual environment and then to install the packages from the requirements file just need to do something like um requirements.txt enter now as before it's using cached packages it's no problem okay and now if we run freeze so as you can see even though we didn't have all the dependencies here pandas automatically downloaded the all the dependencies so as I said if here you had two and maybe the six wasn't required anymore by deleting the virtual environment and creating it from scratch and then having just pandas 2.0.0 in the requirements and not all the others here you would have just those here here you would have two and this wouldn't be here because this is not required anymore by pandas so you would have just those and not this one anymore I hope it makes sense so now go and watch this other really interesting video about Python and don't forget to use Virtual environments
Info
Channel: Fabio Musanni - Programming Channel
Views: 3,772
Rating: undefined out of 5
Keywords: python, python3, programming, coding, python for beginners, learn python, python tutorial, venv python, python virtual environment, python virtual environment vscode, python venv, python venv tutorial, python venv explained, virtual environment, venv, how to use virtual environment in python, how to create virtual environment in python in windows, how to create virtual environment in python, python virtual enviroment, python virtual env, python tutorial for beginners
Id: UET0u834P7Y
Channel Id: undefined
Length: 7min 13sec (433 seconds)
Published: Thu Oct 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.