If you have a Python Module that you want to package it and re-use it over and over again, especially if you have a cloud instance and you need to deploy your Python Package (a.k.a. pip install
) onto your cloud instance, this post may be useful for you. Let’s take a look how to package the Python Module and how you can pip install
it to your system or to your cloud instance.
Table of Contents
Learn Python from the Books
You want to learn more about Python, how to make a package in Python, check out the below book at Amazon.
Preparation of Python Module Package
Python Module Package needs a following structure:
root directory
|
|-- setup.py
|-- module directory
|
|-- __init__.py
|-- module_a.py
|-- module_b.py
Configure the setup.py
setup.py
needs to contain the following
from setuptools import setup
setup(name='techcookbook',
version='0.1',
description='TechCookBook Python Packge',
url='https://techcookbook.com',
author='@yuki',
author_email='',
license='MIT',
packages=['techcookbook'],
install_requires=
[
'PyMySQL'
],
zip_safe=False)
Basically, it includes the basic information about the package as well as dependent libraries. In the above setup.py
for example, I included the PyMySQL. This means, one of my modules will need this library (and it will be used) When you declare this library in the setup.py
, when you install your Python package, it will automatically install the dependent libraries listed in install_requires
parameter.
Configure the __init__.py
In the __init__.py
under module directory, you need to declare all the modules will be included in the package. For example, if you have module_a
and module_b
as part of the package. The __init__.py
file will look like this.
from module_a import *
from module_b import *
To give you more example, let’s say you have a module (a filename) called, crud.py
. crud.py
has functions will be used for CRUD database operation. In this case, your module name will be crud
. So you need to import * (asterisk — all the functions) from the crud module (.py file). It will look like below if you add it in your __init__.py
.
from crud import *
Making Modules
Next is let’s create a couple of modules. For this exercise purposes, let’s create a module where it does the database CRUD operation. Let’s name this crud.py
. And other one is it will convert the strings to all lower case or all upper case and let’s name this as caseconvert.py
crud.py Module
crud module will have the following functions in its .py file.
- connection()
- close_connection()
- create_record()
- read_record()
- update_record()
- delete_record()
The .py file will look something like below:
caseconvert.py Module
caseconvert module will have the following functions in its .py file.
- to_lowercase()
- to_uppercase()
The .py file will look something like below:
Packing the Modules
Now we have 2 modules. Let’s package them and try to install in the system.
There are couple of ways to host the Python Package to install. The easiest one is installing from your local machine using zip file.
You just simply need to zip all the files under root directory into one zip file. If you have the following file structure, zip the setup.py
and techcookbook
directory together in one zip file. Zip filename can be anything but it will be ideal to use the same name as your package name. In this case, I would name the zip file as techcookbook.zip
|-- setup.py
|-- techcookbook (directory)
|
|-- __init__.py
|-- crud.py
|-- caseconvert.py
Install Python Package via ZIP file
You can now try installing the Python Package you created in your local machine or in the VirtualEnv. (Please refer to How to use VirtualEnv in Python post)
pip install Python package
Installing your Python package is same as how you install the standard libraries. It is just you need to specify the path to zip file in this case. Try something like below:
pip install <path_and_zip_filename>
So if you have techcookbook.zip, you can do pip install techcookbook.zip
(assuming your zip file is located same location as your current location)
If you pip list, you should see the Python package you created is being installed as well as the dependent package(s).
If you want to learn more about Python, check out the books at Amazon to advance your knowledge in Python.
In the How to pip install from GitHub Repo post, I will share how you can host your Python Package from the GitHub website and pip install from GitHub.
Latest Posts
- How to convert MD (markdown) file to PDF using Pandoc on macOS Ventura 13
- How to make MD (markdown) document
- How to Install Docker Desktop on mac M1 chip (Apple chip) macOS 12 Monterey
- How to install MySQL Workbench on macOS 12 Monterey mac M1 (2021)
- How to install MySQL Community Server on macOS 12 Monterey (2021)