Python Virtual Environments - Full Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is a virtual environment well this is simply a self-contained location that enables you to maintain separate and isolated environments for your python projects this isolation lets you manage dependencies versions and packages without conflicts across different projects when you work on multiple python projects it's likely that some of them depend on different versions of external libraries or even python itself without virtual environments if you were to install every dependency globally you might run into issues where one project needs a different version of a library than another project this can lead to compatibility issues virtual environments solve this issue by allowing each project to have its own set of installed packages independent of what's installed globally or in other environments so now let's look at how you create virtual environments so to begin we need to open up a terminal or a command prompt if you're on Mac or Linux open up your terminal if you're on windows open up command prompt or a Powershell instance from here we're going to navigate into the directory where we want to create a new version virtual environment it's worth noting that virtual environments are simply directories on your system so you want to put them in a place that's logical like the location of a python project that requires this environment in my case you can see that I am in the example directory which is on my desktop so simply go to a directory where you want to create your virtual environment from here we're going to create a new environment using the built-in venv command or V EnV now it's worth noting that there are other tools that can do this like cond virtual EnV pip en V and poetry but VV is built into Python and it's the most common one now the commands for creating and activating the virtual environment are slightly different for Mac or Windows but I'll run through both sets so if you're on Windows you're going to use this command which is python hyphen M venv and then EnV now the EnV part here at the end is the name of the environment you can name this anything that you like but it's convention to name it EnV now if you're on Mac or Linux you'll simply change the command to be Python 3 so my case I'm on Mac so I'm going to run python 3-m VV EnV now at this point it will create a new directory for us so if we type LS you'll see that we have the environment EnV in this directory now what we need to do is activate the virtual environment and to do that again the command will be slightly different depending if you're on Mac or Windows if you're on Windows you're going to run the following command now the command is this the name of your environment which in this case is EnV SL scripts activate. bat now for some reason that doesn't work you can try just SL activate so remove that bat extension now if you are on Mac or Linux the command is a little bit different this is going to be Source EnV SL bin and then slash activate like that and once you've activated the environment you should see that you get a prefix with the name of the environment appearing before the normal line in your terminal so whether you're on Mac windows or Linux you'll know this is working once you see the parenthesized version of your environment name before your main terminal line now that we're here I'll show you how we deactivate the environment so to go back into our Global python installation to do that you simply type deactivate the command will be the same whether you're on Mac windows or Linux then we can reactivate by using the source command again or that activate script that I showed you on windows so now that the environment is created and activated we can go ahead and install some packages into it now to do this we're going to use the PIP command which stands for the preferred installer program or the recursive acronym which is PIP installs packages now if we want to view the packages that are in the current environment we can start by typing pip list now it will show us that we can actually upgrade pip if we like to we don't need to do that right now but we can see that we just have two packages pip and setup tools the reason we don't see any other ones is because we're in the virtual environment now not in our Global version of python if we were to deactivate this and run the command again we'd see any packages in our global environment but here we're in the virtual one now that we've done that we can install a package so one some of us have probably used before is the request package so let's go pip install request when we do that it should install everything and if we type pip list you'll see now that we get all of the dependencies for requests as well as the request package itself you'll notice now if I deactivate the environment and I type pip list it's actually needs to be pip 3 here I'll get a huge long list of a bunch of other packages that are installed globally whereas if I go back into the environment here and I type pip list we just see the ones that are part of this environment so now this point let's imagine we've done some intense python development and we're ready to push this to GitHub or share our project with someone else now in order for them to run this code they'll likely need the same requirements as us now rather than exporting our entire environment we can simply save a list of all of the dependencies we used into a requirements.txt file now to do that we're going to use the following command which is PIP freeze we're then going to use the greater than sign and then we're going to type requirements.txt if I spell that correctly and what this will do is take the output of pip freeze which gives us all of the different packages and their version and save it in this file so when I do that and I type LS you'll see that now we get a requirements.txt file and if we have a look at requirements.txt it has the output of pip list that we looked at before now let's get out of this here and let's see how we could use the requirements.txt file to actually install these various packages so let's imagine we're someone else we've now got access to this code we want to start working in it and we have requirements.txt file well first we'll make sure that we're out of this environment and we'll probably end up creating a new one so we'll start again and we'll type the commands so in this case Python 3 hyphen M venv we'll just call this mycore EnV we'll create a new environment we will then activate it so this will be source and then my EnV /bin SL activate again on Windows this going to be a different command for Activation now we're inside of this environment this is a new environment and you'll notice that now we have EnV my EnV and requirements.txt if I pip list inside of here you'll see that we don't have those requirements so the request module that we need but we can install them using this requirements.txt file to do that we very simply just type pip install dasr and then requirements.txt the DHR stands for install from a file so we specify the file name which is requirements.txt and now what pip will do read this file find all of the requirements and install it in this environment so when I hit enter here it's going to go ahead and install that and now if we type pip list you'll see that we get a list of all of the different packages that were from our requirements.txt file so this is very common practice you'll actually download a python project you'll see a requirements.txt file what's expected from that point forward is you're going to create a new virtual environment you're going to activate it you're then going to install all of the different packages using the requirements.txt file again that command was pip install hyphen R requirements.txt now at this point you've learned how to create an environment activate it install individual packages install packages from a file and save your packages or dependencies into a requirements.txt file you pretty much know everything you need to about virtual environments but I'll share with you a few last helpful tips that you'll probably want to know so first of all it's common practice to create a virtual environment folder inside of the directory where your python files exist so for example maybe I'll make a new directory called project and this is where I'm going to have all of my code so I'll CD into project and maybe I have a python file so I can say maybe just touch main.py now what I would do is create my virtual environment inside of this directory so it's alongside my python files and all of my source code now it's common practice that we call this EnV or VV you can call it whatever you want but but typically this is what it will be named now once we create that and activate the environment we want to make sure we don't save any of our source code inside of that directory so I'm not going to do it right now but let's say that we have this ven V folder right so we have main up high and ven V where our virtual environment is we're not going to put any of our custom python code inside of here it's just going to sit inside of the main directory this folder is only used for storing the things related to our virtual environment that's about it if you're finished with the virtual environment and for some reason you need it anymore you can simply delete this folder and that's all you have to do so if you're on Mac you can type something like rm- RV EnV that's going to delete the directory you also can just simply find it and delete it so right click it move it to trash whatever you want now that's it that's all I have for you if you enjoy my teaching style and you want to become a developer then consider checking out my software development course from the link in the description leave a like on this video subscribe to the channel and I will see you in the next one oh
Info
Channel: Tech With Tim
Views: 53,207
Rating: undefined out of 5
Keywords: tech with tim, Python, programming tutorial, virtual environments, Python development, Python tips, coding tips, Tech with Tim, Python virtual environment tutorial, Python programming guide, Python environment setup, Python virtualenv, coding in Python, software development, Python projects, Python programming tips, coding tutorial, virtualenv tutorial, Python coding, coding for beginners, Python setup, Python development environment
Id: Y21OR1OPC9A
Channel Id: undefined
Length: 9min 5sec (545 seconds)
Published: Sat Mar 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.