0% found this document useful (0 votes)
14 views19 pages

Python 2 - P4 - Module and Package PDF

Modules enable code reuse and organization in Python. There are built-in modules and user-defined modules. User-defined modules allow users to define their own classes, functions and variables. Modules can be imported and used in other Python programs. Packages are collections of modules and can be installed from the Python Package Index using pip. Virtual environments isolate package installations for different projects and allow different versions of the same package to be used.

Uploaded by

Lê Hà
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views19 pages

Python 2 - P4 - Module and Package PDF

Modules enable code reuse and organization in Python. There are built-in modules and user-defined modules. User-defined modules allow users to define their own classes, functions and variables. Modules can be imported and used in other Python programs. Packages are collections of modules and can be installed from the Python Package Index using pip. Virtual environments isolate package installations for different projects and allow different versions of the same package to be used.

Uploaded by

Lê Hà
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

PYTHON 2

MODULES & PACKAGES


TS. NGUYỄN HOÀNG LONG
BỘ MÔN TIN HỌC TRẮC ĐỊA
0916666384
nguyenhoanglong@humg.edu.vn
MODULES
• The module is a simple Python file that
contains collections of functions, classes and
global variables and with having a .py
extension file.
• Modules enable you to organize your code
into smaller pieces that are easier to manage.
• The code in the modules can be reloaded and
rerun as many times as needed, which enables
code reuse
• A separate namespace is defined by a module
that helps to avoid collisions between
identifiers
MODULES

MODULES

Built-in User-defined
modules modules
BUILT-IN MODULES
BUILT-IN MODULES
EXAMPLE
USER-DEFINED MODULES
CREATE A NEW MODULE
A module can contain:
• Definitions and implementation of classes,
• Variables
• Functions that can be used inside another
program
USING THE CREATED MODULE
HOW TO USE PYTHON MODULES
FROM MODULE IMPORT
HOW TO USE PYTHON MODULES
IMPORT MODULE
RENAME A PYTHON MODULE
PACKAGE

Self study
HOMEWORK

Develop your own Python package


PYTHON PACKAGES

• Python has an active supporting community of


contributors and users that also make their software
available for other Python developers to use under
open source license terms.

• This allows Python users to share and collaborate


effectively, benefiting from the solutions others have
already created to common (and sometimes even
rare!) problems, as well as potentially contributing
their own solutions to the common pool.
INSTALLING PYTHON
PACKAGES
python -m pip install SomePackage
or pip install SomePackage
or pip install SomePackage==1.0.4 # specific version
or pip install SomePackage>=1.0.4 # minimum version
VENV MODULE
• The original design of Python and its
packaging system puts installed packages
alongside the Python interpreter;
• Furthermore, only a single version of a given
package can be installed with a given Python
installation.
• This led to issues when different projects
required different versions of packages.
• To address this problem, Python virtual
environments (virtualenvs) were developed
• For example, if developers want to test Django
3.0 while on the system has Flask 1.0, they can
create new venv to install Flask 1.1.2
VENV MODULE
• Virtual environments let developer have a
stable, reproducible, and portable
environment.
• Developers are in control of which packages
versions are installed and when they are
upgraded.
• For an additional layer of control over when
developers update to new versions of
Python, developers can compile their own
Python interpreter and create a virtual
environment based on it.
CREATE NEW
VIRTUAL ENVIRONMENT
START VIRTUAL ENVIRONMENT
INSTALLING PYTHON MODULES INTO
THE VENV

pip list: list all the intalled modules inside the virtual env

You might also like