Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In addition to the modules that distribute with Python in the Python standard library there is also a repository of modules that have been written by other people. This is called PyPI, the Python Package Index, and it's available at this web page. We're going to click on the link here, Python 3 packages, so we can see what packages are available that support Python 3. Of course by the time you look at this, this list will probably be much longer because more-and-more modules are being imported to Python 3 all the time.
Just as an example of how to install a module that you find here, I've selected one called bitstring, and what this does is it allows you an easy way of working with strings of bits, and we'll go ahead and we'll download this and install it. And so this is the page and most of the pages look something like this. They've got some documentation. They have a link to the web site where it's been developed and a way to download a module.
And so I've already downloaded this and unpacked it on the computer here and there it is. Go ahead and change into that directory. And we can see that it's this Python file and a setup.py and some other related files. All of the modules that are available on PyPI have a setup.py and it's a standard, and so they all install pretty much the same way. Now I am installing this on Windows.
And the way that you install it on other operating systems is pretty much exactly the same. You run the setup.py script with the argument install. And on Windows, unless you've put Python in your path you'll have to type in the path to Python. On most other systems you won't need to do that. So I am just going to type in the path here. And I say python31, which is the standard place where it installs on a Windows system. And let's say python, and then I am going to give the name of the setup script, which is setup.py and the word install, like that.
And so if you are on a UNIX-based system like a Mac or Linux you probably just type something like Python 3 setup.py install. You do want to make sure that if you have both Python 3 and an earlier version of Python installed on your system that you are running the correct Python when you run the setup.py. If for example you were to run Python 2 and the setup and install, your module would be installed in the path for the Python 2 and not in the path for the Python 3.
So you want to make sure you are running the right Python when you do this. And so in this case that's by using this directory and then Python and setup.py and install. So I am going to go ahead and run that, and there it is it's built it and it's copied it into the site-packages directory, which is where all of your user installable packages are installed. And again if you are on a UNIX system or on any different operating system that will be in a different place, but it'll be called site-packages just like that.
Let me go ahead and change to that directory so we can see what happens here. So this is the site-packages directory on this system, and you see it just has this one module installed. And it installs this egg-info, which is just a lot of information about the module, and that's useful. It installs the .py file and then it also installs this .pyc file. What that is is a complied version of the module. So that when it's run it's actually a little bit more efficient, and this happens at install time.
So we'll go ahead and switch to Eclipse now and we'll take a look at how we use this. Make a working copy of modules.py. Call that modules-working.py. We'll open that up and instead of import sys here we are going to import bitstring, and we'll go ahead and use it. Set a variable.
Now I have already read the documentation from the module. That might be the first thing that you'll want to do. a = bitstring.BitString(bin = 01010101, and I want to put that in quotes because that's a string. So what this does is it allows you to find binary patterns using strings and then use those. We'll go ahead and print it in some different forms. a.hex, a.bin, a.unsigned integer, and we'll save that and run it.
And there we have successfully installed and used a module that we've found on the PyPI index. So the PyPI index is a very useful thing and you want to be careful of course because a lot of people submit modules and they are varying quality, so you want to do a little bit of vetting. Or you may just want to download some modules and look at their source code and learn how other people do things. It's a very valuable resource. And again it gives you the opportunity to leverage other people's work and to not be reinventing the wheel.
So do take a look at the Python index at PyPI and become familiar with what it is and how it works and add it to your quiver of tools.
Get unlimited access to all courses for just $25/month.Become a member