For all the hype around packages, a python package is a pretty straightforward thing. It’s just a file with a .py extension that contains Python code. That’s all. So every time you write Python code and save it to a .py file, you are creating a package. This doesn’t mean that you should always use this code as a package. It can certainly be treated as a stand-alone application. But if you wanted to create a package with python, with just code that you often need in your job, you can do it. In this article, I’ll walk you through how to create a package with Python.
Create A Package with Python
A python package is just a file with a .py filename extension. The name of the package is the same as the name of the file (without the .py). Like any .py file, the package contains Python code. Now let’s move forward with to create a package with python.
I will use three names to create a package with python, they are explained as follows:
- to_date(any_str): Allows you to pass any date string (any_str) in the format mm / dd / yy or mm / dd / yyyy and returns a Python datetime.date that you can use for date calculations.
- mdy(any_date): Allows you to pass any Python date or date / time and returns a date string formatted in mm / dd / yyyy format for display on the screen.
- to_curr(any_num, len): Allows you to pass any Python floating-point or integer number and returns a string with a dollar sign at the beginning, commas in thousands of places, and two digits for pennies. The len is an optional number for the length. If provided, the return value will be padded on the left with spaces to match the specified length.
So here is the full code of .py file to create a package with python:
You can then create the same file yourself and name it myfunctions.py if you want to track. Note that the file only contains functions. So if you run it, it won’t do anything on the screen because no code calls any of these functions.
Now as we are done to create a package with python. Next thing to do is to use these functions in a Python application or any program you will write, but first, be sure to copy this myfunc.py file to the same folder as the rest of the Python code you write. Then when you create a new page you can import myfunc as a package just like you would any other package created by someone else. And then just use:
You will need to use the package name in front of one of the functions you call from this python package. So if you want to make the code a bit more readable, you can use this instead:
With this as the opening line, you can refer to any function in your custom package with my. as a prefix. For example, my.to_date () to call the to_date function. Here is the code that imports the python package, then tests all three functions using my syntax:
I hope you liked this article on how to create a package with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.