Up until recently, I haven't done a lot with python since college. I wrote the occasional script here and there. Anytime that I did, I ended up using the standard python argparse libraries and just ran it with
python FooScript.py. However I always wanted to write a real CLI utility. I put it off because of how much work it would be. Then I wrote one and found out its only a few extra lines of code.
Making a utility
Organize the code
Just make a folder called whatever you want the CLI utility to be called and put the main source code in there in a file called
__init__.py. The main code should be in a function called
Then in the folder above that, create
setup.py with a very simple setup:
from setuptools import setup, find_packages setup( name='WHATEVERYOUCALLEDTHATFOLDER', version='0.1', py_modules=['WHATEVERYOUCALLEDTHATFOLDER'], packages=find_packages(), install_requires=['YOURREQUIREDPKGS',], author="YOURPyPiAccountUsername", author_email="YOUREMAIL", entry_points=''' [console_scripts] WHATEVERYOUCALLEDTHATFOLDER=WHATEVERYOUCALLEDTHATFOLDER:cli ''', )
After its written, anyone could download it from source and use
pip install --editable . and get it working. However how could I make it so it was even simpler? The pypi website was always foreboding to me since it looks like a hacker board from 1996. Once again, I was overthinking it. Just make an account, create a package submission on their site, then run a few commands:
- Register the repo:
python setup.py register -r pypi
- Upload the code:
python setup.py sdist upload -r pypi
So in summary, I've been running away from signing up on a website, making one extra .py file, and running like 2 extra CLI commands.
And because I now know that, now I've written several over the last few weeks.