5.7 Import external libraries - Video Tutorials & Practice Problems
Video duration:
8m
Play a video:
<v ->While the Python standard libraries</v> does come with quite a few libraries already installed chances are at some point you're going to want to install your own libraries. So for example in lesson seven when we create our web app we will want to install the flask library so that we can actually build our website. There are a lot of different libraries, for example for Scientific computing packages we will be looking at these in, or we'll be looking at like a small subset of these in lesson six when we do some data analysis. And there's also something called the Python Package Index, or PyPI and this is a big repository of libraries that are available to download using what's called pip. And I'm gonna show you how to use pip and I'm also gonna show you how to download libraries just using PyCharm. So if you're curious about tutorials for finding and installing and uninstalling python packages using pip this is a great tutorial. I'm just gonna show a very brief overview but yeah there's like a whole art of even finding what libraries are available for download. So just to quickly show you this is PyPI the location that stores a whole bunch of Python libraries. You can search for projects here. So if I search for flask I can see here's flask and also a whole bunch of other things related to flask or that have flask in the name. There's 168 pages. So all of these are available to download to your computer written by people of all experience levels. You can write your own package that you then put on PyPI and other people can download. And now I'll show you how to actually download and install things onto your own computer. So the first way that I'm gonna show you is just using PyCharm. So if you go back into the settings. So remember with, on Windows it'll be File and then settings. So I'm gonna go to Preferences. Then inside of the Project Interpreter for your project here are a bunch of packages that are already listed that have been downloaded. Some of these just come with Python already, so we can install things by clicking this plus button and then searching, and I'm gonna download flask. So, Install Packages or Package. And then Package flask Installed successfully. Then I can close this. And now there's a whole bunch more packages here including Flask, Click, Jinja2, MarkupSafe, Werkzeug. And that's because flask has some dependencies. It relies on some other packages that needed to be downloaded, in order for its download to work. So I can click OK and then now if I open up my scratch.py file I can say from flask, import flask, and do whatever I want. So now its available for me to use. And lets just make sure it's working, I'll run this. Yes, and it gives me a bunch of documentation for the flask object class. So that's the first way of installing them and that's, I wanted to show you in case you are not comfortable with the command line. But if you do wanna use the command line this is the method that is way more commonly used. I don't know how many like proper developers actually use the PyCharm interface. You should learn this at some point. So in the command line or terminal I can say python, dash dash, version again, and that's 3.7.1. Python three, dash dash, version. So on your computer you might have this as Python two and this as Python three. Yeah, similarly we can check out pip. So pip is a tool that we can use to download libraries for Python. So if I look at pip version I can see that pip is it's actually found in Anaconda and it's using Python 3.7. If I look at pip3 this is in a different location. It's in my version 3.6. Oh interesting. So if I use pip3 then it's going to download into this location. So I'll just show you what it looks like to use pip. I'm gonna use pip3. Install flask. And now it's doing some stuff. And it finished successfully. If I try to do pip3 install, something that didn't exist. Could not find a version, satisfies the requirement. And then it also gives me this handy little command here that's telling me my pip is out of date. So I'll do that as well. So then if I wanted to uninstall flask I can do that, and uninstall. And then, now flask is not available. So there are a lot more functions that pip can do and actually in my case my pip3 was pointing to the wrong Python version. But what I'm gonna show you in the next sub lesson is like managing virtual environments and that actually doesn't matter too much and that's why I haven't noticed that it's a problem on my computer. Because when you use pip it will install that library for any project that is using that Python version. Where as you wanna start getting into the habit at some point of using virtual environments so that you can isolate your projects and their requirements. And actually before we go onto the next lesson I'm gonna just show you how to remove installations from PyCharm, and I can click on it, say minus, and now it's gone. Unfortunately though it doesn't delete all of the other things that were downloaded when flask was installed, so I have to kinda know what the dependencies are and delete those individually. Great, so that's it.