A Library in Python, also known as a module is a Python file that contains pre-written classes, functions, and code that can be used in any Python program by importing the file. If you are a Python coder, you might have been using built-in modules or installing Python libraries and then importing them, however, in this short article, we will discuss how you can create a Python library on your own. We will go through step by step process of creating a Python library and then we will learn how we can import the Python library that we have just created and access the functions inside it.
What is a Python Library?
A Python library is also known as a Python module. A Python library is simply a file that contains pre-written code, functions, and classes. We can use these functions and classes in our program by simply importing the Python library.
Some of the common Python libraries include:
- Numpy: Use for numeric calculations
- Pandas: Use to handle datasets
- Math: Contains many useful mathematical formulae
- Scikit-learn: Use for Machine learning purposes
- Matplotlib: Helpful in data visualization
These are just a few Python libraries. One of the reasons for the popularity of Python language is its large number of libraries which help you to do coding without writing all the functions and classes. If you will search the total number of Python libraries on google, you will get results something like this:
This shows only the modules that are in the PyPI or official Python Package. The actual number of Python modules is much larger than this number and every day this number is increasing.
How to Create a Python Library in a Virtual environment?
Now, we will jump into the actual purpose of this article which is to create a Python library. In this tutorial, we will be using visual studio as our main Python editor and will create the Python library on a virtual environment. If you are using a different editor or don’t want to create a Python library on a virtual environment, the steps are pretty much the same.
Here are the specifications of the system that I am using in order to create a Python library on my system.
- Using Linux operating system (Steps on other operating systems will also be same)
- Visual code studio ( Steps on any other IDE will also be the same)
- Creating the module in a virtual environment.
If you have all these specifications, then the following steps will be very easy to follow for you.
Step-1: Creating the directory for the Python Library
The first step in creating a Python library is to select the directory where you want to have this Library. You can use the GUI to create a new directory or you can also use the Linux commands in order to create a new directory.
By the way, here are some useful Linux commands that can help you to create a new directory on your system.
- pwd: Used to return your current working directory
- ls: This command is used to list all the folders and files that exist in your current directory.
- cd <path>: This command is used to jump from one directory to another directory. In the path section, you need to provide the full path to the folder from your current directory.
- mkdir <folder name>: This command helps you to create a new folder in the current directory. You can use this command to create a new folder for your Python module and name it whatever you want.
In my case, the name of the folder where I will be working to create a Python library is PythonLibrary.
Step-2: Open Visual Studio
The next step is to open visual studio and make sure that the folder that you have just created should be your working directory. For example, as shown below:
If you are using any other IDE or Python text editor, you can open it in the same directory.
Step-3: Create a Virtual Environment
If you don’t want to create a Python library in a virtual environment, you can skip this section. The reason for using a virtual environment to create a Python module is because of some solid reasons. Virtual environments prevent the issue of running into dependency issues later on. For example, in older projects, you might have worked with older versions of the NumPy library. Some old code, that once worked beautifully, might stop working once you update its version. Perhaps parts of NumPy are no longer compatible with other parts of your program. Creating virtual environments prevents this. They are also useful in cases when you are collaborating with someone else, and you want to make sure that your application is working on their computer, and vice versa.
Now let us create a virtual environment by following the following commands: Open the terminal and write the following commands to create a virtual environment:
python3 -m venv venv
Once you will run the command, you will notice that a new folder will be created with the name venv in the directory where you are currently working.
The next step is to activate the virtual environment that you have just created. Run the following commands to activate it.
The moment you will run the command, you will notice that (venv) will be added in the terminal as shown below:
Now make sure that you have installed the pip on the virtual environment.
Step-4: Installing the required Modules
Once the virtual environment is activated, you need to install some required Python modules which will help us to create a Python library.
You can use the pip command to install these modules on your virtual environment.
pip install wheel pip install setuptools pip install twine pip install pytest==4.4.1 pip install pytest-runner==4.4
Once the installation is complete, we are good to go to the next step.
Step-5: Create the folder structure
In this step, you will add files and folders to the project. You can either use the terminal or GUI to create and add files to your project. The following files and folders should be added to the projects.
- First, create an empty python file named setup.py.
- Next, you can create an empty file named README.md.
- Now, create a folder named PythonLib( you can give your own name to the folder). One thing to keep in mind while giving the name to the folder is that the name should be unique if you wish to publish your Python library later.
- Now, go inside the PythonLib folder and create a python file named __init__.py.
- In the same folder (PythonLib), create a file named functions.py.
- And now finally, create a folder test in the root folder. Inside the test folder create two empty python files named, __init__.py and test_functions.py.
Once you are done with all the mentioned steps above, your working directory structure will look like this.
If your working directory looks like this, then you are good to go to the next step.
Step-6: Create the content for the library
Now it is time to create the content for your library. You can add the functions and classes to your library now. In our case, we will just add one function that will find the sum of two numbers.
def main(num1: float, num2: float): return num1+num2
This code should be inside the functions.py file.
Step-7: Testing the function
When you are creating a library, it is highly recommended to test it multiple times. For testing our function, we will use pytest and pytest-runner modules that you have already installed on your system.
Let us create a small testing function that will be used to test our function. This function should be inside the test_functions.py file.
from PythonLib import functions def test_haversine(): assert functions.haversine(52.370216, 4.895168)
Once you have your testing code, it is now time to create a setup.py file which will help to build the library. Here is what the limited version of setup.py looks like:
from setuptools import find_packages, setup setup( name='mypythonlib', packages=find_packages(), version='0.1.0', description='My first Python library', author='Me', license='MIT', )
Once you have your setup file ready, then run the following commands which will go through the testing process.
python setup.py test
Once the testing will be complete, you will see something like this in your directory inside the test folder.
Step-8: Build the library
Now everything is ready and we completed all the necessary steps. It is now the time to build our library. Make sure that you are in the root directory of your project.
In the terminal, run the following commands:
python setup.py bdist_wheel
Running the above command will create some new directories in your root folder as shown below:
Now, you can install your library using the following commands:
pip install /path/to/wheelfile.whl
Congratulation!!! You have successfully created your Python module
Creating a library in Python requires some easy steps. We need to install some libraries and arrange our directory in a proper way to build a Python library. In this short article, we discussed how we can create a library in Python using simple steps. We discussed each step in detail.
- Extra trees classifier and regressor using Python
- How to start learning Machine Learning?
- Simple ways to understand sklearn confusion matrix in machine learning