#! shbang for pyc files?

M

MarkV

Is there a way to make it possible to execute a compiled python file
(whatever.pyc) on a linux/unix system without requiring the user to type
"python whatever.pyc"? In the case of a .py file, you can put "#!
/usr/bin/python" at the top and chmod +x, and then you don't even need the
..py extension to run it - just type "whatever" - but when you compile the
file, it loses this ability!

FYI, the context here is that we want to distribute scripts as part of an
application, but we don't want users to go mucking around with the scripts.
Yeah, I know, that's half the fun, but they don't get to muck around with
the C programs, and when they do muck around with the current shell scripts,
it causes us fits because their changes don't get into our source control.

Thanks in advance for any suggestions.
 
C

Christian Gudrian

MarkV said:
.py extension to run it - just type "whatever" - but when you compile the
file, it loses this ability!

Write a wrapper script:

#!/bin/sh
python mycompiledpythonprogram.pyc

Christian
 
J

Jeff Epler

On Linux, you can use binfmt_misc to recognize pyc/pyo files by magic
number and execute them directly. This was mentioned in some long-ago
release notes. The magic number is different in newer versios of
Python.

- The interpreter accepts now bytecode files on the command line even
if they do not have a .pyc or .pyo extension. On Linux, after executing

echo ':pyc:M::\x87\xc6\x0d\x0a::/usr/local/bin/python:' > /proc/sys/fs/binfmt_misc/register

any byte code file can be used as an executable (i.e. as an argument
to execve(2)).
[http://mail.python.org/pipermail/python-dev/2001-January/012044.html]

Long ago I wrote and submitted a patch to make Python recognize files
with a #! line followed by the magic number, but there seemed to be no
interest.
http://groups.google.com/[email protected]&output=gplain

Having
#!/usr/bin/env python
import myapp
myapp.main()
really won't spend much time byte-compiling, so normally it's not worth
worrying about.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAweL/Jd01MZaTXX0RAizKAJ9ecPx5o/gYqeE/Lpih0L2yWJe1JACfbm9a
v8vTVfGFIbr/GsXnDY4MK+Y=
=To9F
-----END PGP SIGNATURE-----
 
T

Thorsten Kampe

* MarkV (2004-06-05 16:56 +0100)
Is there a way to make it possible to execute a compiled python file
(whatever.pyc) on a linux/unix system without requiring the user to type
"python whatever.pyc"? In the case of a .py file, you can put "#!
/usr/bin/python" at the top and chmod +x, and then you don't even need the
.py extension to run it - just type "whatever" - but when you compile the
file, it loses this ability!

zsh: alias -s pyc=python
 
R

Roman Suzi

On Sat, 5 Jun 2004, Christian Gudrian wrote:

Then why not just use a wrapper Python program
which imports module and runs "main" function?
Write a wrapper script:

#!/bin/sh
python mycompiledpythonprogram.pyc

Christian

Sincerely yours, Roman Suzi
 
F

Fredrik Lundh

MarkV said:
FYI, the context here is that we want to distribute scripts as part of an
application, but we don't want users to go mucking around with the scripts.
Yeah, I know, that's half the fun, but they don't get to muck around with
the C programs, and when they do muck around with the current shell scripts,
it causes us fits because their changes don't get into our source control.

here's one way to do it:

http://www.pythonware.com/products/python/squeeze/

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top