How To Create A Python Library. Ever Wanted To Create A Python Library - by Kia Eisinga - Analytics Vidhya - Medium
How To Create A Python Library. Ever Wanted To Create A Python Library - by Kia Eisinga - Analytics Vidhya - Medium
Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
Listen Share
Ever wanted to create a Python library, albeit for your team at work or for some open
source project online? In this blog you will learn how to!
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 1/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
The tutorial is easiest to follow when you are using the same tools, however it is also
possible for you to use different ones.
Remember:
- With pwd you can see your present working directory.
- With ls you can list the folders and files in your directory.
- With cd <path> you can change the current present directory you are in.
- With mkdir <folder> you can create a new folder in your working directory.
In my case, the folder I will be working with is mypythonlibrary . Change the present
working directory to be your folder.
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.
(Make sure you changed the present working directory to the folder you are going to
create your Python library in ( cd <path/to/folder> ).)
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 2/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
Activating a virtual environment modifies the PATH and shell variables to point to the
specific isolated Python set-up you created. PATH is an environmental variable in
Linux and other Unix-like operating systems that tells the shell which directories to
search for executable files (i.e., ready-to-run programs) in response to commands
issued by a user. The command prompt will change to indicate which virtual
environment you are currently in by prepending ( yourenvname ).
In your environment, make sure you have pip installed wheel , setuptools and twine .
Search Medium
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 3/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
You now can start adding folders and files to your project. You can do this either
through the command prompt or in Visual Studio Code itself.
1. Create an empty file called setup.py . This is one of the most important files when
creating a Python library!
2. Create an empty file called README.md . This is the place where you can write
markdown to describe the contents of your library for other users.
3. Create a folder called mypythonlib , or whatever you want your Python library to be
called when you pip install it. (The name should be unique on pip if you want to
publish it later.)
4. Create an empty file inside mypythonlib that is called __init__.py . Basically, any
folder that has an __init__.py file in it, will be included in the library when we
build it. Most of the time, you can leave the __init__.py files empty. Upon import,
the code within __init__.py gets executed, so it should contain only the minimal
amount of code that is needed to be able to run your project. For now, we will leave
them as is.
6. And, finally, create a folder tests in your root folder. Inside, create an empty
__init__.py file and an empty test_myfunctions.py .
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 4/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
# Haversine formula
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 5/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
This function will give us the distance in meters between two latitude and longitude
points.
Whenever you write any code, it is highly encouraged to also write tests for this code.
For testing with Python you can use the libraries pytest and pytest-runner . Install the
library in your virtual environment:
> pip install pytest==4.4.1
Let’s create a small test for the haversine function. Copy the following and place it
inside the test_myfunctions.py file:
def test_haversine():
assert myfunctions.haversine(52.370216, 4.895168, 52.520008,
13.404954) == 945793.4375088713
Finally, let’s create a setup.py file, that will help us to build the library. A limited
version of setup.py will look something like this:
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 6/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
The name variable in setup holds whatever name you want your package wheel file to
have. To make it easy, we will gave it the same name as the folder.
file.
Install_requires should be limited to the list of packages that are absolutely needed.
This is because you do not want to make users install unnecessary packages. Also note
that you do not need to list packages that are part of the standard Python library.
However, since we have only defined the haversine function so far and it only uses the
math library (which is always available in Python), we can leave this argument empty.
Maybe you can remember us installing the pytest library before. Of course, you do not
want to add pytest to your dependencies in install_requires : it isn’t required by the
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 7/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
users of your package. In order to have it installed automatically only when you run
tests you can add the following to your setup.py :
setup(
name='mypythonlib',
packages=find_packages(include=['mypythonlib']),
version='0.1.0',
description='My first Python library',
author='Me',
license='MIT',
install_requires=[],
setup_requires=['pytest-runner'],
tests_require=['pytest==4.4.1'],
test_suite='tests',
)
Running:
> python setup.py pytest
Your wheel file is stored in the “dist” folder that is now created. You can install your
library by using:
> pip install /path/to/wheelfile.whl
Note that you could also publish your library to an internal file system on intranet at
your workplace, or to the official PyPI repository and install it from there.
Once you have installed your Python library, you can import it using:
import mypythonlib
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 8/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
Follow
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 9/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
529 6
247 5
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 10/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
1.8K 12
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 11/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
10.1K
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 12/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
10 Exciting Project Ideas Using Large Language Models (LLMs) for Your
Portfolio
Learn how to build apps and showcase your skills with large language models (LLMs). Get
started today!
1.5K 8
2.2K 12
Lists
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 13/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
New_Reading_List
173 stories · 1 save
1.3K 14
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 14/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
3.2K 46
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 15/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
1.1K 51
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 16/17
6/24/23, 4:28 AM How to create a Python library. Ever wanted to create a Python library… | by Kia Eisinga | Analytics Vidhya | Medium
1.1K 16
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f 17/17