Run pyc file without specifying python path ?

Dave Angel davea at ieee.org
Wed Jul 29 14:05:28 EDT 2009


Barak, Ron wrote:
> Hi,
>
> I wanted to make a python byte-code file executable, expecting to be able to run it without specifying "python" on the (Linux bash) command line.
>
> So, I wrote the following:
>
> [root at VMLinux1 python]# cat test_pyc.py
> #!/usr/bin/env python
>
> print "hello"
> [root at VMLinux1 python]#
>
> and made its pyc file executable:
>
> [root at VMLinux1 python]# ls -ls test_pyc.pyc
> 4 -rwxr-xr-x  1 root root 106 Jul 29 14:22 test_pyc.pyc
> [root at VMLinux1 python]#
>
> So, I see:
>
> [root at VMLinux1 python]# file test_pyc.py*
> test_pyc.py:  a python script text executable
> test_pyc.pyc: python 2.3 byte-compiled
> [root at VMLinux1 python]#
>
> If I try to do the following, no problem:
>
> [root at VMLinux1 python]# python test_pyc.pyc
> hello
> [root at VMLinux1 python]#
>
> However, the following fails:
>
> [root at VMLinux1 python]# ./test_pyc.pyc
> -bash: ./test_pyc.pyc: cannot execute binary file
> [root at VMLinux1 python]#
>
> Is there a way to run a pyc file without specifying the python path ?
>
> Bye,
> Ron.
>
>   
I don't currently run Unix, but I think I know the problem.

In a text file, the shell examines the first line, and if it begins #! 
it's assumed to point to the executable of an interpreter for that text 
file.  Presumably the same trick doesn't work for a .pyc file.

Why not write a trivial wrapper.py file, don't compile it, and let that 
invoke the main code in the .pyc file?

Then make wrapper.py executable, and you're ready to go.

DaveA




More information about the Python-list mailing list