This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: .PYO files not imported unless -OO used
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: jeffsasmor, jhylton
Priority: normal Keywords:

Created on 2002-06-18 15:31 by jeffsasmor, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (2)
msg11244 - (view) Author: Jeff Sasmor (jeffsasmor) Date: 2002-06-18 15:31
If you execute a program where all the files/modules 
are .pyo, that is, compiled while the interpreter had been 
started with the -OO command-line option (and presumably -
O as well), then imports fail unless the interpreter had been 
started with -OO (-O?) during that run of the program.

Thus, if you want to distribute a program in its smallest form 
where you might run a 'build' script that uses the compileall
built-in to create a distribution fileset that comprises 
only .pyo files, you'd obviously need to run that script with 
the Python interpreter in -OO 'mode'.  

When you you try to run the target program from the 
distribution fileset, you are required to start the interpreter 
in -OO mode or all imports fail since the interpreter does not 
search for that extension unless -OO has been used. One 
workaround is to rename all the .pyo files to .pyc files, but 
this is somewhat silly....  This sitution is undocumented 
AFAIK. 

Additionally, it would be nice to be able to switch this 
optimize mode on/off programmatically.  In the app I am 
working on (an 'IDE' for Python) I store code objects 
(created by using the compile built-in)  in the ZODB.  If I 
have to run the interpreter in -OO mode then I lose debug 
information that I'd like to keep. Hence it would be nice to 
be able to turn this mode on and off.
msg11245 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-07-24 16:01
Logged In: YES 
user_id=31392

I'm not sure I understand what you want to see changed.

If you only distributed .pyo files, then the python must
indeed by invoked with -O to be able to use these files.  In
general, Python programs are distributed as .py files that
can be used with or without -O.  If you want to distribute
without source but work in either case, distribute the .pyc
and .pyo.

The compiled code is different in .pyc and .pyo files, which
is why there are two different files.  The size of these
files is small compared to the size of most disk drives, so
I don't see why you would worry about a factor of 2 in size
of total distribution.

The -O flag affects the way the code is compiled.  assert
stmts, for example, generate no compiled code in -O mode. 
It isn't practical to turn this on and off at runtime.  In
projects where we store code in ZODB, we use a dictionary
keyed on __debug__ (and Python version) to make sure the
compiled code object is compatible with the current interpreter.
History
Date User Action Args
2022-04-10 16:05:25adminsetgithub: 36765
2002-06-18 15:31:12jeffsasmorcreate