Generating .pyc/.pyo from a make file

T

Tim Daneliuk

I use a makefile to create distribution tarballs of freestanding Python
programs and their documentation. I cannot seem to find the right
command line option to just generate a pyc/pyo file from the program
and then exit. If I use 'python -OOOO -c"import myprog"' it creates
the pyo file, but myprog starts up and keeps running.

IOW, I need a batch method for generating compiled python. I know it
exists, but I can't find it for some reason ...

TIA,
 
R

Roland Heiber

Tim said:
I use a makefile to create distribution tarballs of freestanding Python
programs and their documentation. I cannot seem to find the right
command line option to just generate a pyc/pyo file from the program
and then exit. If I use 'python -OOOO -c"import myprog"' it creates
the pyo file, but myprog starts up and keeps running.

IOW, I need a batch method for generating compiled python. I know it
exists, but I can't find it for some reason ...

TIA,
Hi,

take a look at http://docs.python.org/lib/module-compileall.html

HtH, Roland
 
T

Tim Daneliuk

Roland said:

It does - thanks. One more question: Are pyc and pyo file portable
across operating systems? I suspect not since I generated a pyo
on a FreeBSD machine that will not run on a Win32 machine. I was
under the impression that "compiled" meant optimized byte code that
was portable across implementations, but it looks to not be the case...
 
R

Roland Heiber

Tim said:
It does - thanks. One more question: Are pyc and pyo file portable
across operating systems? I suspect not since I generated a pyo
on a FreeBSD machine that will not run on a Win32 machine. I was
under the impression that "compiled" meant optimized byte code that
was portable across implementations, but it looks to not be the case...

Hi,

..pyc's should be, cause it's standard python-bytecode, if you use
massive optimizations it depends not on the os but on the underlying
cpu/architecture ...

So long, Roland
 
S

Steve Holden

Roland said:
Hi,

..pyc's should be, cause it's standard python-bytecode, if you use
massive optimizations it depends not on the os but on the underlying
cpu/architecture ...

So long, Roland

You probably tried to use a bytecode file from *one* version of Python
with an interpreter of another version. Python actually checks the first
four bytes of the .pyc file for a compatible "magic number" before
accepting the file for execution.

regards
Steve
 
T

Tim Daneliuk

Steve said:
You probably tried to use a bytecode file from *one* version of Python
with an interpreter of another version. Python actually checks the first
four bytes of the .pyc file for a compatible "magic number" before
accepting the file for execution.

regards
Steve

Aha! Exactly ... and that makes perfect sense too. D'oh! I guess a better
distribution strategy would be to have the installation program generate the pyo
file at installation time...

Thanks -
 
S

Steve Holden

Tim said:
Aha! Exactly ... and that makes perfect sense too. D'oh! I guess a
better
distribution strategy would be to have the installation program generate
the pyo
file at installation time...

Thanks -
That's what most sensible distributions do.

regards
Steve
 
V

vincent wehren

Tim said:
Aha! Exactly ... and that makes perfect sense too. D'oh! I guess a
better
distribution strategy would be to have the installation program generate
the pyo
file at installation time...

Thanks -

Also, the *.py? files contain the full pathname of the *.py they have
been compiled from. Copying them to other path locations will give you
the wrong __file___ information in tracebacks.
 
R

Roland Heiber

You where right, i was totally mislead by "optimized" ... ;)

Greetings, Roland
 
J

Just

vincent wehren said:
Also, the *.py? files contain the full pathname of the *.py they have
been compiled from.
True.

Copying them to other path locations will give you
the wrong __file___ information in tracebacks.

This is not 100% accurate: yes, the traceback shows the original source
file path, yet module.__file__ does point to the actual .pyc file it was
loaded from.

Just
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top