pythonpython-wheel

Python wheel entry point not working as expected on Windows


I'm tryring to setup a python wheel for my testrunner helper script to make it easyly acessible from everywhere on my Windows machine. Therefore I configure a console entry point in my setup.py. I can see the generated entry point in the entry_points.txt but if I'm trying to invoke my script I get the error message.

No module named testrunner.__main__; 'testrunner' is a package and cannot be directly executed

My installer folder tree looks like this

setup.py
README.md
LICENSE.txt
testrunner/
    __init__.py
    testrunner.py
    templates/
        testDesc.json

The testrunner.py looks like this

def runTests():
    print("Hello World")
    #More not relevant code here

if __name__ == '__main__':
    runTests()

The setup.py content

from setuptools import find_packages, setup
from pathlib import Path

# read the contents of your README file
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()
        
setup(
    name='testrunner',
    version='0.3.0',
    packages=find_packages(include=['testrunner']),

    description='C/C++ test runner',
    long_description=long_description,
    long_description_content_type='text/markdown',

    author='Me',

    license="Proprietary",
    license_files = ('LICENSE.txt',),
    
    entry_points={
        'console_scripts': ['testrunner = testrunner:main']
    },

    classifiers=[
        'Topic :: Software Development :: Build Tools',
        'Topic :: Software Development :: Compilers',

        'Private :: Do Not Upload',
        'Operating System :: Microsoft :: Windows',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research ',
        'Intended Audience :: Education ',
        'Natural Language :: English',
        'License :: Other/Proprietary License',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: 3.10',
    ],
    install_requires=[],
    package_data={'':['templates\\*.json']},
)

And finally the __init__.py

from .testrunner import main, runTests

I build the wheel with the command: py -m pip wheel --no-deps -w dist . After installation with pip and checking the content in the site-packages directory executing py -m testrunner -h results in No module named testrunner.__main__; 'testrunner' is a package and cannot be directly executed


Solution

  • No module named testrunner.__main__; 'testrunner' is a package and cannot be directly executed

    You don't have __main__.py so you cannot do python -m testrunner. To fix the problem:

    echo "from .testrunner import runTests" >testrunner/__main__.py
    echo "runTests()" >>testrunner/__main__.py
    

    The second problem:

    from .testrunner import main, runTests

    You don't have main in testrunner.py. Code if __name__ == '__main__': doesn't create any main. You only have runTests to import. So change the import to

    from .testrunner import runTests
    

    and change your setup.py:

        entry_points={
            'console_scripts': ['testrunner = testrunner:runTests']
        },