# Python Tutorial: virtualenv and why you should use virtual environments

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=N5vscPTWKOk
- **Дата:** 14.04.2015
- **Длительность:** 9:45
- **Просмотры:** 860,144

## Описание

In this video, we will be looking at virtualenv and why you should be using virtual environments in Python. Virtual Environments in Python allow us to keep project-specific dependencies in a separate place than our global site-packages. This is extremely useful when you have different versions of packages for different projects.


✅ 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

## Содержание

### [0:00](https://www.youtube.com/watch?v=N5vscPTWKOk) Intro

hey what's going on everybody how's it going in this video I wanted to talk to you about virtual EnV um so what is virtual EnV is a way that you can uh separate different python environments uh for different projects um so why would you want to do something like this um for example say that you have uh multiple projects um and they all rely on a single package say it's flask or D Jango or something like that um each one of these projects uh may be using a different version of Jango or flask um now if you go and upgrade that package and your Global uh size packages then it could break a couple of your websites um that might not be what you want to do um it would be better if each of those projects had an isolated environment where they had only the dependencies and the packages that they need and the specific versions that they needed and that's what virtual EnV allows us to do it allows us to make those different python environments um so let's go ahead and get started and I'll kind of show you how this works now I already have virtual EnV installed on my machine if you don't have it installed on your machine just do a pip install virtual EnV now the first thing I'm going to do here is a pip list and you can see in my Global site packages I have um all these different packages um all at a specific version now let's use Virtual EnV to

### [1:31](https://www.youtube.com/watch?v=N5vscPTWKOk&t=91s) Create a virtualenv

make a few different python environments first I like to make a directory um that is called environments uh to keep all these in one place and I'll go ahead and uh CD to that directory now if I do an LS here you can see that this directory is currently empty so to make our first virtual environment let's just type in Virtual EnV and I'm just going to call this one project 1or EnV hit enter and you can see what this said here is that it was it went ahead and installed setup tools and pip for you uh so as soon as you um go into that environment then you can use those to start installing packages so in order to activate this uh this new python environment all you have to do is type in source and then the name of the environment we chose project 1or EnV then go into bin and then activate hit enter and now we're in our new python environment uh the way that you can tell this is it will now uh add this to your prompt even in when I clear out my prompt it still says project one EnV here at the top and that's one indicator that we are currently um in that environment we can also type in um which Python and if we type in which python uh you can see that the path uh to the python that we're using is within our environments and project 1 EnV /bin SL python I can do the same thing uh with Pip and you can see that the PIP that we're using is inside of our project one environment so now if I do a pip list here then you can see we only have uh pip and setup tools we don't have all those uh Global pack site packages that uh were listed earlier whenever we were outside of this environment so let's go ahead and install a few packages into uh this new python environment so there we have numpy installed and now I'll go ahead and install pz and let me install one more just so that we uh have a few packages to work with I'll do PSU till so now let's go ahead and do a pip list and you can see all the uh packages that we just installed in this environment now let's say I wanted to um export all these packages and their version numbers to use in another project uh with that I can just do pip freeze and let's do this TCT Tac local here and what this does is uh you can also uh use your Global site packages within a uh virtual python environment um uh we didn't choose to do that but if we had then we would have all those global site packages available to us if you do a get freeze local then what that does is it takes only the local dependencies um that you had in your python environment now let's go ahead and output this to a requirements. txt file and let me do an LS to make sure we got that if we do a cat on the requirements file you can see that we have all of our packages and version numbers there and now say that you

### [4:55](https://www.youtube.com/watch?v=N5vscPTWKOk&t=295s) Activate virtualenv

wanted to get out of your python environment you want and go back uh to being in the global environment all you have to do to get out of your virtual environment is to type in deactivate and now you can see that our prompt no longer shows up if I type in which python then this is the python that I'm using uh in my global environment also if I do a pip list then you can see that it goes back to all my Global site packages so now you can see if I do

### [5:29](https://www.youtube.com/watch?v=N5vscPTWKOk&t=329s) Deactivate virtualenv

an LS here uh we still have this project one environment what if I wanted to get rid of that all together I just wanted to uh play around in a python environment for a little while to test something out and now I want to get rid of it well we already deactivated it and after you deactivate it uh all you have to do is uh delete it so I can just do an RM RF uh type in Project one environment there if I do an LS then it's gone and that's it that's all it takes to uh get rid of um a virtual environment so now we still have this requirements. txt file here um let's go ahead and make a another virtual environment um but instead of just typing in the project name here uh let's do a tacp and what we can do here is we can specify a specific version of uh python to use so let's do user bin and I'll do python 2. 6 oh and it uh looks like I did that path wrong let me go back here and put that in and that should do it oh sorry about that and that time I forgot to do the project name uh so anyways yeah we have so we can specify the version of python that we want to use and now I have to put in a project name here so I'll just call this uh Pi 26v so now if I do an LS you can see that we have our p26 EnV and just like before we can do source and the name of our environment bin and activate and now you can see that our prompt has changed and we have uh this p26 EnV up here in parenthesis if I do which python then you can see that we are currently in this uh python environment if I do a python D- version then now you can see that we are in fact using this python 2. 69 um so with that requirements. text file that we had earlier if we want to install those packages within um this virtual environment we can just do a pip install t r then requirements. txt and it should go out and grab all of those for us and now you can see if I do a pip list after those are installed then you can see that we have all of our P packages here and all of the uh correct versions so at this point you could begin working on your project um in whatever environment that you want and as soon as you are done working on that project or you want to switch projects you can just go to deactivate and now you can see that our prompt has disappeared and we're back uh using our uh Global installation of python and all of our Global packages I do want to point one thing out that I probably should have pointed out earlier um if I do an LS here our p26 EnV um what these virtual environments they are they're meant to be environments for uh your dependencies and your packages and uh things like that for your project but they're not actually for your project files you wouldn't want to go in and start uh building uh your project files Within your uh within your virtual environment because you know you want to be able to uh pass these along and throw them away uh whenever you want so you don't want to have all of your project files in these virtual environments they're really just intended uh to separate out the packages dependencies and the versions that you're going to use from Project to project so with that said that pretty much touches on the basics of virtual environments in Python um you know if you have any questions or if I left anything out uh feel free to ask in the comment section below um be sure to subscribe for future tips and tutorials and thank you guys for watching

---
*Источник: https://ekstraktznaniy.ru/video/12656*