Python Tutorial: virtualenv and why you should use virtual environments

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody how's it going in this video I wanted to talk to you about virtual a and B so what is virtually NV virtual we and B is a way that you can separate different Python environments for different projects so why would you want to do something like this for example say that you have multiple projects and they all rely on a single package say it's flask or Django or something like that each one of these projects may be using a different version of Django or a different version of flask now if you go and upgrade that package and your global sites packages then it could break a couple of your websites that might not be what you want to do 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 version so that they needed and that's what virtual env allows us to do it allows us to make those different Python environments 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 it pip install virtual env now the first thing I'm going to do here is I'm going to do a pip list and you can see in my global site packages I have all these different packages all at a specific version now let's use virtually env to make a few different Python environments first I like to make a directory that is called environments to keep all these in one place and I'll go ahead and 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 one underscore env hit enter and you can see what this said here is that it was it went ahead and installed set up tools and pip for you so as soon as you go into that environment then you can use those to start installing packages so in order to activate this this new Python environment all you have to do is type in source and then the name of the environment we chose project one underscore env then go into bin and then activate hit enter and now we're in our new Python environment the way that you can tell this is it will now add this to your prompt even in when I clear out my prompt it still says project 1 e and V here at the top and that's one indicator that we are currently in that environment we can also type in which Python and if we type in which Python you can see that the path to the Python that we're using is within our environments and project 1 env slash bin slash Python I can do the same thing with pit and you can see that the pit that we're using is inside of our project 1 environment so now if I do a tip list here then you can see we only have pip and setup tools we don't have all those global packed site packages that were listed earlier whenever we are outside of this environment so let's go ahead and install a few packages into this new Python environment so there we have numpy installed and now I'll go ahead and install pi TZ and let me install one more just so that we have a few packages to work with I'll do PS util so now let's go ahead and do a clip list and you can see all the packages that was just installed in this environment now let's say I wanted to export all these packages and they're version numbers to use in another project with that I can just do pip freeze and let's do this tack tack local here and what this does is you can also use your global site packages within a virtual Python environment we didn't choose to do that but if we had then you have all those global sight packages available to us if you do a get freeze local then what that does is it takes only the local dependencies that you had in your Python environment 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 it cat on the requirements file you can see that we have all of our packages and version numbers there and now say that you wanted to get out of your Python environment you want to go back to being in the global environment all you have to do to get out of your virtual environment is to type in the activate 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 in my global environment also if I do a fit list then you can see that it goes back to all my global site packages so now you can see if I do an LS here we still have this project one environment what if I wanted to get rid of that all together I just wanted to 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 all you have to do is delete it so I can just do an RM RF type in project one environment there I do an LS then it's gone and that's it that's all it takes to get rid of a virtual environment so now we still have this requirements txt file here let's go ahead and make a nether or virtual environment but instead of just typing in the project name here let's do a pack P and what we can do here is we can specify a specific version of Python to use so let's do user bin and I'll do Python 2.60 and it 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 prod name so anyways yeah we have so we can specify the person Python that we want to use and now I have to put in a project name here so I'll just call this pipe to six underscore env so now if I do an LS you can see that we have our PI Q six underscore env and just like before we can do source and the name of our environment then and activate and now you can see that our prompt has changed and we have this pi to six underscore env up here in parenthesis if I do which Python then you can see that we are currently in this Python environment if I do a Python - - version and now you can see that we are in fact using this Python two point six point nine so with that requirements dot text file that we had earlier if we want to install those packages within this virtual environment we can just do a pip install tac are 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 packages here and all of the correct versions so at this point you could begin working on your project and 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 the activate and now you can see that our prompt has disappeared and we're back using our 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 if I do it an LS here our PI - 6 underscore env what these these virtual environments they are they're meant to be environments for your dependencies and your packages and things like that for your project but they're not actually for your project files you wouldn't want to go in and start building your project files within your within your virtual environment because you know you want to be able to pass these along and throw them away whenever you want so you don't want to have all of your project files in these virtual environments they're really just intended to separate out the packages dependencies and the versions that you're going to use from project project so with that said that pretty much touches on the basics of virtual environments and Python you know if you have any questions or if I left anything out feel free to ask in the comment section below be sure to subscribe for future tips and tutorials and thank you guys for watching
Info
Channel: Corey Schafer
Views: 594,967
Rating: 4.9526005 out of 5
Keywords: Python, virtualenv, Virtual Environment, Virtual Environments, Python Packages, Programming, Computer Science, Video Tutorial, How to, How-to, Software, Software Engineering, Development, Web Development, Web Dev, Python (Programming Language), Python virtualenv, Python Tutorial, Python Tutorials
Id: N5vscPTWKOk
Channel Id: undefined
Length: 9min 45sec (585 seconds)
Published: Mon Apr 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.