0

I often encounter some python packages (which I installed using pip) which can be accessed like a standalone binary on Terminal . Not just a python module .

I want to know the standard way to create a package which can be installed by $pip install mypackage ( or by $python setup.py install) and is also available like $mypackage from anywhere. I know that I can access the __main__.py file by $python -m mypackage . But I want my package to function just like any binary stored in /usr/bin .

I am not looking for any alias/path variable hack and don't want to create .deb/.rpm stuffs .

EDIT: I got help from commentbox . Eventually , setuptools already has the option . Setuptools Documention

entry_point parameter of setup() function does the trick .

setup(
    # other arguments here...
    entry_points={
        'console_scripts': [
            'foo = my_package.some_module:main_func',
            'bar = other_module:some_func',
        ],
        'gui_scripts': [
            'baz = my_package_gui:start_func',
        ]
    }

Here entry_point takes a dictionary whose value contains a list . Two CLI scripts named foo and bar are created which contains function main_func and some_func respectively from my_package.some_module and other_module . Gui scripts can also be created via this method .

  • You mean you're trying to expose scripts from your own package? Then configure them in the setup.py. – jonrsharpe Dec 18 '17 at 13:29
  • 1
    https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation – wildwilhelm Dec 18 '17 at 13:30
  • Yeah , I was looking for this exactly . Thanks . I had no idea that setuptools had this option . @wildwilhelm – Tonmoy Ahmed Dhroubo Dec 18 '17 at 13:38
  • even if you get help in comment, you should really answer the answer the question properly as other people may stumble on the same question and there's a chance they don't really follow what the guide is saying. a few steps to follow would suffice. thanks a lot. – stucash Dec 18 '17 at 14:09
  • 1
    Just be aware that installing packages for the system python interpreter using pip can lead to problems, specially when pip tries to update a package that is also installed by the package manager (apt) and existing packages require the older version. pip does install packages to `/usr/local/lib/` which should not interfere with apt installed packages, but if a system package suddenly finds a newer, incompatible version of a dependency it can break. Using a venv is preferred. – mata Dec 19 '17 at 13:44
  • The question header is not clear for the searcher, perhaps it should better be: `How to install a Python package system-wide on Linux so that it can be accessed like a standalone binary on Terminal?` Since I was searching for a way to not install the packages in the local user directory. Which turned out to be done by using `sudo` in front of `pip install`, as easy as it my seem. Also, the question should be self-answered if no one else answers. – questionto42 Sep 02 '21 at 07:05

0 Answers0