Python Packages
By Mr. PRR
PRR TECHNOLOGIES                25-05-2021   1
                        Python Packages
• Modules are files containing Python statements and definitions, like function
   and class definitions.
• A package is basically a directory with Python files and a file with the name
   __init__.py. This means that every directory inside of the Python path,
   which contains a file named __init__.py, will be treated as a package by
   Python.
• It's possible to put several modules into a Package.
PRR TECHNOLOGIES                                                       25-05-2021   2
                   __init__.py
PRR TECHNOLOGIES           25-05-2021   3
                       Create A Package
•   Create a new folder named D:\MyApp.
•   Inside MyApp, create a subfolder with the name 'mypackage'.
•   Create an empty __init__.py file in the mypackage folder.
•   Create modules greet.py and functions.py
•   We have created our package called mypackage with two modules and one
    file.
PRR TECHNOLOGIES                                                  25-05-2021   4
PRR TECHNOLOGIES   25-05-2021   5
                       Mypackage files
greet.py                   functions.py        __init__ .py:
def SayHello(name):        def sum(x,y):       pass
   print("Hello ", name)     return x+y
                           def average(x,y):
                             return (x+y)/2
                           def power(x,y):
                             return x**y
PRR TECHNOLOGIES                                           25-05-2021   6
    Importing a Module from a Package
• We can import modules from package by using import keyword. Here we are
   Importing the functions module from the mypackage package and call its
   power() function.
• Ex:
• >>> from mypackage import functions
   >>> functions.power(3,2)
   9
PRR TECHNOLOGIES                                                   25-05-2021   7
       It is also possible to import specific
     functions from a module in the package.
>>> from mypackage.functions import sum
>>> sum(10,20)
30
>>> average(10,12)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
NameError: name 'average' is not defined
PRR TECHNOLOGIES                           25-05-2021   8
                                __init__.py
• Python defines two types of packages, regular packages and namespace packages.
• Regular packages are traditional packages as they existed in Python 3.2 and earlier.
• A regular package is typically implemented as a directory containing an __init__.py
   file.
• When a regular package is imported, this __init__.py file is implicitly executed, and
   the objects it defines are bound to names in the package’s namespace.
• The __init__.py file can contain the same Python code that any other module can
   contain, and Python will add some additional attributes to the module when it is
   imported.
PRR TECHNOLOGIES                                                             25-05-2021   9
                      Install a Package Globally
• Once a package is created, it can be installed for system-wide use by running the
  setup script. The script calls setup() function from the setuptools module.
• Let's install mypackage for system-wide use by running a setup script.
• Save the setup code as setup.py in the parent folder MyApp. The script calls the
  setup() function from the setuptools module. The setup() function takes various
  arguments such as name, version, author, list of dependencies, etc.
• The zip_safe argument defines whether the package is installed in compressed
  mode or regular mode.
   PRR TECHNOLOGIES                                                       25-05-2021   10
 setup.py
from setuptools import setup
setup(name='mypackage',
version='0.1',
description='Testing installation of Package',
url='#',
author='auth',
author_email='author@email.com',
license='MIT',
packages=['mypackage'],
zip_safe=False)
PRR TECHNOLOGIES                                 25-05-2021   11
• Now execute the following command to install mypackage using the pip
  utility. Ensure that the command prompt is in the parent folder, in this case
  D:\MyApp.
• D:\MyApp>pip install mypackage
  Processing d:\MyApp
  Installing collected packages: mypack
  Running setup.py install for mypack ... done
  Successfully installed mypackage-0.1
• Now mypackage is available for system-wide use and can be imported in any
  script or interpreter.
  PRR TECHNOLOGIES                                                       25-05-2021   12
Ex:
• D:\>python
   >>> import mypackage
   >>>mypackage.average(10,20)
   15.0
   >>>mypackage.power(10,2)
   100
PRR TECHNOLOGIES                 25-05-2021   13
               PyPI - Python Package Index
• PyPI is the default repository of Python packages for Python community
   that includes frameworks, tools and, libraries.
• Python developers can install and use packages in the Python application.
• It is open to all Python developers to consume and distribute their
   distributions.
• Developers can search or browse projects from pypi.org.
PRR TECHNOLOGIES                                                     25-05-2021   14
                               Install pip
• PIP has been included with Python installer since Python 3.4. You can verify
  whether the pip is installed on your machine by running the following command in
  your console:
    C:\Users\PRR TECHNOLOGIES> pip --version
    pip 20.2.3 from c:\users\prr
    technologies\appdata\local\programs\python\python39\lib\site-packages\pip
    (python 3.9)
 PRR TECHNOLOGIES                                                     25-05-2021   15
• If you are using an older version of pip, you can upgrade pip by running the
   following command on Windows:
     C:\> python -m pip install -U pip
• Execute the following command on Linux or Mac OS to upgrade pip:
     $ pip install -U pip
PRR TECHNOLOGIES                                                     25-05-2021   16
• If pip isn't already installed, then first try to bootstrap it from the standard library
  by executing the following command in your console or terminal window:
    python -m ensurepip --default-pip
• If that still doesn't install pip, the following steps should install pip on your
  platform.
    o Download get-pip.py from https://bootstrap.pypa.io/get-pip.py and save it to
       your local folder.
    o Navigate command prompt or terminal to the folder where you have
       downloaded the file and run the command: python get-pip.py
• This command will install pip in your pc. Additionally, it also installs the wheel and
  setuptools.
   PRR TECHNOLOGIES                                                           25-05-2021   17
                      pip Help command
• The pip help command is used to get a list of all the functions available in pip.
     C:\Users\PRR TECHNOLOGIES>pip help
     Usage:
       pip <command> [options]
     Commands:
       install                Install packages.
       download           Download packages.
     Etc…
PRR TECHNOLOGIES                                                              25-05-2021   18
                       Installing packages
• PyPI maintains packages as projects. Use the following pip command to install the
  latest version of the project.
    Pip install "project-name"
• Use the following command installs the specific version of the project:
    pip install "project-name==2.4"
• Use the following command to install a version that's "compatible" with a certain
  version:
    pip install "project-name~=2.4"
  PRR TECHNOLOGIES                                                      25-05-2021   19
                            List packages
• The list command can be used to see all the packages that have been installed on the
   system. If you want to check all the packages, use the pip list command:
     C:\Users\PRR TECHNOLOGIES>pip list
     Package Version
     ---------- -------
     cx-Oracle 8.1.0
     pip           20.2.3
     PRR            1.0
     setuptools 49.2.1
PRR TECHNOLOGIES                                                              25-05-2021   20
                              Show package
• If you want to check the metadata of a package, then use pip show command. The following
   command will display the metadata of the urllib3 package.
C:\Users\PRR TECHNOLOGIES>pip show cx-Oracle
Name: cx-Oracle
Version: 8.1.0
Summary: Python interface to Oracle
Home-page: https://oracle.github.io/python-cx_Oracle
Author: "Anthony Tuininga",
Author-email: "anthony.tuininga@gmail.com",
License: BSD License
Location: c:\users\prr technologies\appdata\local\programs\python\python39\lib\site-
packages
PRR TECHNOLOGIES                                                              25-05-2021   21
                   Uninstalling a Package
• The pip uninstall command can be used to remove a package. For example,
   if you want to remove urllib3 package, you can simply use the following
   command:
• $ pip uninstall urllib3
• The pip package manager will ask you to confirm if you want to uninstall the
   package. Proceed (y/n)?: If you press y, the package will be removed.
• Thus, you can use pip as a package manager of your Python application.
PRR TECHNOLOGIES                                                      25-05-2021   22