Simon Norton
Simon Norton

Reputation: 125

Run a python wheel from CLI as if it were a single script

I wish to make a python wheel to upload as a spark-submit job in Azure Databricks, but I can't validate my wheel is working. I don't understand where or how a call to the wheel finds the __main__ method.

How do I build the package and/or call the wheel file on the command line in a way that the main method gets run?

Below is a simple package I'm attempting; all it does is echo today's date.

Some of the commands I've tried to run the script:

python dist/today-0.0.1-py3-none-any.whl/
python dist/today-0.0.1-py3-none-any.whl/__main__
python dist/today-0.0.1-py3-none-any.whl/main

I've tried a lot of variations on naming the main file main.py and __main__.py as well as naming the method main or __main__, but everything gives me the same error: C:\Python391\python.exe: can't find '__main__' module in 'C:\<DIRECTORYPATH>\\dist\\today-0.0.1-py3-none-any.whl'

The package consists of an empty __init__.py and main.py which looks like:

import datetime

def main():
    print(f'Today is {datetime.date.today()}')

if __name__ == '__main__':
    main()

My directory structure is:

Wheeltest
  |-- setup.py
  |-- today
       |-- __init__.py
       |-- __main__.py

I've unzipped the wheel file and can confirm that it has a top level of the folder today with the 2 py files inside it. My setup.py file looks like this (I've also tried without an entry_points section):

from setuptools import setup
from setuptools import find_packages

VERSION = '0.0.1'
DESCRIPTION = 'today package.'
LONG_DESCRIPTION = 'today dist.'

# Setting up
setup(
    name='today',
    version=VERSION,
    author='Simon Norton',
    author_email='<[email protected]>',
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    packages=find_packages(),
    entry_points={
        'console_scripts': ['main=today.__main__:main']
    },
    classifiers=['Development Status :: Testing',
        'Programming Language :: Python :: 3',
        'Operating System :: Microsoft :: Windows',
        'Operating System :: Linux']
)

Many thanks!

Upvotes: 3

Views: 6092

Answers (1)

Simon Norton
Simon Norton

Reputation: 125

The path to the wheel file must contain the top-level folder inside the wheel to find __main__ From my Example, the top level folder is called "today":

C:\<DIRECTORYPATH>\\dist\\today-0.0.1-py3-none-any.whl\\today

Upvotes: 2

Related Questions