Python Tutorial: VENV (Windows) - How to Use Virtual Environments with the Built-In venv Module
17:09

Python Tutorial: VENV (Windows) - How to Use Virtual Environments with the Built-In venv Module

Corey Schafer 02.04.2019 600 751 просмотров 12 550 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Python Programming Tutorial, we will be learning how to use virtual environments on the Windows operating systems with the built-in venv module. We will learn how to create them, activate them, remove them, and much more. Let's get started... VENV (Mac & Linux) - https://youtu.be/Kg1Yvry_Ydk ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python #venv

Оглавление (4 сегментов)

Segment 1 (00:00 - 05:00)

hey there how's it going everybody in this video we're going to be learning how to use the built-in VV module from the standard library to work with virtual environments now I've done a couple of videos on different ways to manage virtual environments I have other videos on Virtual EnV and pip EnV but this buil-in V EnV module is actually the one that I'm currently using most often so I wanted to show how to do this also my virtual EnV video is getting pretty old at this point and when I made that video I only showed how to use Virtual EnV on Mac and Linux this time around I'm to make videos for Windows and Mac and Linux so for this video we'll be learning how to use this on Windows but if you're on Mac and Linux then I'll be sure to leave a link to that video in the description section below okay so with that said let's go ahead and get started okay so first of all why would we use a virtual environment so the purpose of a virtual environment is to have a space where we can install packages that are specific to a certain project so for example let's say that you have a lot of D Jango sites that use Django version one but you want to start using Jango version 2 on all of your newer projects well if you're just using your single global environment then when you update Jango to version two it could break some of your old projects that use Jango version one so we don't want all of our projects pointing to a single instance of D Jango so each project should have its own packages separate from each other and that's what we use Virtual environments for so with that said let's look at how we can use the built-in V andv module to create virtual environments now in order to use this module you're going to need to use Python 3. 3 or higher I'm going to be using python 3. 7 but anything over 3. 3 should be good now I have my command prompt here open on my desktop uh so let's create a new environment to see what this looks like and actually I'm in my home folder here so let me actually uh CD to my desktop okay so now I'm on the desktop and now uh let's see what it looks like to create a new virtual environment now unlike virtual EnV or pip EnV you don't need to install anything to use this it already comes with the standard library now first let me do a pip list to show you the modules that I have installed with my system installation of python so if I say pip list then we can see a list of packages here and these are all the packages that I have installed on my system installation of python so now let's create a new virtual environment uh so to create a new virtual environment I can simply say python DM venv and now the name of our virtual environment so I'm just going to call this projector EnV so when you run- m and then specify a module like we did here uh python will search your sis. path and execute that module as the main module so this VV module expects the environment name that you want to create and then it will go out and create that for you uh so uh now we should have a new environment uh called projector EnV so if I list out everything on my desktop by using di then we can see that now we have this virtual environment called projector uh EnV so it creates that in our current directory so to activate that we can simply say Let Me Clear My screen here to activate this we can just say projector EnV which was the name of my project and then script and then activate dobat so if I run that then we can tell that our environment is activated because you'll likely see the environment name in parenthesis here in our command line uh another way that you can tell this is by typing where python spell that right and when we run that it gives us the paths to the current python command and our virtual environment directory here is listed uh at the top of that now the version of python that it's going to use in the environment is going to be the same version of python that you use to create the environment so since I used python 37 to create the environment then this environment is also going to use Python 37 now if you need to use a different version of python then you'll need to use something like virtual EnV instead but honestly I don't really have the need uh much at all to do that anymore so it's not really something I've even noticed so now within this environment now that we have this activated let me do a pip list so we can see that all we have installed within this environment is pip and setup tools so now if you install some packages here then they will only be installed for this environment so let's say that our project requires uh the request library and pz so to do that I could simply say pip install requests and once that is installed then I can just hit the up Arrow Here and Now do a pip install pyz and we'll install that

Segment 2 (05:00 - 10:00)

and then once this is installed we will run pip list again okay so first I'll clear my screen and now I'll do a pip list so it installed all of those uh packages within our virtual environment now all these other envir or packages here are just uh dependencies of those other packages that we installed now if you ever want to export the packages that you're using for a specific environment the way that we can do this in python is with a requirements. txt file this will allow someone else to create an environment and use your requirements. txt file to install all of the same packages and dependencies that you're using so to do that we can use the PIP freeze command so I'm going to say pip freeze and that's going to be similar to pip list but it will give us the packages in the correct form for the requirements. txt file so you can take that output and uh just put that output into a requirements. txt file so I'm going to create a requirements. txt file on my desktop and put the information from pip freeze inside of there now I could do a redirect of the output in my command prompt and create the file that way but I ran into this weird uh bug where it was changing the color of my prompt when I tried that so I'm just going to do this manually so I'm going to uh copy the output from this pip freeze here and whoops let me looks like I accidentally pasted that let me do that one more time and just do a pip freeze okay so just let me copy this here and I'm going to create a new file on my desktop and this is just going to be a text file this is going to be requirements. txt I'll open that up in Sublime and now I'm going to paste in the output uh from pip freeze into that file and now we uh should have our requirements. txt file and our current directory with the same contents of as that pip freeze command okay so now let's deactivate our environment so I'm going to bring back up my command prompt here and to deactivate your environment it's as easy as saying deactivate and when we run that we can see that we no longer have the environment in our prompt and if you want to delete the virtual environment Al together so it's no longer active uh but it still exists if we want to delete the environment altogether it's as simply as deleting the directory for that environment and on Windows you can do this in the command line simply by saying RM dur uh and then the name of the directory that you want to delete and then we're also going to do a for SLS here uh to tell it that uh even if this isn't empty we want to delete it and actually more specifically that for SLS will also delete uh the entire tree so it'll Del subdirectories and things like that as well okay so now that environment should be deleted now if you're not comfortable with using the command line for stuff like this then you can simply delete the directory by dragging it to the trash bin if you'd like to do that instead now normally when I create a virtual environment I usually create it inside of my project and I name the environment venv and that's a pretty common convention now the reason that I called it something different before was because I wanted to differentiate between the VV module and the name of the environment but normally I would do something uh like this I would create a new project so in this case I'll just create a directory called my project so MK dur myor project and then I would create a virtual environment inside of that project called VV so I'll say python DM venv to run this VV module and now the name of our environment now I want to put this inside of my project so I'll say my project V and sometimes these virtual environments will uh take a second to create but now once that is created okay so that finished so now to activate that I'm simply going to say mycore project venv which is the name of the virtual environment scripts and then activate dobat and this will activate that virtual environment like we saw before and now that environment is active now remember we created a requirements. txt file from our last environment now if we want to create a new environment and use a requirements. txt file to install our packages then we can do that by saying pip install dasr and now since we're using that- R option it's going to be in expecting a require re. txt file so that

Segment 3 (10:00 - 15:00)

is just located here on my desktop so I'm just going to pass that in directly so I'll say requirements. txt and I will run that and it's going to go out and install everything from that requirements. txt file and all of the dependencies that we had listed so once all of these are installed I'm going to let this finish here okay clear my screen so now if I do a pip list then we should see the same installations that we made in our last environment okay now uh one other thing that I should mention is that it's common to put your virtual environment inside of your project folder but you don't want to put any of your project files into the virtual environment so let me show you what I mean by this so first I'm going to CD into my new project so I'm going to say CD into my project now when we create files and directories within our project we would never put them inside of the VV directory so let's say that I create a simple script within this project so I would just create it and put it in the root of this directory so let me create a script within my project so I'm going to minimize uh the command line here I'm going to open up my project which is just on my desktop and now I'm going to uh within my project here just create a new file and this is just going to be script and instead of txt I'll do script. py and say yes there okay so back within my command line here if I do a DI within my project then we can see that now we have script. py and then our virtual environment so now we have a script but we didn't put anything in that virtual environment the environment should be something that can be thrown away and rebuilt so you don't want to put any of your project files in there now another thing is that you shouldn't commit your virtual environment to Source control so if you ever look at a template get ignore file for python projects then they usually have virtual environments ignored which means they won't be committed to Source control uh now uh what you would commit to Source control would be something like your requirements. txt file which lets people build their own environments to run your project but there's no need to add the environment itself okay so now let me deactivate this environment so we'll just say deactivate like we saw before and clear my screen again now the last thing that I want to show you is how to create an environment with access to these system packages so if I do a pip list within our uh system installation of python then these are the system packages that I have installed on my Global version of python and we saw these earlier so there is a way that we can create a virtual environment that has access to these now I hardly ever do this uh anytime I need packages that are installed GL I usually just create a requirements. txt file instead uh but there must be enough people who request this functionality that they made it possible so let's go ahead and see how to do this so first I'm going to delete the environment that we just created so to delete that we saw this before but I'll say RM dur and that was called venv and also the for SLS to delete those subdirectories as well say yes we are sure that we want to delete that okay so Now to create an environment that has access to those system packages we can say python DM V EnV which is the module name the name of our environment is also going to be venv and now we can add in this option here uh two for slashes system- site- packages and sorry that's running on the next line there I want this text to be large enough to where you all can see it but sometimes it makes it a little uh hard to read because it goes on multiple lines but this is system- site- packages so if I run that then that is going to create that environment with our system packages and once that is created we can activate that by saying V andv which is the name of our environment uh scripts and then activate let me spell that right activate dobat so we can see that environment is now active and let me clear my screen here if I do a pip list whoops let me spell that right pip list then we can see that all of those uh system packages are also available inside of this virtual environment now the additional packages that we install in this environment still won't affect these systems packages so let's see a package that we don't have here so I don't have uh SQL Alchemy in my Global installation of python so let's install that so I'll say pip install SQL Alchemy and once

Segment 4 (15:00 - 17:00)

that is installed uh then it's only going to be installed in our environment okay so once that is installed if I do a pip list then we can see that now we have SQL Alchemy installed here um but it's also possible to only list the packages that we've installed in this environment so this is everything including our uh system packages but if I only want what we've installed in this environment then and I could say pip list-- local and when I do that we can see that now we just have Pip setup tools and SQL Alchemy and just to show you that didn't install in the uh system packages if I do a deactivate and then do a pip list then we can see that SQL Alchemy did not get installed on our system packages or for our Global version of python so that's good that's exactly what we wanted now that-- loc command also works for PR pip freeze as well so if you ran pip freeze --lc then it would only give you the packages that you installed in that environment as well okay so I think that is going to do it for this video I hopefully now you have a pretty good idea for how you can use the built-in VV module to create and manage your virtual environments so this is personally what I've been using the most lately it doesn't have as many bells and whistles as virtual Envy or pippy Envy but I just find it really simple to use and it's great for what I need 99% of the time so if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to Simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that in the description section below be sure to subscribe for future videos and thank you all for watching oh

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник