By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

Generating .pyc/.pyo from a make file

P: n/a
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,
--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/

Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tim Daneliuk wrote:
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
Jul 18 '05 #2

P: n/a
Roland Heiber wrote:
Tim Daneliuk wrote:
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


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

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #3

P: n/a
Tim Daneliuk wrote:
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
Jul 18 '05 #4

P: n/a
Roland Heiber wrote:
Tim Daneliuk wrote:
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


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
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.python.org/pycon/2005/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #5

P: n/a
Steve Holden wrote:
Roland Heiber wrote:
Tim Daneliuk wrote:
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

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 -

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #6

P: n/a
Tim Daneliuk wrote:
Steve Holden wrote:
Roland Heiber wrote:
Tim Daneliuk wrote:

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


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 -

That's what most sensible distributions do.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #7

P: n/a
Tim Daneliuk wrote:
Steve Holden wrote:
Roland Heiber wrote:
Tim Daneliuk wrote:

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.

--
Vincent Wehren

Jul 18 '05 #8

P: n/a
Roland Heiber wrote:
Tim Daneliuk wrote:
under the impression that "compiled" meant optimized byte code that


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

Greetings, Roland
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.