A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py

create hello.py then write the following function as its content:

def helloworld():
   print("hello")

Then you can import hello:

>>> import hello
>>> hello.helloworld()
'hello'

To group many .py files put them in a folder. Any folder with an __init__.py is considered a module by python and you can call them a package

|-HelloModule
  |_ __init__.py
  |_ hellomodule.py

You can go about with the import statement on your module the usual way.

For more information, see 6.4. Packages.

Answer from Anuj on Stack Overflow
Top answer
1 of 8
482

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py

create hello.py then write the following function as its content:

def helloworld():
   print("hello")

Then you can import hello:

>>> import hello
>>> hello.helloworld()
'hello'

To group many .py files put them in a folder. Any folder with an __init__.py is considered a module by python and you can call them a package

|-HelloModule
  |_ __init__.py
  |_ hellomodule.py

You can go about with the import statement on your module the usual way.

For more information, see 6.4. Packages.

2 of 8
288

Python 3 - UPDATED 18th November 2015

Found the accepted answer useful, yet wished to expand on several points for the benefit of others based on my own experiences.

Module: A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

Module Example: Assume we have a single python script in the current directory, here I am calling it mymodule.py

The file mymodule.py contains the following code:

def myfunc():
    print("Hello!")

If we run the python3 interpreter from the current directory, we can import and run the function myfunc in the following different ways (you would typically just choose one of the following):

>>> import mymodule
>>> mymodule.myfunc()
Hello!
>>> from mymodule import myfunc
>>> myfunc()
Hello!
>>> from mymodule import *
>>> myfunc()
Hello!

Ok, so that was easy enough.

Now assume you have the need to put this module into its own dedicated folder to provide a module namespace, instead of just running it ad-hoc from the current working directory. This is where it is worth explaining the concept of a package.

Package: Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.

Package Example: Let's now assume we have the following folder and files. Here, mymodule.py is identical to before, and __init__.py is an empty file:

.
└── mypackage
    ├── __init__.py
    └── mymodule.py

The __init__.py files are required to make Python treat the directories as containing packages. For further information, please see the Modules documentation link provided later on.

Our current working directory is one level above the ordinary folder called mypackage

$ ls
mypackage

If we run the python3 interpreter now, we can import and run the module mymodule.py containing the required function myfunc in the following different ways (you would typically just choose one of the following):

>>> import mypackage
>>> from mypackage import mymodule
>>> mymodule.myfunc()
Hello!
>>> import mypackage.mymodule
>>> mypackage.mymodule.myfunc()
Hello!
>>> from mypackage import mymodule
>>> mymodule.myfunc()
Hello!
>>> from mypackage.mymodule import myfunc
>>> myfunc()
Hello!
>>> from mypackage.mymodule import *
>>> myfunc()
Hello!

Assuming Python 3, there is excellent documentation at: Modules

In terms of naming conventions for packages and modules, the general guidelines are given in PEP-0008 - please see Package and Module Names

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

🌐
Python documentation
docs.python.org › 3 › tutorial › modules.html
6. Modules — Python 3.14.4 documentation
The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:
Discussions

I need tips/guidelines on making my own python module
I highly recommend starting with the official packaging docs. Specifically these 2 pages: https://packaging.python.org/en/latest/overview/ https://packaging.python.org/en/latest/tutorials/packaging-projects/ They walk you through everything you need from tooling to project structure. p.s. slapping all your workflow scripts into a package and uploading to a public package repository sounds like a pretty bad idea. Packages should generally serve one purpose and should only really be published publicly if they will be useful to others. However, you can still use all the packaging tools and install it directly from a GitHub repo rather than somewhere public like PyPI. More on reddit.com
🌐 r/pythontips
2
1
March 14, 2025
Where to place custom module?
I’m still new to learning Python. I have Python 3.9 on Windows 10. I have not learned yet how to make modules. I’m making a custom module crutil.py which has many functions that I will use in many programs. My crutil.py will be used by many programs in many directories. More on discuss.python.org
🌐 discuss.python.org
6
0
February 28, 2024
How do I create my own python library which would be used by coders?
Upload it to GitHub to start with. When it's in a decently stable condition, release it to PyPI so that people can install it via pip: instructions here . More on reddit.com
🌐 r/learnpython
23
69
April 26, 2022
How can I create a Python module that is good enough to be uploaded to PyPI?
I created my first package on PyPi a few days ago, so my experience may be helpful: Firstly, do not use ChatGPT. It has a lot of out of date information and will mislead you more than it helps. Secondly, do use the official PyPi documentation. When you create your PyPi account, you will be shown very useful documentation for how to proceed. It is a little vague in a couple of places, but overall it good and accurate. If you get stuck or find that you need clarification, they also have a HELP section. Use a modern packaging tool. I used Poetry , which is one of the most popular and has comprehensive documentation. There are others, but having used Poetry I would recommend it, not least because of its comprehensive and easy to follow documentation. Use GitHub as your "trusted publisher". I don't know if it the best to use, but it works and is reasonably straightforward to use. GitHub provides a "workflow" for publishing Python packages. The published package appears as a "release" in your GitHub repository, and PyPi then gets your published project from GitHub. Note that despite what ChatGPT might tell you, it is not necessary to use twine or set up authentication keys, or manually push the package to PyPi - in fact I don't think that method works these days. You will need to set up 2FA (two factor authentication). If you don't have this set up already, then I would recommend Google's 2FA "Authenticator" app for mobile, as it is reliable and easy to use. The process If you don't already have a GitHub account, set one up (it is free for open source projects). Do this first because you will need it. If you don't already have a PyPi account, set one up (free), but only as far as registering your name. Publishing your first project comes later. Begin your project with Poetry. Follow the Basic usage tutorial. This will create a project structure that will work for both publishing on GitHub and PyPi. Don't start working on the project yet, just set up the bare bones of the project. Create a new repository for your project on GitHub. Do not add a README or LICENSE file yet - just create an empty project. Using either the command line, or your IDE, initialise git in your local project and configure your new GitHub repository as the "remote branch". Upload your bare bones project to GitHub using either git from the command line, or from your IDE. Ensure that you only upload the files created by Poetry in step 3. Now go back to GitHub and add the README, LICENSE, and .gitignore files (GitHub provides easy to use templates for these). Use git (command line or via your IDE) to "pull" and "merge" the new files into your "local branch" (the code on your computer). Develop your project and use "git commits" frequently. Lots of small commits are much better than committing big changes. When your project is ready to publish, setup a "workflow" on GitHub to publish. GitHub provides a workflow specifically for publishing Python projects, and you just need to fill in a few project specific details. Do not run the workflow yet! Set up a "provisional" trusted publisher on PyPi. Use the Poetry build command and check that the build artifacts work correctly (try installing the "wheel" using pip in a virtual environment). Run the "workflow" on GitHub by creating a new release (from the "Releases" section of your repository. This should create a new release on GitHub AND release to PyPi. Good luck. More on reddit.com
🌐 r/learnpython
19
7
December 27, 2024
🌐
W3Schools
w3schools.com › PYTHON › python_modules.asp
Python Modules
Consider a module to be the same as a code library. A file containing a set of functions you want to include in your application. To create a module just save the code you want in a file with the file extension .py:
🌐
Python Packaging
packaging.python.org › tutorials › packaging-projects
Packaging Python Projects — Python Packaging User Guide
This tutorial walks you through how to package a simple Python project. It will show you how to add the necessary files and structure to create the package, how to build the package, and how to upload it to the Python Package Index.
🌐
Python 101
python101.pythonlibrary.org › chapter36_creating_modules_and_packages.html
Chapter 36 - Creating Modules and Packages — Python 101 1.0 documentation
You want to append the parent folder that holds your new module, NOT the module folder itself. If you do this, then the code above should work. You can also create a setup.py script and install your package in develop mode. Here’s an example setup.py script: #!/usr/bin/env python from setuptools import setup # This setup is suitable for "python setup.py develop".
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-modules
Python Modules - GeeksforGeeks
4 days ago - Instead of writing everything in one place, related functionality can be grouped into its own module and imported whenever needed. To create a module, write the desired code and save that in a file with .py extension.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › create-and-import-modules-in-python
Create and Import modules in Python - GeeksforGeeks
December 29, 2019 - Scoping: A separate namespace is defined by a module that helps to avoid collisions between identifiers. A module is simply a Python file with a .py extension that can be imported inside another Python program. The name of the Python file becomes the module name. The module contains definitions and implementation of classes, variables, and functions that can be used inside another program. Example: Let's create a simple module named GFG.
🌐
Reddit
reddit.com › r/pythontips › i need tips/guidelines on making my own python module
r/pythontips on Reddit: I need tips/guidelines on making my own python module
March 14, 2025 -

Hey guys, so I've used python, bash and C extensively with my project work at uni. To the point where I have way too many scripts to streamline my workflow and I'm debating combining them all in a module I can upload to conda-forge however, I'm unsure where to start. Short of just taking a module which handles something similar to what I do and using it as a skeleton I'm kinda lost. Plus i would like to actually code it from the ground up instead of using someone elses entire skelton. I also get that 'you can do whatever you want with python' but I want it to be intuitive to follow for anyone who might take over my position and edit the module. So if anyone had any good guides I can follow or tips on what would be 'best practice' that would be amazing.

🌐
GeeksforGeeks
geeksforgeeks.org › installation guide › how-to-install-a-python-module
How to Install a Python Module? - GeeksforGeeks
July 23, 2025 - Replace <module name> with the name of the module you want to install. For example, to install the popular numpy module, you would use: ... Note: If you are using Python 3.x, you may need to use the command pip3 instead of pip.
🌐
DataCamp
datacamp.com › tutorial › modules-in-python
Python Modules Tutorial: Importing, Writing, and Using Them | DataCamp
March 28, 2025 - Any Python file can be referenced as a module. A file containing Python code, for example: test.py, is called a module, and its name would be test. There are various methods of writing modules, but the simplest way is to create a file with a .py extension, which contains functions and variables.
🌐
Medium
medium.com › @JTPrieto01 › how-to-create-a-module-in-python-d9fd0660c871
How to create a module in python? | by Ashima Sethi | Medium
October 21, 2020 - How to create a module in python? A module is a python object with an arbitrarily named attribute that you can bind and reference. In simple language, it is just another python file consisting of …
🌐
Medium
martinxpn.medium.com › how-modules-actually-work-in-python-and-how-to-create-your-own-custom-module-81-100-days-of-d1a84fead104
How Modules Actually Work in Python and How to Create Your Own Custom Module (81/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - A module in Python is simply a file with a .py extension that contains Python code. To create a module, you just need to write your code in a .py file and save it with an appropriate name.
🌐
Medium
medium.com › @roylowrance › how-i-create-local-python-packages-2f528ef57346
How I Create Local Python Packages | by Roy Lowrance | Medium
March 20, 2025 - This workflow assumes that you haven’t created any of the package code or directories to hold the project artifacts yet. Packages are installed using the Python pip command. To invoke it, type python3 -m pip [arguments]. Starting pip from python3 guarantees that you get the version of the pip command that came with your Python system. To get started, we need some terminology. A Python module is a file containing Python source code ending with .py.
🌐
Wellsr
wellsr.com › python › how-to-create-custom-modules-in-python
How to Create Custom Modules in Python (with examples) - wellsr.com
April 1, 2022 - To import a custom module, you can use the import statement followed by your module name. This syntax is similar to importing default or installed Python modules. The script below imports the newmodule module which is created by the
🌐
Real Python
realpython.com › build-python-c-extension-module
Building a Python C Extension Module – Real Python
June 27, 2023 - When a Python program imports your module for the first time, it will call PyInit_fputs(): ... It implicitly sets the return type of the function as PyObject*. It declares any special linkages. It declares the function as extern “C.” In case you’re using C++, it tells the C++ compiler not to do name-mangling on the symbols. PyModule_Create() will return a new module object of type PyObject *. For the argument, you’ll pass the address of the method structure that you’ve already defined previously, fputsmodule.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-write-modules-in-python-3
How To Write Modules in Python 3 | DigitalOcean
August 20, 2021 - Additionally, you can create your own Python modules since modules are comprised of Python .py files. This tutorial will guide you through writing Python modules for use within other programming files. You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to ...
🌐
Reddit
reddit.com › r/learnpython › how can i create a python module that is good enough to be uploaded to pypi?
r/learnpython on Reddit: How can I create a Python module that is good enough to be uploaded to PyPI?
December 27, 2024 -

I am a beginner in Python. I have an idea to make a CLI app, but I don't know where to start or how it should be formatted. How should a Python module be created? Is there any specific formatting or structure?

Top answer
1 of 7
15
I created my first package on PyPi a few days ago, so my experience may be helpful: Firstly, do not use ChatGPT. It has a lot of out of date information and will mislead you more than it helps. Secondly, do use the official PyPi documentation. When you create your PyPi account, you will be shown very useful documentation for how to proceed. It is a little vague in a couple of places, but overall it good and accurate. If you get stuck or find that you need clarification, they also have a HELP section. Use a modern packaging tool. I used Poetry , which is one of the most popular and has comprehensive documentation. There are others, but having used Poetry I would recommend it, not least because of its comprehensive and easy to follow documentation. Use GitHub as your "trusted publisher". I don't know if it the best to use, but it works and is reasonably straightforward to use. GitHub provides a "workflow" for publishing Python packages. The published package appears as a "release" in your GitHub repository, and PyPi then gets your published project from GitHub. Note that despite what ChatGPT might tell you, it is not necessary to use twine or set up authentication keys, or manually push the package to PyPi - in fact I don't think that method works these days. You will need to set up 2FA (two factor authentication). If you don't have this set up already, then I would recommend Google's 2FA "Authenticator" app for mobile, as it is reliable and easy to use. The process If you don't already have a GitHub account, set one up (it is free for open source projects). Do this first because you will need it. If you don't already have a PyPi account, set one up (free), but only as far as registering your name. Publishing your first project comes later. Begin your project with Poetry. Follow the Basic usage tutorial. This will create a project structure that will work for both publishing on GitHub and PyPi. Don't start working on the project yet, just set up the bare bones of the project. Create a new repository for your project on GitHub. Do not add a README or LICENSE file yet - just create an empty project. Using either the command line, or your IDE, initialise git in your local project and configure your new GitHub repository as the "remote branch". Upload your bare bones project to GitHub using either git from the command line, or from your IDE. Ensure that you only upload the files created by Poetry in step 3. Now go back to GitHub and add the README, LICENSE, and .gitignore files (GitHub provides easy to use templates for these). Use git (command line or via your IDE) to "pull" and "merge" the new files into your "local branch" (the code on your computer). Develop your project and use "git commits" frequently. Lots of small commits are much better than committing big changes. When your project is ready to publish, setup a "workflow" on GitHub to publish. GitHub provides a workflow specifically for publishing Python projects, and you just need to fill in a few project specific details. Do not run the workflow yet! Set up a "provisional" trusted publisher on PyPi. Use the Poetry build command and check that the build artifacts work correctly (try installing the "wheel" using pip in a virtual environment). Run the "workflow" on GitHub by creating a new release (from the "Releases" section of your repository. This should create a new release on GitHub AND release to PyPi. Good luck.
2 of 7
4
Use a project manager such as uv, it'll take care of the project structure and packaging. You'll want to add a CLI entrypoint to your package metadata so that pip creates an executable that runs your program.
🌐
Plain English
python.plainenglish.io › how-to-create-a-custom-python-module-step-by-step-guide-b892f779cb3b
How to Create a Custom Python Module (Step-by-Step Guide) | Python in Plain English
October 5, 2025 - Learn how to write your own Python modules and packages to organize, reuse, and share your code for cleaner projects.