show/hide this revision's text 3 clarify question in body

I noticed importing an executable Python file saves a *.py[co] file, as expected, but that file also is marked executable. Randomly, I wondered if I could execute this file directly, and it worked!

Steps to duplicate:

$ echo 'print "Worked."' > testcase.py
$ chmod u+x testcase.py
$ python -c 'import testcase'
Worked.
$ ./testcase.pyc
Worked.

How did this work? Why didn't I get an error message? Is my system, Ubuntu 9.04, doing something special, or am I forgetting something on how Linux works in general?

$ python --version
Python 2.6.2
$ hd testcase.pyc
00000000  d1 f2 0d 0a a4 4f 08 4b  63 00 00 00 00 00 00 00  |.....O.Kc.......|
00000010  00 01 00 00 00 40 00 00  00 73 09 00 00 00 64 00  |.....@...s....d.|
00000020  00 47 48 64 01 00 53 28  02 00 00 00 73 07 00 00  |.GHd..S(....s...|
00000030  00 57 6f 72 6b 65 64 2e  4e 28 00 00 00 00 28 00  |.Worked.N(....(.|
00000040  00 00 00 28 00 00 00 00  28 00 00 00 00 73 0b 00  |...(....(....s..|
00000050  00 00 74 65 73 74 63 61  73 65 2e 70 79 74 08 00  |..testcase.pyt..|
00000060  00 00 3c 6d 6f 64 75 6c  65 3e 01 00 00 00 73 00  |..<module>....s.|
00000070  00 00 00                                          |...|
00000073

(Including hexdump to show there's no shebang line, and reading about .pyc format, there's nothing here that isn't highly specific to Python.)

show/hide this revision's text 2 clarify duplication testcase; added 301 characters in body; added 9 characters in body

I noticed importing an executable Python file saves a *.py[co] file, as expected, but that file also is marked executable. Randomly, I wondered if I could execute this file directly, and it worked!

Duplicate

Steps to duplicate:

$ touch empty.py; echo 'print "Worked."' > testcase.py
$ chmod u+x empty.py
testcase.py
$ python -c 'import emptytestcase'
$ ls -og empty.py
-rwxr----- 1  0 2009-11-21 15:25 empty.py*
-rwxr----- 1 95 2009-11-21 15:25 empty.pyc*
Worked.
$ ./empty.pyc

(There isn't any output, as expected.)/testcase.pyc Worked.

How? Is my system, Ubuntu 9.04, doing something special, or am I forgetting something on how Linux works in general?

$ python --version
Python 2.6.2
$ hd empty.pyc
testcase.pyc
00000000  d1 f2 0d 0a ae 41 a4 4f 08 4b  63 00 00 00 00 00 00 00  |.....A.Kc.......|
.....O.Kc.......|
00000010  00 01 00 00 00 40 00 00  00 73 04 09 00 00 00 64 00  |.....@...s....d.|
00000020  00 47 48 64 01 00 53 28  01 02 00 00 00 73 07 00 00  |.GHd..S(....s...|
00000030  00 57 6f 72 6b 65 64 2e  4e 28 00 00 00 00 28 00  00  |.S(....N(....(..|
00000030  .Worked.N(....(.|
00000040  00 00 00 28 00 00 00 00  28 00 00 00 00 73 08 00 0b 00  |..(....(....s...|
00000040  ...(....(....s..|
00000050  00 00 74 65 6d 70 73 74 79 63 61  73 65 2e 70 79 74 08 00  |..testcase.pyt..|
00000060  00 00 3c 6d |.empty.pyt....<m|
00000050  6f 64 75 6c  65 3e 01 00 00 00 73 00  |..<module>....s.|
00000070  00 00 00                                          |odule>....s....|
0000005f
...|
00000073

(Including hexdump to show there's no shebang line, and reading about .pyc format, there's nothing here that isn't highly specific to Python.)

show/hide this revision's text 1

How do *.pyc files execute?

I noticed importing an executable Python file saves a *.py[co] file, as expected, but that file also is marked executable. Randomly, I wondered if I could execute this file directly, and it worked!

Duplicate:

$ touch empty.py; chmod u+x empty.py
$ python -c 'import empty'
$ ls -og empty.py
-rwxr----- 1  0 2009-11-21 15:25 empty.py*
-rwxr----- 1 95 2009-11-21 15:25 empty.pyc*
$ ./empty.pyc

(There isn't any output, as expected.)

How? Is my system, Ubuntu 9.04, doing something special, or am I forgetting something on how Linux works in general?

$ python --version
Python 2.6.2
$ hd empty.pyc
00000000  d1 f2 0d 0a ae 41 08 4b  63 00 00 00 00 00 00 00  |.....A.Kc.......|
00000010  00 01 00 00 00 40 00 00  00 73 04 00 00 00 64 00  |.....@...s....d.|
00000020  00 53 28 01 00 00 00 4e  28 00 00 00 00 28 00 00  |.S(....N(....(..|
00000030  00 00 28 00 00 00 00 28  00 00 00 00 73 08 00 00  |..(....(....s...|
00000040  00 65 6d 70 74 79 2e 70  79 74 08 00 00 00 3c 6d  |.empty.pyt....<m|
00000050  6f 64 75 6c 65 3e 01 00  00 00 73 00 00 00 00     |odule>....s....|
0000005f