Now you when install Python what you do is you make available the Python built-in code.
An example of this built-in code would be the print function, so let me
open a command window here and call Python.
So when you say print, your execute and everything works perfectly, so where did
you get this print function from? Well, when you a start a Python session like here
in the interactive shell or when you run a script, a Python script the session
loads together with a bunch of built-in
functions and methods and this is one example, so the print function. Now there
are also commands that are not loaded when Python loads and these are
considered to be less frequently used by programmers, so it has been judged that
it's not a good idea to load everything into memory when you load Python, so the
solution here, the workaround is that you can load these commands explicitly
yourself when you need them and to do that you use the import keyword. So let's
say you want to concatenate, so to merge a bunch of text files that you have in
directory, in a folder. So you want to merge the text in one single file.
What you need first is you need to create maybe a Python list where you
have as items you have all the file paths that you want to concatenate.
And to generate a list of file paths in the current directory
you'd need a module called OS, so you need to import OS because you know if you
just do os.listdir which is a function that lists the file paths,
you'd get an error. It says name OS is not defined, so what you do is you
import OS and that is successful if you don't get an error. And then OS.listdir.
And yeah, you get an empty list because I have nothing in here at the
moment, so I'm going to create a file. Sample.txt and now you get a list
with this file name, so what you did is accessed the listdir method
of the OS module and you can see a list of available methods that you can apply to
your OS module so dir os and yeah. Among them you'll find listdir which
is here, so just here and yeah don't get scared about this entire code. You don't
have to know these. I mean I don't know most of them. I've just used only a few
of them when the need comes to do something you just try to google and you
you'll have people who have had the same issue, so you get the answer in some
forums like StackOverflow and you'll see a what other people have used.
Yeah, this is called introspection, so when you inspect your code, when you see
information about the code like we did here with a dir function.
And yeah OS is a module. We also have a libraries and packages which basically are the
same thing, but I'll go through them one by one, so let's focus on modules first.
And a module is just a Python file. A dot py file, so if you apply the file method
with double underscore in front and on the back, you'll get the path of the
module, so you can see here the module name is OS, so the file name is
os.py and you can go ahead and locate that file now. I think it should
be this one here. Yeah, here it is, so this is my Python installation and you have a
Lib folder in there and just inside that you have all the external modules
that you can import in Python, so you can try any of them if you like. Like import pickle.
pAnd if you don't get an error that is successfully imported, and you
get also ask for help, pickle, to see what the module is about, so create portable
serialized presentations of Python objects. And this particular module is
used for network programming so if you're interested you can look at that.
And yeah if you, you can open this file with any kind of editor any yeah, this is quite scary,
you don't have to understand this code. This will do the work on the
background, but is just good for your information to know what a module is.
All right. Now there's also the concept of libraries, and libraries that have
usually more code, so people who build them they just decided to write the
code into different files instead of just one dot py file.
So you have a collection of dot py files in four libraries, however the way
you use a libraries in Python is the same, so let me locate a library here.
So we're still in the Lib folder and all these folders that you see now are libraries
for instance sqlite3. If you go ahead and say import sqlite3, and that is
successful, so you do the same thing as you did with modules and if you look at
sqlite3, you'll see a bunch of Python files as I said and here you have files.
as well. You don't have to know this but basically what happens is when you import
a library, this init.py file is executed in Python and this contains the
code that loads on all the available methods in Python. Now this
particular one, what this does is it imports yet another file into Python.
So module sqlite3.dbapi2 which is this one here, so it refers to sqlite3, the folder name.
And then a dot, and then dbapi2, the name of the file, of the Python file that is
imported and then inside here you have other code and so on. You don't have to
know this. It's enough to understand the structure and yeah,
know that's about libraries and modules. These modules and libraries they
come with your Python installation by default.
However they don't do every possible thing that is out there.
That's why third-party modules and libraries exist and these are often
referred to as packages. Now these packages they don't come with the
default installation of Python and the good reason for that is there are lots and
lots of packages out there and you don't want to get a heavy installation there
on your computer by default. There are packages of biology and astronomy,
web scraping, everything, and so there are things that you you're not really
interested, and yeah, the solution to that is you install these packages.
And you use them just as you do with modules and libraries and the way to install them
is you just open your terminal, so command line on windows, terminal
on Mac and Linux, the directory doesn't matter. All you do is you say pip
install, let's install glob2, so this is a package or you can refer to it as a
third-party library and you hear people referring to packages as third party
libraries and yeah this installs glob2 for you. Now you can go ahead and
access it in any Python session. Glob2 and yeah, you're done. Similarly you can ask
for help for that package, so help glob2. And yeah, you see what this is about.
Sometimes though they don't say what they're about in the help file, so you'll
have to do search on the internet for glob2. And every package has its own
page almost. Glob2 just like that. Yeah, you go here, so what this does
is it capturs patterns and supports recursive with course and basically what
these do is you know, you say glob2.glob. Yeah, you need to pass a file path
there, sorry. And yeah, what this does is when you pass
a * there it lists all the files in there, but if you are only the text files,
you'd want to say *.txt 360 and it gives you every fall that as a txt extension so in
this case you have this particular file in that directory. This is useful
over a listdir because you know if you want to only get access the txt files,
you'd want to do this because in a directory you may have lots
of files and this allows you to access only a few of them and yeah, now about pip.
So you sow me use this pip package. Actually pip is a library that comes
installed with Python and you can find it in the scripts folder, pip yeah.
So pip.exe is for Windows and pip is normally in the computer search path which
underlines that you know when you are in your terminal and you do pip,
your computer knows where to get pip from so it gets to this pip.exe and pip then,
what pip does is it downloads the package and once it downloads it,
it installs it, so if you go up here, it says
pip install glob2, downloading and then installing the package and then the packages is there,
in your system and you can use it as you do with modules and libraries. So to sum it up
modules are Python scripts that you can import in Python,
and libraries are collections of Python scripts and you can import libraries
as well, and also packages are third-party libraries, so three different terms are
there, but library, you can use the term library as a general term for anything
that is imported in Python. You know just for the sake of simplicity people
do this, so that's about this lecture and you could even make it through without
knowing this information that you got from this lecture, but it's good to
understand these things as you can also understand that and be able to fix problems
that you might get with modules and libraries or packages.
So yeah if some things look a little unclear for you, just drop a question.
I'll talk to you in the next lecture.