Shreyas Kulkarni
Shreyas' Blog

Shreyas' Blog

How to create your own Package in python?

How to create your own Package in python?

Shreyas Kulkarni
·Sep 20, 2021·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

let's understand the basics first, what is module and package, consider any python file with the .py extension is called a module. in short module is a single python file.

on the other side package is a collection of python modules, and it contains an additional init.py file.

let's get some clarity here, you can put your python files in some folder that's a usual way of saving your scripts but here you want to make that folder a package so for that you will need to add an init.py file. so that it will get identified as a package. and each module in that folder can be called we will see it further.

so before jumping into creating our package I would like to list down the why:

Advantages of using python packages:

  1. to break down large programs into smaller manageable and organized files.
  2. ensure reusability of code
  3. can be called in any python file, where the file name becomes the module name.

let's start the real stuff:

First, find the path where you will need to save your package, here I have listed the most common path but if you have installed your python or anaconda somewhere else then the path may differ.

# if you are using anaconda
C:\ProgramData\Anaconda3\Lib\site-packages

# if you are using core python
C:\Program Files\Python39\Lib\site-packages

here you need to create a folder named geometry, yeah you can create whatever you want here for the sake of the demo I am giving you all the inputs,

# create folder called geometry

Now open notepad, and we will create our first module here, just copy-paste the following code in the notepad, and name it volumelib.py and save it in the geometry folder.

make sure there's no extension other than ".py"

# volumelib module

# calculate cube volume
def calculateCubeVolume(sideSize):
    volume = sideSize * sideSize * sideSize
    print('Cube volume :',volume)

# calculate cuboid volume
def calculateCuboidVolume(length,breadth,height):
    volume = length * breadth * height
    print('Cuboid volume :',volume)

now you are following me on this, here we will create our second module, follow the same procedure and again save it in the same folder here file name should be arealib.py

make sure there's no extension other than ".py"

# arealib module

# calculate area of square
def calculateSquareArea(sideSize):
    area = sideSize * sideSize
    print('Area of square :',area)

# calculate area of rectangle
def calculateRectangleArea(length,breadth):
    area = length * breadth
    print('Area of rectangle :',area)

now we are halfway done and the next step is to create an init.py file.

# save an empty file named as __init__.py
# underscore underscore init underscore underscore .py

Now you are ready with your package let's go to the IDE, I will prefer jupyter here you are free to choose spyder or pycharm

we will import our module first:

# import module
import geometry.arealib
import geometry.volumelib

if you are getting errors at this stage then there will be either of the cases

  1. check the path of the file you saved.
  2. check for spelling mistakes in saved module files if you haven't just copied and pasted.

and if you're not getting any output then congratulations you made it, your package is imported successfully and waiting for you to ask him to get something done out of it

# use function from module (arealib)
geometry.arealib.calculateSquareArea(10)
geometry.arealib.calculateRectangleArea(10,15)

you made it, your package is working at first you are calculating the area of the square and second you are calculating the rectangle area.

# use function from module (volumelib)
geometry.volumelib.calculateCubeVolume(10)
geometry.volumelib.calculateCuboidVolume(10,20,30)

here you are calculating the volume of cube and cuboid respectively. remember python is case sensitive so make sure you follow the syntax.

we made our package and it's time to celebrate, here you are 1% better than you were 15 minutes ago I feel its enough reason to celebrate.

So I would be glad if you show some decency to your own self and treat yourself as an achiever until then keep visiting here for getting some python and data science stuff.

thanks for reading, happy coding, catch you in the next one!

 
Share this