INSTALLING AND USING PACKAGES IN PYTHON (Beginner's Guide to Python Lesson 10)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this video I'm going to explain how you install and use packages within Python. In the previous video I explained how you create and activate a Python virtual environment. We're going to need to use that Python virtual environment in this video, so if you haven't done so already I recommend going back to the previous video and following it to the end so that you have your Python virtual environment setup and understand how to activate it. Okay so once that's done we're good to go. Let's start by installing our first package within Python. Load up the command prompt or terminal window and make sure that your virtual environment is activated. So as I explained in a previous video if you're using Windows you activate the virtual environment by typing env back slash scripts back slash activate and hit enter. If you're using a Mac or Linux then the command is source env forward slash bin forward slash activate and that will activate the environment for you on that operating system. Okay so we know that our virtual environment is activated because it has prefixed our command prompt input with the name of the virtual environment. Now we know we're good to install our first Python package. The way that you install a package within Python is you use the Python package manager called pip. It's very easy to use and the way that you use it to install a package is you type pip install and the name of the package you want to install. We're going to be installing a package called requests, so let's type pip install requests and hit enter. Requests is a very popular Python package and it allows you to make HTTP requests on the internet very easily within your Python code. It can take a while to install this dependency because it has to go and download the Python package from the internet and then set it up within our virtual environment. Okay so the package has been installed and now we can go ahead and create a simple Python script to test this package. So open up the Atom editor to our project and let's create a new file and let's call it requests underscore package.py. Alright so the way that you import a package within a Python script is you use the import keyword. You can put imports anywhere within the Python code however best practice suggests that you keep all of your Python imports at the top of your Python script. So let's use the import keyword to import a package called requests that we just installed. Alright so requests as I mentioned is a package that allows you to make HTTP requests on the Internet I'll link to the documentation in the notes of this video so you can go ahead and read and understand how to use requests in your own time if you want so for now we're just going to make a very basic call to request using the request get function. All it does is it gets the contents of a HTTP page and it stores it as an object in our script. The way you use it is you type the name of the object you want to store it in equals and then requests.get and then the URL that you want to retrieve the contents of. So just to test let's retrieve the contents of HTTP colon slash slash google.com alright so this will make a http get request to Google and it will store it in a requests response object. The way that you print the content of the object is you simply type print r.requests sorry dot content this will print the content of whatever was retrieved when we made this HTTP GET call. Alright so let's save that and let's go ahead and test our script by typing Python the name of the script requests underscore package dot py and hitting enter you can see that it's gone ahead and it has found the, it's retrieved the HTML contents from google.com and it is printed it to the screen here. Okay so we can see that it is successfully using this request package to retrieve the content of this page using the get function. The beauty behind this is that we didn't have to write all of the logic for accessing a page via HTTP we just simply installed the request package and then we build on top of that to get the page that we wanted. Alright so that's how you install and use packages within Python the next thing I'm going to show you is how you can store a set of dependencies for your particular project so what you can do with pip the pipe and package manager is you can use the pip freeze command. If you type pip freeze hit enter what this will do is it will output the currently installed packages and their version to the screen for your particular virtual environment that you're working on so you can see that there is one two three four five packages installed here and this is because we installed requests and requests had some other dependencies which it also installed for us automatically alright so let's say we want to capture this list of dependencies so we can share it with another developer so we wanted to shift our operating system and move to a Mac then we could do that by creating what we call a requirements.txt file. The requirements file lists all of the requirements for that specific project. The reason why you create this is because you shouldn't ever share or move this environment file to any other computer this Python virtual environment was created specifically for this machine so if we were to copy this to a different operating system it wouldn't work so the best practice is you create a new environment on any new machine that you work on and you install the packages from the requirements.txt file the way that you save the requirements.txt file is you create you equal pip freeze and then you use this angle arrow here and then the name of the file so we're going to call it requirements oops you got to put a space there I'm going to call it requirements.txt you can call the file anything however the best practice recommends that you call the file requirements.txt hit enter and you can see that it goes ahead you can see that it creates a list of all the dependencies and the version which we have installed. Ok so that's how you install and use packages within Python thank you so much for watching I hope you found this useful if you get stuck or you have any questions then please leave a comment in the comments below and I'll try my best to help you remember to subscribe to this channel for all future videos that we do on Python and other programming languages thanks for watching see you next video.
Info
Channel: London App Developer
Views: 47,620
Rating: undefined out of 5
Keywords: Python, learn python, learn how to code, python programming, python tutorial, python for beginners, python course
Id: Jok9ozKdO88
Channel Id: undefined
Length: 7min 54sec (474 seconds)
Published: Thu Dec 28 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.