What are Virtual Environments in Python? - Python Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys hey going in this video I'm going to be explaining what Python virtual environments are why we need to use them and of course how to actually use them so essentially a virtual environment is an isolated Python environment for running your path on projects and technically speaking they are nothing more than simply just a directory that sits within your path on projects so they definitely aren't as complex as they may originally sing and just keep in mind that this tutorial is going to cover the recommended way to use virtual environments in Python 3.5 or above so basically it's not going to work for Python 2.7 so with that being said one of the main reasons to use Python virtual environments is to resolve problems that arise when you have multiple different projects on the same system that depend on and use different versions of the same package so I'm going to be showing you an example of this problem and of course we're going to fix it using virtual environments so as we can see right here in my directory structure I have two main directories here or folders I've got project a and I've got project B so essentially these two hypothetical projects are completely unrelated inside the main depay for project a we can see here that project a is importing requests okay so the requests package and also it requires requests aversion 1.2.3 or less to actually work correctly and the reason for this is because the get method has significant changes in version 2.2 or above and right here we've got a hypothetical example of what could happen in a real project and of course these things right here where functions or methods change their behavior is very common and one of the reasons why projects requires specific versions of a particular package so right here we've got a hypothetical example of the project right here requiring requests 1.2.3 or less okay so of course with this in mind we can go ahead and we can install the requests package at version 1.2 0.3 or less so this is done by saying pip install then requests at version less than or equal to one point two point three press enter and now of course pip is going to get requests at version 1.2 point three just like that so now if I was to run the people show then requests commands we can see here that we we indeed her version one point two point three of the requests package so now essentially the requirements for the main dot pipe or for project a have been satisfied we have the exact version of the requests package for this to work so now let's get inside project B and we can see here project B requires requests 2.2 or above so in this case we can see where the problem may arise or what's actually happening here and of course now if I was to try and run project B using my system it's not going to work because of course I have requested version one point two point three installed so of course if I keep show request once again it's version one point two point three so of course it's not going to work so in order for this to work I need to basically say you know people uninstall requests and then after this on it's actually ring install of a twist so keep in store request this time getting the most recent version which is actually the version are two point two two so now I've restored requests and of course now keep to show requests it's going to give me version 2.2 too so now of course project B is going to work but unfortunately project a is not going to work so that what there is one of the problems when you don't use a virtual environments so it can definitely be really difficult to make sure you have the correct versions of the package when having multiple projects so this being said let's solve this problem using virtual environments but first I want to uninstall the global installation of the requests package so I'm gonna say peak uninstall request and we're going to start with a fresh and clean slate so to create a virtual environment you want to go within your project a directory or in your project directory just like this and once we're inside the directory of the project inside the command line to create a virtual environment you simply say Python - M then you say be e and V and this right here is the command to create a virtual environment the next thing you type is the name of the directory for the virtual environment in this case I'm just gonna simply name it the same thing so Vee and V if I press ENTER right here we can see that we got a new directory inside the project called virtual environment or V E and V and that basically comes from our last string which we supply to the command so now if we inspect this directory we can see we have a few things going on but in particular inside the scripts directory and also this is going to be different on Mac or Linux UMaine said have a bin directory basically bin is the same as scripts it's just that it's called scripts on Windows so inside the scripts directory we can see here we've got our own version of Python right here so python exe and basically we can see that of course this right here is a separate environment for running Python okay and also more importantly inside Lib and then side packages we can see we have basically no packages installed for the virtual environment as of yet so essentially we've got this virtual environment a bunch of executables and a and a place to store packages so now we're going to basically go within this virtual environment and instead try to execute project a within the virtual environment instead and along with that we of course going to install the correct version of the requests library so let's just go down here to activate or go within your virtual environment you simply say and this is for Windows so for Windows you say VM just like this then you say scripts then you say activate just like this you press enter and now you can see that we have B env in brackets as a prefix to our command line prompts a different way to activate the virtual environment so I'll just go ahead and just actually deactivate it just like this and now you're back out of the virtual environment and I'll totally way to activate it on Windows is going to be them scripts and then activate dot bat press enter and the same thing happens well I believe that works on command line maybe not PowerShell but it definitely works for command line but anyway if you're using basically anything other than Windows so Linux or Mac the way you activate the virtual environment is going to simply be source vm bin and then activate just like this okay so that is how to do it on Linux or Mac so let's go back here and actually just activate the virtual environment so now that we inside the virtual environment we can go ahead and we can actually install the request library and our desired version so I can say right here hit install request then less than equal to one point two point three and press ENTER and now inside the lib and then site packages directory we can see we now have requests inside here so this is perfect so now inside the virtual environment if I was to say you know Python their main type I and run this we can see it works fine and also it's going to be using requests at version one point two point three if I say keep show requests just like this we can see of course we get one point two point three so this right here is your virtual environment and you can of course run your scripts inside here perfectly fine if I was to basically say deactivate here and go back to the normal or global mode and then try to rerun my project we can see it does not work because we have no requests package installed globally on the system outside of the virtual environment okay so essentially we can go now inside the project B directory and do the exact same thing so let's see D out of here and say CD Projekt underscore B and do the exact same thing so that'll be half on - M billion VM press enter and we get the exact same thing right here inside this directory and once it's done we can of course install keep installed requests at version 2.2 and the latest version okay you know what I forgot to go inside the virtual environment so let's pick an install request real quick here and activate the environment so there will be scripts for billion and then scripts and then activate just like this and then we can say pip install request once again and now we can try running the the main dot pi so python main dot pi and we can see it works perfectly fine peep show requests it's going to give us version 2.2 - so as we can see here we have solved the problem of the two projects having the same dependency with different versions while having virtual environments to execute our Python scripts within so now I do want to show you real quickly here an example of it working on Ubuntu so I've got this VirtualBox here with a Bluetooth server running on it and I'll just get out of the virtual environment here essentially right here of course we have this main dot pi so I can just say right here python - m vm and then be empty so the exact same thing it'll need to be passed on three for this particular system press enter and we can see it's loading but it works the exact same way and to activate it we say source and then the end been that source of being in between then activate just like this and now we can see we get the exact same thing right here vm is prefix on the command line okay so that is basically how to activate and use virtual in roms however I do want to show you one more thing and that involves the requirements dot txt file so essentially we're inside project B right now and if I want to give my friend or my mate this project to run on his own system he needs to know that you know he needs request version 2.2 and or above so if for that I need to basically tell him to install that library but of course if I had 50 libraries it's gonna be hard to tell him that so we can solve this by using what's called a requirements dot txt file and basically it just lists each package with their versions so for example to generate the Python sorry to generate the requirements dot text file you simply just say pip and then freeze and then in just a carrot or a you know greater than sign and then you just say requirements text just like this press ENTER and we can see inside project B we've got a new file and it's called requirements dot txt as we can see we've got a few things related to request itself but ultimately we've got rehear requests 2.2 - okay so of course now if I was to you know upload this to a git repository I would include the requirements dot text file and of course my friend can download this and once he's actually got the requirements that text file and it's within his virtual environment he can simply to install everything he could simply say pip install and then - our requirements got text just like this and when you press ENTER here it's basically going to go through all of these packages and install them at the version which is specified right here and that's just something to definitely consider especially if you want your project to survive in the future if you want to go back to it one day and try to use it or of course transporting it across you know the internet or on github for many people to use and download so that what there is virtual environments in Python I hope that made sense if anything did not make sense definitely comment below and just let me know ask me question and do all of that stuff so anyway thanks for watching guys and I'll see you later
Info
Channel: dcode
Views: 13,445
Rating: undefined out of 5
Keywords: code, coding, programming, tutorial, introduction, beginner, walkthrough, guide, software, development, simple, easy, english, with, example, examples, developer, lecture, recording, how, to, python, python3, venv, virtualenv, virtual, environments, pip, install, uninstall, windows, mac, linux, package, packages, library, libraries, requirements, txt, requirements.txt, global
Id: JWcrseHrh2w
Channel Id: undefined
Length: 14min 34sec (874 seconds)
Published: Mon Oct 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.