user1952219
user1952219

Reputation:

Python call program from command line

How do I call a python program from the command line? Where the program should be?

What's the difference between the python GUI and the command line?

Upvotes: 0

Views: 387

Answers (2)

zmo
zmo

Reputation: 24812

python yourscript.py

or you can prepend to your script the following:

#!/usr/bin/env python

and then run chmod +x yourscript.py in commandline. Good editors can do it automatically for you.

When you run a script from a GUI (like nautilus), it executes it because it has the execute flag on, and the shebang thing within, otherwise the GUI won't recognize it.

Finally, to "install" it as a CLI command, you have to put it in one of the directories of the $PATH environment variable (usually, /usr/local/bin, /usr/bin or /bin).

But a good way to make your script correctly installed, you'd better package it using a setup.py file, here's a tutorial: http://pythonhosted.org/an_example_pypi_project/setuptools.html‎. It will help you to put all your python scripts where it belongs, and create scripts in standard directories for you to run them as commands... You can even create menu items links for your window manager, so one can just double click to launch your app once installed!

Upvotes: 2

dan
dan

Reputation: 1002

python yourscript.py

The GUI just facilitates execution.

Upvotes: 0

Related Questions