1

Instead of typing

$ python3 Program.py -<flags> arguments, etc

I want to be able to DL the git clone and then be able to type

$ Program -<flags> arguments, etc 
# program name without the .py extension

I've seen other programs have .yaml files, req.txt files and dockerized them but I can't find anything that shows me how to do this. All the tutorials and guides have stopped short of how to make them simple command line programs.

I've done all the argparse, etc but I'm looking for a guide or some instruction of how to dockerize it and simply run it without having to nav to the dest folder

torek
  • 448,244
  • 59
  • 642
  • 775
Jameson Welch
  • 31
  • 1
  • 4
  • I'm using a Mac and a Linux system by the way. I know Windows may have a different method but I can figure that out later when I need it. – Jameson Welch Oct 03 '19 at 15:29
  • 3
    for a unix, you just make program.py executable (`chmod +x program.py`) and add a shebang on top of the file: `#! /usr/bin/env python3` – Sergio Tulentsev Oct 03 '19 at 15:32
  • Have a look: https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take – ababak Oct 03 '19 at 15:33
  • This has nothing to do with Git itself, so I snipped off the [tag:git] tag. – torek Oct 03 '19 at 16:38

2 Answers2

3

If you're thinking about distributing the program, you should probably add CLI entry points in your package's setup.py file.

For example:

Project structure

ROOT/
- setup.py
- src/
    - program.py

src/program.py

# program.py

def main():
    pass

setup.py

# setup.py

from setuptools import find_packages, setup

setup(
    name='my_program',
    version='1.0.0',
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'Program=src.program:main'
        ]
    }
)

The important bit is the line 'Program=src.program:main': it associates the name Program (the name to invoke from the command line) with the function main of src/program.py.

Note that this name could be anything - it doesn't necessarily need to be related to your package name, python file names, etc.

You can perform a local installation of your package in order to test this.

From the ROOT directory, type $ pip install -e . Afterwards, typing

$ Program

in the terminal from any directory will execute the main function from src/program.py.

This behaviour is the same if anyone pip installs your package over PyPI, instead of your local installation.

jfaccioni
  • 7,099
  • 1
  • 9
  • 25
1

Add the shebang to the top of the file:

#!/bin/python3  # or wherever your python binary is

If you have that, then you could do:

./Program.py -<flags> arguments etc
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241