How do we execute any script in Python?
$ python do_something.py $ python do_something_with_args.py gfg vibhu
Probably that’s how you do it.
If your answer was that you just click a button on your IDE to execute your Python code, just assume you were asked specifically how you do it on command line.
Let’s make it easier for you.
$ do_something $ do_something_with_args gfg vibhu
That sure looks a lot cleaner. Basically, they are just your python scripts converted to command line tools. In this article, we will discuss how you can do that yourself.
The process can be broken down in these 6 steps:
- Create your Command Line Script.
- Set-up files and folder structure for Packaging.
- Modify your
setup.pyfile to incorporate your CLI scripts.
- Test your package before publishing and then Build.
- Upload on pypi and publish your package.
- Install your newly-published package.
The first two steps are comprehensively covered in their respective articles. It’s recommended to have a look at them before moving forward. This article will mainly continue from Step 3.
Step #1: Make your Command Line Script
Step #2: Set-up files and folder Structure
Step #3: Modify setup.py file
Setuptools allows modules to register entrypoints (
entry_points) which other packages can hook into to provide certain functionality. It also provides a few itself, including the
console_scripts entry point.
This allows Python functions (not scripts!) to be directly registered as command-line accessible tools!
Step #4: Test & Build
Test: Change the directory to top-level of your package, the same one with
Install your intended package by typing in this command.
$ python3 setup.py install
This will install your package if there are no errors in setup.
Now you can test all the functionalities of your package. If anything goes wrong, you can still fix things up.
Build: Make sure you have upgraded pip version along with latest
wheel. Now use this command to build distributions of your package.
$ python3 setup.py sdist bdist_wheel
Step #5: Publish the package
twine is a library that helps you upload your package distributions to pypi. Before executing the following command, make sure you have an account on PyPI
$ twine upload dist/*
Provide the Credentials and Done! You just got your first Python package published on PyPI.
Step #6: Install the package
Now install your newly published package by using
$ pip install vibhu4gfg
$ gfg usage: gfg [-h] [-greet] [--sum] N [N ...] gfg: error: the following arguments are required: N $ gfg -h usage: gfg [-h] [-greet] [--sum] N [N ...] GfG article demo package. positional arguments: N an integer for the accumulator optional arguments: -h, --help show this help message and exit -greet Greet Message from Geeks For Geeks. --sum sum the integers (default: find the max) $ gfg 5 10 15 -greet Welcome to GeeksforGeeks! The Computation Done is Maximum And Here's your result: 15 $ gfg 5 10 15 -greet --sum Welcome to GeeksforGeeks! The Computation Done is Summation And Here's your result: 30
- Python | Set 6 (Command Line and Variable Arguments)
- Command Line Interface Programming in Python
- Python | How to Parse Command-Line Options
- Click Module in Python | Making awesome Command Line Utilities
- Packaging and Publishing Python code
- Python | Line (Canvas) in kivy
- Python PIL | ImageDraw.Draw.line()
- Python OpenCV | cv2.line() method
- Multi-Line printing in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Transpose a matrix in Single line in Python
- Intesting Fact about Python Multi-line Comments
- How to input multiple values from user in one line in Python?
- Multiplication of two Matrices in Single line using Numpy in Python
- Line detection in python with OpenCV | Houghline method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.