1
00:00:00,000 --> 00:00:03,990
In addition to the modules that distribute with Python in the Python

2
00:00:04,000 --> 00:00:08,990
standard library there is also a repository of modules that have been

3
00:00:09,000 --> 00:00:09,990
written by other people.

4
00:00:10,000 --> 00:00:15,990
This is called PyPI, the Python Package Index, and it's available at this web page.

5
00:00:16,000 --> 00:00:20,990
We're going to click on the link here, Python 3 packages, so we can see what

6
00:00:21,000 --> 00:00:22,990
packages are available that support Python 3.

7
00:00:23,000 --> 00:00:27,990
Of course by the time you look at this, this list will probably be much longer

8
00:00:28,000 --> 00:00:31,990
because more-and-more modules are being imported to Python 3 all the time.

9
00:00:32,000 --> 00:00:38,990
Just as an example of how to install a module that you find here, I've selected

10
00:00:39,000 --> 00:00:44,990
one called bitstring, and what this does is it allows you an easy way of working

11
00:00:45,000 --> 00:00:50,990
with strings of bits, and we'll go ahead and we'll download this and install it.

12
00:00:51,000 --> 00:00:53,990
And so this is the page and most of the pages look something like this.

13
00:00:54,000 --> 00:00:55,990
They've got some documentation.

14
00:00:56,000 --> 00:01:02,990
They have a link to the web site where it's been developed and a way to download a module.

15
00:01:03,000 --> 00:01:10,990
And so I've already downloaded this and unpacked it on the computer here and there it is.

16
00:01:11,000 --> 00:01:13,990
Go ahead and change into that directory.

17
00:01:14,000 --> 00:01:23,990
And we can see that it's this Python file and a setup.py and some other related files.

18
00:01:24,000 --> 00:01:28,990
All of the modules that are available on PyPI have a setup.py and it's a

19
00:01:29,000 --> 00:01:32,990
standard, and so they all install pretty much the same way.

20
00:01:33,000 --> 00:01:34,990
Now I am installing this on Windows.

21
00:01:35,000 --> 00:01:38,990
And the way that you install it on other operating systems is pretty

22
00:01:39,000 --> 00:01:40,990
much exactly the same.

23
00:01:41,000 --> 00:01:46,990
You run the setup.py script with the argument install.

24
00:01:47,000 --> 00:01:50,990
And on Windows, unless you've put Python in your path you'll have to type in

25
00:01:51,000 --> 00:01:52,990
the path to Python.

26
00:01:53,000 --> 00:01:54,990
On most other systems you won't need to do that.

27
00:01:55,000 --> 00:01:57,990
So I am just going to type in the path here.

28
00:01:58,000 --> 00:02:03,990
And I say python31, which is the standard place where it installs on a Windows system.

29
00:02:04,000 --> 00:02:08,990
And let's say python, and then I am going to give the name of the setup script,

30
00:02:09,000 --> 00:02:12,990
which is setup.py and the word install, like that.

31
00:02:13,000 --> 00:02:16,990
And so if you are on a UNIX-based system like a Mac or Linux you probably just

32
00:02:17,000 --> 00:02:22,990
type something like Python 3 setup.py install.

33
00:02:23,000 --> 00:02:26,990
You do want to make sure that if you have both Python 3 and an earlier version

34
00:02:27,000 --> 00:02:33,990
of Python installed on your system that you are running the correct Python when

35
00:02:34,000 --> 00:02:35,990
you run the setup.py.

36
00:02:36,000 --> 00:02:40,990
If for example you were to run Python 2 and the setup and install,

37
00:02:41,000 --> 00:02:43,990
your module would be installed in the path for the Python 2 and not in the

38
00:02:44,000 --> 00:02:44,990
path for the Python 3.

39
00:02:45,000 --> 00:02:47,990
So you want to make sure you are running the right Python when you do this.

40
00:02:48,000 --> 00:02:53,990
And so in this case that's by using this directory and then Python and

41
00:02:54,000 --> 00:02:55,990
setup.py and install.

42
00:02:56,000 --> 00:03:01,990
So I am going to go ahead and run that, and there it is it's built it and it's

43
00:03:02,000 --> 00:03:06,990
copied it into the site-packages directory, which is where all of your user

44
00:03:07,000 --> 00:03:09,990
installable packages are installed.

45
00:03:10,000 --> 00:03:12,990
And again if you are on a UNIX system or on any different operating system that

46
00:03:13,000 --> 00:03:17,990
will be in a different place, but it'll be called site-packages just like that.

47
00:03:18,000 --> 00:03:25,990
Let me go ahead and change to that directory so we can see what happens here.

48
00:03:26,000 --> 00:03:29,990
So this is the site-packages directory on this system, and you see it just has

49
00:03:30,000 --> 00:03:32,990
this one module installed.

50
00:03:33,000 --> 00:03:36,990
And it installs this egg-info, which is just a lot of information about the

51
00:03:37,000 --> 00:03:38,990
module, and that's useful.

52
00:03:39,000 --> 00:03:44,990
It installs the .py file and then it also installs this .pyc file.

53
00:03:45,000 --> 00:03:47,990
What that is is a complied version of the module.

54
00:03:48,000 --> 00:03:51,990
So that when it's run it's actually a little bit more efficient, and this

55
00:03:52,000 --> 00:03:52,990
happens at install time.

56
00:03:53,000 --> 00:03:59,990
So we'll go ahead and switch to Eclipse now and we'll take a look at how we use this.

57
00:04:00,000 --> 00:04:07,990
Make a working copy of modules.py. Call that modules-working.py.

58
00:04:08,000 --> 00:04:16,990
We'll open that up and instead of import sys here we are going to import

59
00:04:17,000 --> 00:04:24,990
bitstring, and we'll go ahead and use it. Set a variable.

60
00:04:25,000 --> 00:04:27,990
Now I have already read the documentation from the module.

61
00:04:28,000 --> 00:04:32,990
That might be the first thing that you'll want to do. a = bitstring.BitString(bin =

62
00:04:33,000 --> 00:04:41,990
01010101, and I want to put that in quotes because that's a string.

63
00:04:42,000 --> 00:04:45,990
So what this does is it allows you to find binary patterns using strings

64
00:04:46,000 --> 00:04:48,990
and then use those.

65
00:04:49,000 --> 00:04:57,990
We'll go ahead and print it in some different forms. a.hex, a.bin, a.unsigned

66
00:04:58,000 --> 00:05:02,990
integer, and we'll save that and run it.

67
00:05:03,000 --> 00:05:09,990
And there we have successfully installed and used a module that we've found on the PyPI index.

68
00:05:10,000 --> 00:05:14,990
So the PyPI index is a very useful thing and you want to be careful of course

69
00:05:15,000 --> 00:05:19,990
because a lot of people submit modules and they are varying quality, so you

70
00:05:20,000 --> 00:05:20,990
want to do a little bit of vetting.

71
00:05:21,000 --> 00:05:24,990
Or you may just want to download some modules and look at their source code and

72
00:05:25,000 --> 00:05:25,990
learn how other people do things.

73
00:05:26,000 --> 00:05:28,990
It's a very valuable resource.

74
00:05:29,000 --> 00:05:32,990
And again it gives you the opportunity to leverage other people's work and to

75
00:05:33,000 --> 00:05:34,990
not be reinventing the wheel.

76
00:05:35,000 --> 00:05:40,990
So do take a look at the Python index at PyPI and become familiar with what it

77
00:05:41,000 --> 00:05:51,000
is and how it works and add it to your quiver of tools.

