5.1 Python Packages, Installation, Use

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

5.

Python Packages
Python Packages

We usually organize our files in different folders and subfolders based on some criteria, so that they
can be managed easily and efficiently. For example, we keep all our games in a Games folder and we
can even subcategorize according to the genre of the game or something like this. The same analogy is
followed by the packages in Python.

What is a Python Package?


Python modules may contain several classes, functions, variables, etc. whereas Python packages
contain several modules. In simpler terms, Package in Python is a folder that contains various modules
as files.

Creating Package

Let’s create a package in Python named mypckg that will contain two modules mod1 and mod2. To
create this module follow the below steps:
∙ Create a folder named mypckg.
∙ Inside this folder create an empty Python file i.e. __init__.py
∙ Then create two modules mod1 and mod2 in this folder.
Mod1.py

def gfg():
print("Welcome to GFG")

Mod2.py

def sum(a, b):


return a+b

Understanding __init__.py

__init__.py helps the Python interpreter recognize the folder as a package. It also specifies the
resources to be imported from the modules. If the __init__.py is empty this means that all the functions
of the modules will be imported. We can also specify the functions from each module to be made
available.
For example, we can also create the __init__.py file for the
above module as:
__init__.py

from .mod1 import gfg


from .mod2 import sum

This __init__.py will only allow the gfg and sum functions
from the mod1 and mod2 modules to be imported.

Import Modules from a Package


We can import these Python modules using the
from…import statement and the dot(.) operator.

Syntax:
import package_name.module_name
Example 1:
We will import the modules from the above-created package and will use the functions inside those
modules.

from mypckg import mod1


from mypckg import mod2

mod1.gfg()
res = mod2.sum(1, 2)
print(res)
Output:
Welcome to GFG3
Example 2:
We can also import the specific function also using the same syntax.

from mypckg.mod1 import gfg

from mypckg.mod2 import sum

gfg()

res = sum(1, 2)

print(res)

Output:
Welcome to GFG
3
Python PIP
What is PIP?
PIP is a package manager for Python packages, or modules if you like.
Note: If you have Python version 3.4 or later, PIP is included by default.

What is a Package?
A package contains all the files you need for a module.
Modules are Python code libraries you can include in your project.

Check if PIP is Installed


Navigate your command line to the location of Python's script directory, and type the following:

Example
Check PIP version:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip --version
Install PIP
If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/

Download a Package
Downloading a package is very easy.
Open the command line interface and tell PIP to download the package you want.
Navigate your command line to the location of Python's script directory, and type the following:
Example
Download a package named "camelcase":
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install camelcase
Now you have downloaded and installed your first package!
Using a Package
Once the package is installed, it is ready to use.

Import the "camelcase" package into your project.

Example
Import and use "camelcase":

import camelcase

c = camelcase.CamelCase()

txt = "hello world"

print(c.hump(txt))
Run Example »
Output:

Hello World
Find Packages
Find more packages at https://pypi.org/.

Remove a Package
Use the uninstall command to remove a package:
Example
Uninstall the package named "camelcase":
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip uninstall
camelcase
The PIP Package Manager will ask you to confirm that you want to remove the camelcase
package:
Uninstalling camelcase-02.1:
Would remove:
c:\users\Your Name\appdata\local\programs\python\python36-32\lib\site-packages\
camelcase-0.2-py3.6.egg-info
c:\users\Your Name\appdata\local\programs\python\python36-32\lib\site-packages\
camelcase\*
Proceed (y/n)?
Press y and the package will be removed.
List Packages
Use the list command to list all the packages installed on your system:
Example
List installed packages:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip list
Result:
Package Version
-----------------------
camelcase 0.2
mysql-connector 2.1.6
pip 18.1
pymongo 3.6.1
setuptools 39.0.1

You might also like