Python Essentials 1:
Module 4
Modules, Packages and PIP
الكلية التطبيقية بجامعة اإلمام محمد بن
سعود اإلسالمية Dr. Ali Alwehaibi
Modules, Packages and PIP
In this lecture, you will learn about:
1. how to import and use Python modules;
2. how to create and use Python modules and packages;
3. how to process files.
Modules, Packages and PIP
What is a module?
• A file containing Python definitions and statements, which can
be later imported and used when necessary.
• You become a module user when you used an existing module
• You become a module supplier when you create a brand new
module.
• If you want to use any module, you need to know the name.
• A number of modules is delivered together with Python itself.
• The module consists of entities that are functions, variables,
constants, classes, and objects.
Modules, Packages and PIP
Importing a module-1
• To make a module usable, you must import it.
• You are allowed to import more than one module at once using a
comma-separated list.
• After the module is imported, you can access any of its entities
by prefixing the entity's name using dot notation. For example:
Modules, Packages and PIP
Importing a module-2
• To import only individual entities from a module, the imported
entities must not be prefixed when used.
• To import all entities offered by a module we use a star (*)
Modules, Packages and PIP
Importing a module-3
• You can change the name of the imported entity "on the fly" by
using the as phrase of the import. For example:
• Example for importing math module and using name alias
Modules, Packages and PIP
Working with standard modules
• A function named dir() can show you a list of the entities
contained inside an imported module
• Python language allows you to create and use module. (lab)
Modules, Packages and PIP
Packages
1. a package is a container which
enables the coupling of several
related modules under one
common name.
2. The package can be distributed
as-is (as a batch of files
deployed in a directory sub-
tree) or it can be packed inside
a zip file.
Modules, Packages and PIP
What is PyPI
• PyPI (it's short for Python Package Index) is a centralized
repository of all available software packages.
• The PyPI repository is maintained by a workgroup named the
Packaging Working Group
• Packaging Working Group task is to support Python developers
in efficient code dissemination.
• You can find their website here:
https://wiki.python.org/psf/PackagingWG.
• The PyPI website (administered by PWG) is located at the
address:
https://pypi.org/.
Modules, Packages and PIP
Python Package Installer
• Python Package Index (PyPI) although it's also likely that you
come across the very niche name The Cheese Shop.
• To make use of PyPI website, a specialized tool has been created
and its name is pip.
• Pip job is to install python packages.
• Some Python installations come with pip, some don’t.
• If the pip is installed on the system, you must install the pip on
your system following a proper way depending on the system
(windows, mac, ect).
Modules, Packages and PIP
Using Pip
• To check pip's version one the following commands should be
issued:
pip –version
or
pip3 –version
• If the previous commands don’t show the version, you need to
install pip on your OS.
Modules, Packages and PIP
Pip commands
• The list of the main pip activities looks as follows:
• pip help operation – shows a brief description of pip;
• pip list – shows a list of the currently installed packages;
• pip show package_name – shows package_name info
including the package's dependencies;
• pip search anystring – searches through PyPI directories in
order to find packages whose names contain anystring;
• pip install name – installs name system-wide (expect
problems when you don't have administrative rights);
• pip install --user name – installs name for you only; no other
platform user will be able to use it;
• pip install -U name – updates a previously installed package;
• pip uninstall name – uninstalls a previously installed
package
Modules, Packages and PIP
• Reference:
• Programming Essentials in Python by Cisco
Networking Academy
• (Edube Interactive :: Python Essentials 2 )
• Official Python Documentation and Tutorial
Modules, Packages and PIP