Bootstrap FreeKB - Python (Scripting) - Creating your own pip module using setup.py
Python (Scripting) - Creating your own pip module using setup.py

Updated:   |  Python (Scripting) articles

There are several built in modules that are part of the Python Standard Library included with your Python installation, such as os (Operating System) and re (Regular Expression) and sys (System). For example, here is how you would import the sys module and then use the sys module.

#!/usr/bin/python3
import sys
print(sys.argv[0])

 

Which should print the following (the name of your Python script).

~]$ python3 testing.py
testing.py

 

However, there will ineviatabley be situations where you'll want to create your own module. A module is simply a Python file with the .py extension that does something. For example, let's say you have a file named greeting.py that contains the following.

#!/usr/bin/python3
def hello():
  return "Hello World"

 

If you create some other Python file such as main.py in the same directory as greeting.py, here is how you can use the hello function from greeting.py in main.py.

#!/usr/bin/python3
import greeting
print(greeting.hello())

 

Or, you could use from greeting import hello like this.

#!/usr/bin/python3
from greeting import hello
print(hello())

 

Running main.py should print Hello World.

~]$ python3 main.py 
Hello World

 

Let's create a file named greeting.py that contains the following.

__version__ = '0.0.1'

def hello():
    return "Hello World"

 

And let's create a file named setup.py that contains the following.

from setuptools import setup

from my_pip_package import __version__

setup(
    name='greeting',
    version=__version__,

    url='https://github.com/JohnDoe/modules',
    author='John Doe',
    author_email='john.doe@example.com',

    py_modules=['greeting'],
)

 

And then in the same directory as setup.py and greeting.py, run the pip install command with the -e or --editable option following by a single dot (if setup.py is in your present working directory) or following by the absolute path to the directory that contains your setup.py file.

pip install -e .

 

Or followed by the absolute path to the directory that contains your setup.py file.

pip install --editable /tmp

 

Something like this should be returned.

  Preparing metadata (setup.py) ... done
Installing collected packages: greeting
  Running setup.py develop for greeting
Successfully installed greeting

 

Or, if the package is already installed and you are re-installing it or installing a new version of the package, something like this should be returned.

Defaulting to user installation because normal site-packages is not writeable
Obtaining file:///home/xpostgres
  Preparing metadata (setup.py) ... done
Installing collected packages: greeting
  Attempting uninstall: greeting
    Found existing installation: greeting0.0.1
    Uninstalling snspublish-0.0.1:
      Successfully uninstalled greeting-0.0.1
  Running setup.py develop for greeting
Successfully installed snspublish

 

And the pip list command should show that the "greeting" module has been installed.

~]$ pip list
Package      Version
------------ -------
greeting     0.0.1
setuptools   56.0.0

 

Or, you can store setup.py and greeting.py in GitHub. For example, let's create a public repository in GitHub. For example, I created a public repository named "modules". In the repository, let's create a file named greeting.py that contains the following.

__version__ = '0.0.1'

def hello():
    return "Hello World"

 

And if you have a README.md file, let's create .gitignore to ignore the README.md file. Your public repository should look something like this.

 

Now the pip and git CLI's can be used to install the module from GitHub. If you don't have the git CLI installed, check out my article Install Git on Linux.

dnf install git

 

Now, the pip install command can be used to install the module from GitHub. Nice!

~]$ pip install git+ssh://git@github.com/JohnDoe/modules.git#egg=greeting
Collecting my_pip_package
  Cloning ssh://****@github.com/JohnDoe/modules.git to /tmp/pip-install-i90pj1zy/greeting_c45c7fff3cc64315b04864072b6c0470
  Running command git clone -q 'ssh://****@github.com/JohnDoe/modules.git' /tmp/pip-install-i90pj1zy/greeting_c45c7fff3cc64315b04864072b6c0470
Using legacy 'setup.py install' for greeting, since package 'wheel' is not installed.
Installing collected packages: greeting
    Running setup.py install for greeting ... done
Successfully installed greeting-0.0.1

 

And the pip list command should show that the "greeting" module has been installed.

~]$ pip list
Package      Version
------------ -------
greeting     0.0.1
setuptools   56.0.0

 

And greeting.py should be in the site-packages folder in your Python installation.

~]$ cat /usr/local/python3.9/site-packages/greeting.py
__version__ = '0.0.1'

def hello():
    return "Hello World"

 

And the greeting module can be imported and used. Awesome!

 ~]$ cat testing.py
#!/usr/bin/python
from greeting import hello
print(hello())

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 399344 in the box below so that we can be sure you are a human.