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 OverflowA 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.
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.
I need tips/guidelines on making my own python module
Where to place custom module?
How do I create my own python library which would be used by coders?
How can I create a Python module that is good enough to be uploaded to PyPI?
Videos
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.
I have a very specific subject in mind on which I want to create a python library which would be useful for coders all across. How do I go about creating and publishing the same?
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?