Making your own Modules Python Tutorial



This Python 3 tutorial is focused on the making of modules. Modules are often confusing to people who are first getting started in Python, and they don't have to be! For the most part, modules are just Python scripts that are stored in your Lib or Lib/site-packages folder, or local to the script being run. That's it. The installation of *most* modules is simply the moving of the module's files into these directories. It's very basic. You should be able to, after watching this quick series, just simply download the source code of the module that you are looking to employ, and move the source code yourself to the packages directory.

My aim here is to show how you can create your very own module and where you can put it so you can import it. Hopefully with this information, you can better understand how modules work, that you can edit modules, and what to do if you only have the source code for the module you are wanting.

To do this, the example module that we create is called examplemod.py, and within it is just the following:

def ex(data):
    print(data)

That's it! We just put a function in there that, when called, will just print out the parameter.

Now, we just need to import and use that.

Doing so is as simple as:

import examplemod
examplemod.ex('test')

To do this, the examplemod.py file must be in the same directory as your running-program, or in your Python packages directory. If you are on a windows machine, this will be C:/python34/Lib/site-packages/

Hopefully this can help you see that there really is no "magic" behind most modules. You can look through your own standard library that comes with Python in the /Lib/ directory. This is where all of your standard library modules are. Just take a peak through them to see most of them are just simple Python scripts. There are some more complex modules out there, but the vast majority of Python modules are just simple scripts, sometimes they are "packages" where there are many scripts within a folder, but this is still all it is.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial