Categories
Python Answers

How to write a Python module or package?

Spread the love

Sometimes, we want to write a Python module or package.

In this article, we’ll look at how to write a Python module or package.

How to write a Python module or package?

To write a Python module or package, we just create a file with the .py extension.

For instance, we create hello.py and write

def hello_world():
   print("hello")

to add the hello_world function in it.

Then in another .py file in the same folder, we write

import hello

hello.hello_world()

to import hello.py as hello and call the hello_world function in it with

hello.hello_world()

If we have more than one .py file in a folder, then we need to put an init.py file inside the folder to let us import all the files in the folder.

Conclusion

To write a Python module or package, we just create a file with the .py extension.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *