.pyc & .pyo

Started by
7 comments, last by Triglav 20 years, 6 months ago
i''m coding project in C/C++ with embedded & extended python script, but I don''t know how to use .pyo files instead of .pyc can u advice me pls?
Triglav - Member of TAJGA Team
Advertisement
Create a foo.py file.
Start python.
Type import foo
A foo.pyc file is generated.

Start python -O or python -OO
Type import foo
A foo.pyo file is generated.

If the .py file still exists, when importing a module, python will automatically generate or use a matching .pyo or .pyc file.
Otherwise, I assume it uses the most recent - check the documentation.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
That''s fine, and really simple think, but I thought import .pyo modules to C/C++ mode. because when I use only .py script, embedded Python interpreter compiles it to .pyc; when I use compiled module files .pyc, it works fine, but I want use for distribution optimized modules - .pyo, and that''s my problem, because I don''t know how to include it.
Triglav - Member of TAJGA Team
quote:Original post by Triglav
That''s fine, and really simple think, but I thought import .pyo modules to C/C++ mode. because when I use only .py script, embedded Python interpreter compiles it to .pyc; when I use compiled module files .pyc, it works fine, but I want use for distribution optimized modules - .pyo, and that''s my problem, because I don''t know how to include it.


You don''t have to do anything. The interpreter will use them if they are there - or create them if it has been started with the -O option - that''s all.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
and which function should I use for -O
Triglav - Member of TAJGA Team
quote:Original post by Triglav
and which function should I use for -O


It''s not a function, it''s on the damned command line !

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I think you didn''t get what Triglav wants to do :

He can''t create the .pyo from command line, because the scripts import modules created in his engine, with extending.
So "compiling" outside of the engine will fail.

The question is how to force compilation of python script has .pyo from the C interface.

Triglav :
I don''t know the answer, but if I were you, I would look in the python code what''s happening when you type -O in the command line. It may be a single switch.

By the Way, is there a important performance difference between .pyc and .pyo ?
By the Way, is there a important performance difference between .pyc and .pyo ?

quote:Python tutorial 6.1.2

* When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in .pyo files. The optimizer currently doesn't help much; it only removes assert statements. When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.

* A program doesn't run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that's faster about .pyc or .pyo files is the speed with which they are loaded.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on October 12, 2003 7:07:45 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
By the Way, is there a important performance difference between .pyc and .pyo ?

I need .pyo (with -OO) files only because of my project is limiteed to 5MB, and there is a lot of data
Triglav - Member of TAJGA Team

This topic is closed to new replies.

Advertisement