Search code examples
pythonpython-3.xsetuptoolspypi

Make my pypi script executable without the need to specify every time the path to the script


Recently I created a python script for PyPI. That you can download with pip install. The problem is you can only execute the script, that you downloaded with pip install, when you are in the Scripts folder which is where you python is localized (your_python_location/Scripts/myscript.py).
But this would be a hassle for the users. So I wanted to ask, how can I make it that you can execute the script from everywhere? (like you can do with pip without specifying the pip location). I also don't want that every user needs to set the path to the script.

My Setup.py (maybe its helpful):

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

with open('requirements.txt') as f:
    requirements = f.read().splitlines()

setuptools.setup(
    name="boston-housing-prediction",
    version="0.2.0a0",
    author="xx",
    author_email="[email protected]",
    py_modules=["misc_libary", "polynomial_regression_libary", "linear_regression_libary"],
    scripts=["boston-housing-main.py"],
    description="Predict housing prices in boston.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/XXX",
    packages=setuptools.find_packages(),
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7'
    ],
    keywords="regression meachine-learning housing_prices_boston learning_purpose",
    license="MIT",
    install_requires=requirements,
    python_requires='>=3.5',
)


Solution

  • You can specify entry_points in setup.py, like

    setuptools.setup(
        # ...
        entry_points = {
            'console_scripts': [
                'boston_housing = boston-housing-main:main'
            ]
        },
    )
    

    This will cause pip install to install a wrapper somewhere like /usr/local/bin/boston_housing which basically pulls in the module boston-housing-main and runs its main() function.

    You probably want to replace the scripts entry with this, though there is no reason per se you could not have both.