5.8 Manage virtual environments - Video Tutorials & Practice Problems
Video duration:
12m
Play a video:
<v ->Okay, so now let's talk about virtual environments</v> and this is one of the more tricky parts about being a new Python developer is managing your environments because the ecosystem is not as good as in some other languages like Ruby and RubyGems or JavaScript, Node, and MPM. But the basic idea is if we wanna have multiple Python projects on our computer, for example, I wanna have a website and do some data analysis, but my website uses an older Python version, and I wanna use the latest and greatest features from the newest Python version for my data analysis. So you can do this just fine. You know, you can have Python and pip referred to Python 2.7 and you can have Python 3 and pip3 referred to Python 3.7. And so when you use, you can do pip install, Fabric, Requests, and Flask if you're doing your website. And you can do pip3 install, Numpy, Scikit learn, and Seaborn if you're doing your data analysis. And that's fine. But what if then, you want to upgrade your website from 2.7 to 3.7, but you don't want those packages to get mixed and you wanna still maintain both like 2.7 and 3.7 versions of your website? That's when virtual environments come into play so you can have different environments for your different projects and they can be using the same or different Python versions. So in this example, our old website was in Python 2.7 and we've downloaded and installed these libraries, these packages with and each of those have their own version, and it's for my website 1.0 project. But then you wanna upgrade your website to 2.0, you wanna upgrade your Python version to a more recent one, and then you want to use different packages from your previous ones, so now maybe we're moving onto Django, and we're still gonna use the Requests package, but an updated version of it. And then we can keep all of our data analysis stuff the same so this is like a process of creating a single virtual environment for each project and having them all self-contained. So then, once you have a virtual environment, you can then activate it for that project, and then do pip install, say django-emoji if you wanna start using emojis in your new website, and then it will target the right virtual environment, and add it to the appropriate place. So there are various tools for managing virtual environments. The most common one is using virtualenv to create those environments and pip. So once you've activated your virtual environment, you can then pip install, and things will be added to that environment. There is a newer kid in the game called pipenv and it's a simplified interface for and is a replacement for both virtualenv and pip. So it allows you to create virtual environments using a specified Python version and then also install things to that version and there's just like fewer commands to remember. And then you can also just use PyCharm. And so, I'll show you just these two in this sub lesson. So if we're using just PyCharm, we can go to PyCharm and then Preferences. So I can see these project interpreters. I've just got some the basic ones here so I wanna add a new one. And I can create a new interpreter from a Virtual Environment here. So I'm gonna create a new environment and I'm going to give it a location. So, let's say this virtual environment is just gonna be for all of our code in intro-to-python-master. So I'll put it at that location, but it also needs a folder to go in. So, venv is a common folder name. If you want it to be hidden, you can put a dot in front of it. And then we can choose a base interpreter so for our project, we wanna use 2.7, 3.6, or 3.7. Maybe let's just use 2.7 just for fun. And then we can leave these as is. Press OK and it's gonna create new virtual environment. And then, now that we're in here, we can see that there's a new project interpreter available and if we want to install something like... See if... If we wanna install something like Flask, we can do that here. Install Package. Okay and now it's inside of this virtual environment so we can apply this and then our whole project will be running under this version or we can go back to 3.7 and we don't have access to Flask. So this isn't gonna work right now, but then if we update to use our virtual environment that we created, actually Show All. Here, Existing environment. Pick the virtual environment here. Okay, Flask is still installed. Click OK. So now it's going to wait and now it's using the new version so Python 2.7. And this returns 0. So we know we're not on Python 3. So yeah, that's how you do it in PyCharm. Remember to change your interpreter back to 3.7 if you are doing any other part of this course. Let's look at it with using Terminal and pipenv. If you don't have pipenv yet, you can do pip or pip3 install pipenv. And then... I'm using this pipenv version. Cool, so I have it installed. First, we have to navigate to the project that we want to have a pip, I mean, virtual environment for. So, if you don't know any command lines stuff, I'll try to explain what's happening here. So ls is listing what's in the current folder that I'm in. I can change directory to my Code folder. I'll look what's in there. Okay, I've got this intro-to-python-livelessons-master here so I can change directory into there and then press Tab to autocomplete. And then, I've got my virtual environment here, but that was the one created with PyCharm. I wanna create a new one with pipenv. So I'm gonna say pipenv shell or no pipenv, let's see, help. So here are the things that I can do with pipenv. Create a new project using Python 3.7. Yeah sure, I wanna do that. So pipenv --python 3.7. If I wanted to do version, using 2.7, I can put that instead. So press Enter and then now, it will create a new virtual environment. Okay, and then now I can say pipenv install flask. And it will install it into that virtual environment. It also creates what's called a Pipfile and a Pipfile.lock. And if we look in here, now we have a Pipfile which has the packages that are installed and the Python version that's required. And Pipfile.lock is like, it's not really human readable, but it's to, so that pipenv can know whether things are up to date. Okay, and then if I want to activate it, I can say pipenv shell, and now I'm running from my virtual environment. And now when I say python, I'm using this Python 3.7 that's associated with this virtual environment. Okay, I can also say pipenv uninstall flask. Oh, and actually first before we do that, you can see in the Pipfile.lock, it has all of the other dependencies so click, itsdangerous, jinja2, et cetera, and certain versions of each of those. And then if I uninstall Flask, it's a little bit better than using PyCharm because it will uninstall everything that was installed with Flask. And now, it's removed from the Pipfile. So those are just a couple of the features in pipenv. I can also exit and now I'm no longer in my shell. And now, I can say python, and it's hard to tell, but this is actually a different version of Python than when I was in my virtual environment. Okay, so that's it for now. If you wanted to learn virtualenv and pip, there are lots of tutorials online. I found it a little cumbersome so I do prefer pipenv, but it is pretty new so there are some things that you can't do with it, but if you're just starting out, it's a great option for getting started with virtual environments.