0

I have a Python file called recognize.py, which takes multiple images and performs OCR with multiple processes, where ocr_tools is a large self-written module:

import argparse
import json
from multiprocessing import Pool
from ocr_tools import Organize
organizer = Organize()

parser = argparse.ArgumentParser(description="Recognize the contents on a lab report and return a json string.")
parser.add_argument("-i", "--image", nargs="*", required=True, help="path to input image file")
args = parser.parse_args()

if __name__ == "__main__":
    with Pool() as p:
        report_set = json.dumps({"report_set": p.map(organizer.finalize_report, args.image)}, ensure_ascii=False)
        print(report_set)

For ocr_tools, I also have a requirements.txt generated by pipreqs for users to install the required packages from PyPI:

numpy==1.14.3
regex==2018.2.21
requests==2.18.4
pandas==0.23.0
opencv_python==3.4.1.15
scikit_learn==0.19.1

If I distribute these files and modules, users should be able to execute python recognize.py --image <path_to_an_image> from the command lines and get OCR results after they install those packages listed in requirements.txt.

Now I wonder if there's a way to further enable users to do something like ocr --image <path_to_an_image>, i.e. the same job without specifying python interpreter in the command lines.

I've read a tutorial, How Do I Make My Own Command-Line Commands Using Python? but it seems effective only locally and cannot be distributed. I've also glanced Writing the Setup Script, hoping to find solutions for distribution. However, I don't find a way to specify external packages to be installed from PyPI. (Or maybe I missed something - I am a beginner in Python.) Besides, setup.py seems like neither a good way to install the packages according to comments under the accepted answer of What is setup.py?, nor being capable of enabling CLI.

ytu
  • 1,822
  • 3
  • 19
  • 42
  • There are numerous ways to package Python scripts, which platform(s) is this targeting? – l'L'l Jun 13 '18 at 04:56
  • @l'L'l It should be on Linux Ubuntu. – ytu Jun 13 '18 at 04:58
  • If your Python package/script gets installed in a standard place you could simply create an alias for it (eg. `alias ocr=/path/to/recognize.py`)... – l'L'l Jun 13 '18 at 05:08
  • @l'L'l Yes I am aware of that approach and that's somehow like my attached tutorial. However that's still effective only on _my_ workstation. For others who get the scrips (from GitHub for example), they need to set this by themselves. I am seeking a way to do all the needed settings for _them_. – ytu Jun 13 '18 at 05:13

0 Answers0