0% found this document useful (0 votes)
27 views

Unit 5

Uploaded by

berawor167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Unit 5

Uploaded by

berawor167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Modules,

Packages, and PIP


By
M. Bhavani
M-Tech
UGC-NET qualified
Modules

1. Module is a file containing Python definitions and statements, which can


be later imported and used when necessary.
2. If you want to import a module as a whole, you can do it using the import
module_name statement. Eg., import math
3. You are allowed to import more than one module at once using a
comma-separated list. Eg., import mod2, mod3, mod4
4. If a module is imported in the above manner and you want to access any
of its entities, you need to prefix the entity's name using dot notation.
Eg., print(math.floor(-124.12345))
Continued

5. You are allowed not only to import a module as a whole, but to import
only individual entities from it. In this case, the imported entities must
not be prefixed when used. Eg., from math import floor,
print(floor(-124.1245))
6. The most general form of the above statement allows you to import all
entities offered by a module. Eg., from math import *
7. You can change the name of the imported entity "on the fly" by using the
as phrase of the import. Eg., from math import abs as absolute
Working with Standard modules
1. A function named dir() can show you a list of the entities contained inside
an imported module

2. The math module couples more than 50 symbols (functions and


constants) that perform mathematical operations (like sine(), pow(),
factorial()) or providing important values (like π and the Euler symbol e).
3. The random module groups more than 60 entities designed to help you
use pseudo-random numbers. Don't forget the prefix "random", as there
is no such thing as a real random number when it comes to generating
them using the computer's algorithms.
Continued
4. The platform module contains about 70 functions which let you dive into
the underlaying layers of the OS and hardware. Using them allows you to
get to know more about the environment in which your code is executed.
5. Python Module Index (Python module is a community-driven directory
of modules available in the Python universe. If you want to find a module
fitting your needs, start your search there.
Modules and Packages

1. While a module is designed to couple together some related entities


such as functions, variables, or constants, a package is a container which
enables the coupling of several related modules under one common
name. Such a container 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.
2. During the very first import of the actual module, Python translates its
source code into a semi-compiled format stored inside the pyc files, and
deploys these files into the __pycache__ directory located in the module's
home directory.
3. If you want to tell your module's user that a particular entity should be
treated as private (i.e. not to be explicitly used outside the module) you
can mark its name with either the _ or __ prefix. Don't forget that this is
only a recommendation, not an order.
Continued

4. The names shabang, shebang, hasbang, poundbang, and hashpling


describe the digraph written as #!, used to instruct Unix-like OSs how
the Python source file should be launched. This convention has no effect
under MS Windows.
5. If you want convince Python that it should take into account a
non-standard package's directory, its name needs to be
inserted/appended into/to the import directory list stored in the path
variable contained in the sys module.
6. A Python file named __init__.py is implicitly run when a package
containing it is subject to import, and is used to initialize a package
and/or its sub-packages (if any). The file may be empty, but must not be
absent.
Python Package Installer (PIP)

1. A repository (or repo for short) designed to collect and share free Python
code exists and works under the name Python Package Index (PyPI). It is
available at https://pypi.org/
2. To make use of PyPI, a specialized tool has been created and its name is
pip (pip installs packages while pip stands for... ok, never mind). As pip
may not be deployed as a part of the standard Python installation, it is
possible that you will need to install it manually. Pip is a console tool.
3. To check pip's version one the following commands should be issued:

pip --version
Continued
The list of the main pip activities looks as follows:

1. pip help operation – shows a brief description of pip;


2. pip list – shows a list of the currently installed packages;
3. pip show package_name – shows package_name info including the
package's dependencies;
4. pip search anystring – searches through PyPI directories in order to find
packages whose names contain anystring;
5. pip install name – installs name system-wide (expect problems when you
don't have administrative rights);
6. pip install --user name – installs name for you only; no other platform
user will be able to use it;
7. pip install -U name – updates a previously installed package;
8. pip uninstall name – uninstalls a previously installed package.
LAB 13

1. Import a package in a python file that is place in a separate


folder, run it and check if __pycache__ file is generated.
2. Check pip version
3. Try installing flask package using pip
4. Check if flask is added to the pip list

You might also like