Today I am going to show you how to create a simple python module which you can then use it in your python program again and again. Before we create a python module lets create a main python module folder where we can then keep all our python modules in one place. Create the main module folder either in the c or d drive in your computer, then create a folder inside that main folder to keep our new python module.
Next create a new python project in NetBeans IDE 8.1 just like the previous tutorial (refer to http://gamingdirectional.com/blog/2016/07/20/how-to-set-up-python-platform-in-netbeans-8-1/)
You can either delete the default .py file in the project and create a brand new empty module or turn that default file into module. In this tutorial I will delete the default file and create an empty python module. Right click on summation.py then delete then right click on the project folder and select New->Empty Module. Enter the module name then click on Finish. Next we will enter a few lines of code into the NetBeans editor plus a few lines of comment that explain what this module is for…
#This is the “summation.py" module, and it provides one function called #sumUpNumber() which will sum up all the numbers you have put into it def sumUpNumber(*args): #Your input will be any number, any length of parameters #into this function and it will return the total total = 0 for arg in args: total += arg return total
Next copy the summation.py file and paste it in PythonModule->summation folder which you have just created. Create a new file with Notepad++ Editor which is another IDE I often use to do the coding job. Enter below code into that empty file and save it in the PythonModule->summation folder as setup.py. Those code that you have entered below will become the metadata of your module distribution.
from distutils.core import setup setup( name = 'summation', version = '1.0.0', py_modules = ['summation'], author = 'choose', author_email = 'email@example.com', url = 'http://gamingdirectional.com', description = 'Module for number summation', )
That is it for the setup.py part! Next open up the windows command prompt and browser to the summation folder where those two files are and type in below command then press enter.
This will create the distribution package for the summation module.
Next install the distribution into your local copy of Python with this command
Now this module should be inside the python’s site-packages, it is time to import the module into a new python program and runs it. Create a new python project in NetBeans IDE 8.1 then enter below code and Run the program!
from summation import sumUpNumber if __name__ == "__main__": print(sumUpNumber(1,2,3))
It works! The outcome is as follow:-
So there it is, you have created your first module in python and then install it in the python’s site-packages folder so you can then import and use that module in your other python programming again and again! After you have created your first module, then what’s next? Go ahead and read the second part of this tutorial!