Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a python package from terminal

I have a python package like this:

package/
   setup.py 
   deb/
   build/
   sound/
     __init__.py
     run.py
     config.py
     readaudio.py    

inside run.py:

#! /usr/bin/env python   

start():
    ...do something
resume():
    ....do something
if __name__=="__main__":
   start()

I have built a package and installed it, now I want to run the installed package from command line. Something like

$ ./sound.run

or

$ python sound.run.resume

I want to be able to do this system-wide ( or in a virtualenv), since that's the point of installing it. I know the commands above wouldn't work, but I hope this conveys the idea. I want to call the program not the functions within the python env.

But I am not sure how I can run it from command line without using something like this:

$ python -c " from sound import run; run.start();"

Any suggestions, is that even possible?

like image 432
carefullynamed Avatar asked Feb 23 '26 09:02

carefullynamed


1 Answers

You can use setuptools's entry points feature to create a console script for your project

setup(name=project_name,
    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
    include_package_data=True,
    zip_safe=False,
    entry_points="""
    [console_scripts]
    sound-run = sound.run:start
    sound-resume = sound.run:resume
    """
    # Other setuptools stuff
    )
like image 77
yorodm Avatar answered Feb 24 '26 22:02

yorodm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!