Skip to content
Advertisement

How can I setup a python CLI application so that I can use it without directly referring to the interpreter?

I want to build an application, say it’s called helloworld, with a command line interface in python. My application as multiple nested modules and a top level module main.py.

Say the layout is :

helloworld/
   helloworld/
      __init__.py
      module1/
      module2/
      module3/
   setup.py
   main.py

At this point, once my project is installed I can run it using #> python path/to/helloworld/main.py arg1 arg2 arg3

I want to be able to interact with the program using commands such as : #> helloworld arg1 arg2 arg3, in a similar manner as for example, the Flask framework comes with a command line application I can use with #> flask run.

I looked around and found questions suggesting using a shebang. As far as I can tell, this is not what I want.

I want a self contained application I can install using pip and then launch directly without any reference to the python interpreter.

How should I go about this ?

EDIT

It is an important requirement to me that the package can be installed using pip and the command line interface is immediately available. Shebangs/fiddling manually with the path/creating an additional shell script do not solve my problem.

Advertisement

Answer

You should add main.py to your PATH. What happens when you are running flask run is that your teminal looks up the command flask in PATH and runs the program that it is pointing to. You could see it as a kind of shortcut to the program Flask.

By adding your program to your PATH, you can tell the computer that if you type helloworld in your terminal, the terminal should run /my/path/to/helloworld.py.

I don’t know what OS you are on, so here are links for most common OS on how to add a PATH variable.

Windows

Linux

Mac OSX

Edit: After you gave more explanation, setuptools is what you are looking for. Please look at this reference to see how to use it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement