py vs pyc

S

Sakcee

Hi

I want to know that is .pyc files execute faster than .py files,? since
both are interpreted by python , is there any speedup in using one or
other.?

e.g.

what is the difference between runing from cmd "python test.py" and
"python test.pyc"


thanks
 
T

Tomasz Lisowski

Sakcee said:
Hi

I want to know that is .pyc files execute faster than .py files,? since
both are interpreted by python , is there any speedup in using one or
other.?

e.g.

what is the difference between runing from cmd "python test.py" and
"python test.pyc"


thanks
When running "python test.py" the interpreter will first precompile the
test.py source file, and then execute it. When running "python
test.pyc", the interpreter will go straight to the execution of the script.

Tomasz
 
M

mrmakent

Sakcee said:
what is the difference between runing from cmd "python test.py" and
"python test.pyc"

When you run 'python test.py', the python interpreter first looks to
see if 'test.pyc' (which is the byte-code compiled version of
'test.py') exists, and if it is more recent than 'test.py'. If so, the
interpreter runs it. If it does not exist, or 'test.py' is more recent
than it (meaning you have changed the source file), the interpreter
first compiles 'test.py' to 'test.pyc'.

So you can see that you don't need to directly run 'test.pyc'; the
interpreter handles all that for you. Except for the very first time
you run 'test.py' after creating or updating it, python will
automactically run the pre-compiled version, the '.pyc'.

There is one exception to all the above: If you put '#! /usr/bin/env
python' on the first line of 'test.py', make it executable, and then
run 'test.py' by itself (without explicitly invoking 'python', then
'test.py' does not get compiled to 'test.pyc', meaning that the
compilation step has to be done every time you run 'test.py' this way.
 
F

Fredrik Lundh

When you run 'python test.py', the python interpreter first looks to
see if 'test.pyc' (which is the byte-code compiled version of
'test.py') exists, and if it is more recent than 'test.py'.

nope.

the import statement does this, but the Python interpreter doesn't (running
test.py as a script isn't the same thing as importing it as a module). the inter-
preter does recognize a compiled file, though:

$ cat >q.py
print "hello"
$ python2.4 q.py
hello
$ ls q.pyc
ls: q.pyc: No such file or directory
$ python2.4 -c "import q"
hello
$ ls q.pyc
q.pyc
$ python2.4 q.pyc
hello

however, this only works if you're passing in a filename:

$ python2.4 <q.py
hello
$ python2.4 <q.pyc
sys:1: DeprecationWarning: Non-ASCII character '\xf2' in file <stdin> on line 1,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for
details
File "<stdin>", line 1
m=
^
SyntaxError: invalid syntax

and it usually won't work if you use another Python version:

$ python2.3 q.pyc
RuntimeError: Bad magic number in .pyc file

</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

No members online now.

Forum statistics

Threads
473,798
Messages
2,569,651
Members
45,384
Latest member
GOLDY

Latest Threads

Top