Tuesday, August 30, 2022

[SOLVED] rpm package from python source

Issue

I have a python script MyScript.py. I run it using python MyScript.py [options]. But I want to make rpm package from it. So I created setup.py and created rpm package by using python setup.py bdist_rpm. For this I changed file structure as below:

- MyScript
   - __init__.py
- setup.py

setup.py:

from distutils.core import setup
setup(name='MyScript',
      version='0.0.1',
      author='ABC XYZ',
      author_email='[email protected]',
      packages=['MyScript']
      )

When I run python setup.py bdist_rpm, I get 2 rpm files (noarch.rpm, src.rpm) and 1 tar.gz file under dist folder which is created automatically. But When I use rpm -i on norach.rpm file, it just says that package is installed but I can not use the package when I try to run MyScript command in bash. Am I doing something wrong here? Please guide me if so. I am bit beginner to packaging.


Solution

Yes, you are doing something wrong :)

The fact that you created an rpm and provided a MyScript package does not mean that installing the rpm will expose an executable for you to run (i.e $ MyScript .... To also making an executable available which will interact with the package, you need to provide an entry_point in your setup.py file.

An entry point pretty much maps a script which will be installed in the path to a function in your code and runs it.

Add something like this:

setup(name='MyScript',
      version='0.0.1',
      author='ABC XYZ',
      author_email='[email protected]',
      packages=['MyScript'],
      entry_point={
          'console_scripts': [ 
              'MyScript = MyScript.__init__:FUNC_NAME'
          ]
      } 
  )

where FUNC_NAME is the name of the function in the MyScript package in the __init__ module to invoke.

The general format (for future reference) of a console_script is:

'name_of_executable = package.module:function_to_execute'

An example can be found here: https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/



Answered By - nir0s
Answer Checked By - Timothy Miller (WPSolving Admin)