[Tutor] py_compile and chmod +x

bob gailer bgailer at gmail.com
Fri Dec 19 18:12:46 CET 2008


Emad Nawfal (عماد نوفل) wrote:
> Hi Tutors,
> #! /usr/bin/env python
> print "Hello Tutors"
>
> I have this script saved as hello.py. Why can  I execute it, but not 
> the compiled version?  or am I doing something wrong? Just curious. 
> Any help appreciated.

There are 2 issues here.

1 - The shell inspects the first line of the file (#! /usr/bin/env 
python) to find  the program to run
      The compiled file does not have this first line.

2 - The python interpreter expects a script not a compiled program.
      So even if you got the interpreter to run it would try to treat 
the file as a script and bomb anyway.

However when you import hello then it looks first for hello.pyc, and 
runs that. If hello.pyc is missing or older than hello.py it will 
compile hello.py and then run hello.pyc.
> For example :
>
>
> emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod  +x  hello.py
> emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.py
> Hello Tutors
>
> Now I compile it in Python:
> >>> import py_compile
> >>> py_compile.compile("hello.py")
> >>>
> emad at emad-laptop:~/Desktop/Programming/Haskell$ chmod +x hello.pyc
>
> emad at emad-laptop:~/Desktop/Programming/Haskell$ ./hello.pyc
> : command not found: �
> ./hello.pyc: line 2: syntax error near unexpected token `('
> ./hello.pyc: line 2: `�KIc@s    dGHdS(s
>                                          Hello 
> TutorsN((((hello.py<module>s'
> emad at emad-laptop:~/Desktop/Programming/Haskell$
>
>
> -- 
> لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه 
> كالحقيقة.....محمد الغزالي
> "No victim has ever been more repressed and alienated than the truth"
>
> Emad Soliman Nawfal
> Indiana University, Bloomington
> --------------------------------------------------------
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
Chapel Hill NC 
919-636-4239



More information about the Tutor mailing list