Clean New Projects with venv - Virtual Environments

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn how to professionally set up virtual environments for new projects in Python so let us get right into it [Music] alright so oftentimes we're going to be working on multiple different python projects at the same time and those different python projects rely on different packages and oftentimes on different versions of the same packages so for example I might have a data science project that is a little bit older and it relies on Panda's version 1. so pandas one point something and I might have a newer version or a newer project which relies on pandas version 2. something and those are incompatible and on my system I can only have one pandas installation so when I open up the terminal for example and I type pip3 list or to have it in the requirements txt format pip3 freeze you can see that each package has exactly one version number I cannot have pandas one point something and pandas two point something installed because when I install one of them the other one is going to be uninstalled so for example if I say now pip3 install I think I have pandas1 installed here because of a project that I'm working on without an environment and if I say now I want to install pandas equals equals version 2. something uh then it's going to uninstall the installation that I already have as you can see here uninstalling pandas 1.5 to install pandas 2. uh 2.0.3 and the same thing the other way around if I install pandas1 it's going to uninstall pandas2 and because I need both versions depending on the project the best practice way to do that the professional way to work with multiple projects is to create so-called virtual environments so for this I'm going to go to my desktop let's say here now I have two projects let's say project one and I have project two and instead of just using my python installation and all the packages that I have on my system I'm going to create now virtual environments for those two different projects and how do I do that there is a core python tool called vnf that we can use to create these virtual environments now I also have a video already on this channel on Virtual enf which is a third party tool so a package that is not a core python package and this is useful if you're working with older python versions but since python 3.3 there is this uh core python module vnf available and this is the go-to way to work with virtual environments so let's say this is now the project one here we can open up the terminal and we can create a new virtual environment and the idea of a virtual environment is that you have specific package versions and only those packages are installed in that particular environment so you don't get all the packages you have on your system you only get the packages that you install for that virtual environment and you only get the package versions that you specify here inside of that virtual environment and this is useful for multiple reasons first of all of course because you can have different versions of pandas for example for the different projects but second of all you can also just export everything that is installed in the environment into and into a requirement txt file so that people can easily install everything that you have in your virtual environment to reproduce uh the setup basically so there is no real convention for naming the environment you can call your environment whatever you like however the I wouldn't say convention but the go-to way to do that is to create an environment inside of a project folder and to call it either vnf Dot N or Dot vnf and I think that the dot VN version is actually recommended in the python documentation so we're going to go with this one so to create a virtual environment with vnf we're going to say python or Python 3 depending on your terminal and operating system and then we're going to say Dash M vnf and then the name of the environment in our case Dot vnf and as you can see now here it creates a DOT vnf directory in my project folder and what I have to do now is I have to navigate into that directory here and then I'm going to just say here on Linux Source bin and then uh Source bin and then activate and this is going to activate the virtual environment now if I say pip 3 list you will see that I have exactly two packages installed here pip and setup tools so basically nothing and if I say now pip3 install pandas it's going to install the most recent version of pandas and you will see here in a second this is pandas2 so pane is 2.0.3 and if I now say deactivate in this environment uh and if I now say pip3 list and I grab the pandas line you will see that on my main installation without a virtual environment I have pandas one but if I do the same thing here again with activate and then the same command you can see I have pandas 2.0.3 installed so I have different pandas versions depending on the environment and of course I can do the same thing now I can deactivate again and I can go back to the desktop I can go to project uh project two and here I can create another environment with the same name so Python 3 vnfindin Dot vnf and in here for example I do the same thing Source dot vnf bin activate and then I can just say pip 3 install pandas equals equals one point whatever and then it installs a different pandas version in this virtual environment so depending on which virtual environment I'm in I'm going to be working with different packages and of course here I can also now install something else like for example uh opencv Dash python and the good thing about such a virtual environment is now that I can go ahead and Export everything I can take all the installed modules with the versions that are installed and I can export them to a requirements txt file so pip3 freeze is what I want to get this is the text and I can just take this and feed it into a requirements txt file and then I can open this file and you can see it is what we have installed here in this virtual environment so when I create some project here and I want to pass this to someone to tell them look this is what I have and if you want to run this on your systems on your system those are the packages that you need to have installed they can just run pip3 install Dash R requirements txt and they will if they have their own virtual environment they can then reproduce the exact setup that I have here at least on a python level so of course system packages are not included here but this is also a benefit of virtual environments now another way to see that you're in a virtual environment is to see where python is so you can say which Python 3 and you can see that the Python 3 that we're currently accessing here is part of the virtual environment this is not the case if I deactivate in this case you will see that it's in user bin python 3. so yeah this is how you work with virtual environments using the core python module vnf so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 12,493
Rating: undefined out of 5
Keywords: python clean project, clean new python projects, venv, virtualenv, virtual environments, venv tutorial, python venv, python virtual environments, python multiple package versions
Id: sUKgrSHSHtM
Channel Id: undefined
Length: 8min 11sec (491 seconds)
Published: Thu Aug 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.