Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: compile into an Unix commandline app

I am not sure if I searched for the wrong terms, but I could not find much on this subject. I am on osx and I'd like to compile a commandline python script into a small commandline app, that I can put into usr/local/bin so I can call it from anywhere. Is there a straighforward way to do that?

Thanks

like image 715
moka Avatar asked Nov 25 '11 16:11

moka


People also ask

How do I compile Python code in Unix?

You can also automatically compile all Python files using the compileall module. You can do it from the shell prompt by running compileall.py and providing the path of the directory containing the Python files to compile: monty@python:~/python$ python -m compileall .

Can Python run in UNIX?

You cannot use UNIX commands in your Python script as if they were Python code, echo name is causing a syntax error because echo is not a built-in statement or function in Python. Instead, use print name . To run UNIX commands you will need to create a subprocess that runs the command.

Can Python be run at a command line?

To start a Python interactive session, just open a command-line or terminal and then type in python , or python3 depending on your Python installation, and then hit Enter .


1 Answers

The simplest way to do this is to rename your file from filename.py to filename and add this line to the top of the file.

#!/usr/bin/env python

You may also need to set the executable bit on the file, which may be set using chmod +x on the command line.

like image 68
Andrew Marsh Avatar answered Sep 23 '22 17:09

Andrew Marsh