Write Setup File for Python Module#
In the final project of your Python code, you want to make it the complete installer of your program and it can be installed in any people’s Python machine. To do so, you have to create a setup file. The setup file will allow you to install the Python program (library) into the computer system and you can call it from any working directory. So, in this article, I will share how to create a setup file in Python.
Files Structure#
The first thing is you have to create a file structure to make your Python installer more looks organized. For example, you have a main folder (directory) named Calculate-Module or you can name it as the main title of your Python program. Inside this directory, you will have more than one subdirectory or file. In this case, inside the Calculate-Module, We have test and calculate directories, as well as two files which are README.md and setup.py. The test directory consists of a file (run_myadd.py) to be used to run a sample test of your Python program by a new user. Then, inside the calculate directory, you will have a main Python program (myadd.py) and an empty file named init.py. Remember, the empty file must be created and put in the same directory as the main Python program. Then, the README.md is the file that consists of some information related to your Python program. The important file is setup.py. This file consists of some configurations related to your Python program. The whole file structure can be seen as follows
Calculate-Module [Main-folder]
test [sub-folder]
run_myadd.py [file]
calculate [sub-folder]
__init__.py [initial file]
calculate.py [module file]
README.md [file]
setup.py [file]
Your Python Code#
The first thing is you have to make sure that your final Python code has been completed. Remember, when you want to make it installer, your Python code has to be written in function mode. For example, I have a simple Python code to calculate the simple summation between two numbers as follows. The function name is myadd and the file name is calculate.py
[1]:
def myadd(var1,va2):
x = var1+var2
return x
I believe the complex Python code will have many functions in different directories. So, this is just for simplicity case.
Create a README file#
Next, you have to create an information file of your Python program called README.md. This file describes everything about your Python program, what is the main purpose of your Python program to be used for, the capabilities of your Python program, the current version of your Python program, and so on. This file is written Markdown file format (.md). You may have a content structure of your information something like this
# MAIN TITLE
Describe the main information about your module/package
# REQUIRED
Describe the what required library for support your module/package
# INSTALL
Describe how to install your module/package
# USAGE
Describe how to use your package
# MORE INFORMATION
Describe another piece of information that you want to share
Create Setup File#
The format of the setup file looks something like this
[ ]:
import setuptools
with open("README.md","r") as fh:
long_description = fh.read()
setuptools.setup(
name="calculate",
version="0.0.1",
author="Author Name",
author_email="Author Email",
description="Describe about your program,shortly",
long_description=long_description,
long_description_content_type="text/markdown",
url="Repository link that you have,e.g. git,github,etc",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.7',
)
Save it with the name setup.py and put it in a main directory (in this case, in Calculate-Module).
The meaning of each variable is described as follows:
The name is a module name
The version is a module version. The version number is written in three-digit numbers (e.g., 0.0.1)
The author is the module owner’s name (e.g., individual name or team name)
The author_email is the owner’s email
The description is a short description of the module
The long_description is the long description of the module that saved in the README.md file
URL is the repository link (e.g., git/GitHub)
The packages is the variable to find the package of the module
The classifiers are the information about the version of Python, License, Operating System, and other information that is used in the module
The python_requires is the requirement of the Python version to use this module
Install The Module#
Finally, your setup file is complete. Now, you can try to install your own Python program into a computer machine through the following command
python3 setup.py install –-record installpath.txt
The command of python3 can be adjusted and depends on what command you use to call a Python command. To check the module has been successfully installed, you can run the test file that you have.
I hope this article is useful. See you in the next article.