Reusing Python Packages over GitHub

Well, the title speaks for itself. In this article, I will demonstrate how to reuse a Python library in various projects over GitHub.

I prefer this method over reusing local files due to benefits of GitHub.

Library Project

In this article, I will use a sample library called “Databus”.

The project structure should look like this:

  • All of your Python library files should be placed in a package called “databus”
  • Your folder root should contain a file called “setup.py”
  • It’s nice to include a readme.md file (optional)

Here is how the folder structure would look like:

Note that all of my library files are placed under a folder called databus. This ensures that the client may use the libraries as databus.driver.primal_driver ; without mixing them with their main project.

Here is how setup.py looks like:

import setuptools

setuptools.setup(
    name="databus-keremkoseoglu",
    version="0.0.8",
    author="Kerem Koseoglu",
    author_email="kerem@keremkoseoglu.com",
    description="A framework to transfer data between systems",
    long_description="A framework to transfer data between systems",
    long_description_content_type="text/markdown",
    url="https://github.com/keremkoseoglu/databus",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.7',
)

The content is pretty intuitive. You ideally need to increase the version in case you make new releases though.

At this point, you can push the library project to GitHub.

Client Project

What we need to do for the client project is pretty simple.

First, ensure that your virtual environment is activated. This can be achieved with the following terminal command:

. venv/bin/activate

Once your virtual environment is active, you can use the following command to install the library client:

pip install git+https://www.github.com/keremkoseoglu/databus.git

Obviously; the path would depend on your GitHub username and project name – but that’s how the command should look like.

Once pip finishes working, you can start using the library right away!

""" SAP sales order creation module """
import json
import requests
from databus.pqueue.queue_status import PassengerQueueStatus, QueueStatus
from databus.pusher.abstract_pusher import AbstractPusher


class Sap(AbstractPusher):
    """ SAP sales order creation class """
    def __init__(self, p_log=None):
        super().__init__(p_log=p_log)
(...)

In Visual Studio Code, you might need to change the environment to your OS environment & back to the project virtual environment for the IDE to detect the new package.

Voila & cheers!


Posted

in

,

by

Tags:

Comments

Leave a comment