Discussion:
How to compile a python script to *.pyo
Peter Hansen
2003-04-02 20:25:45 UTC
Permalink
I'm new on this, please bear with we.
--------------------------
#!/usr/bin/env python
x = 'Hello Python'
print x,
--------------------------
How can I compile this script to *.pyo?
What's the name of the script?
Peter Hansen
2003-04-03 18:52:23 UTC
Permalink
I want my users run *.pyo or *.pyc files instead of *.py.
To expand on Jp's response a little: please explain why you think
you want this, so we can better understand the need that's driving it.
Maybe you are trying to do something you shouldn't be trying
to do, or maybe you're just going about it the wrong way. What
makes you think doing what you are asking is a good thing?
-Peter
I am working in test environment, and I didn't like testers alter my
script (this happened to me). Basicly, I want my scripts are under control
with my knowledge. If the problem come up, I know where to start
troubleshooting.
Hmm... when we have a situation where someone does something inappropriate,
we ask them not to, and explain why. Generally they don't do it again...

Better than using individual .py[oc] files then would be to package
the program using py2exe, wouldn't it? Are these people testing this
code itself, or using the code in the process of testing something
else?

If they're testing this code, then they're idiots and should
be fired if they go changing it on you.

If they are using the code as a tool for testing something else,
you should probably have a proper release process for the tool,
involving revision control, version numbers, and a nice little
packaging and installation sequence which prevents the whole issue
from happening in the first place.

Anyway, what makes you think that they won't do things like install
their own replacement .py files which do whatever they want, rather
than use your .pyc file, if they're such tricky types that they would
change your .py files on you?

(My advice: use education of the users to solve your problem, not
fancy technical solutions.)

-Peter
danny
2003-04-02 21:39:37 UTC
Permalink
I'm new on this, please bear with we.
--------------------------
#!/usr/bin/env python
x = 'Hello Python'
print x,
--------------------------
How can I compile this script to *.pyo?
Thanks in advance,
Danny
python -O yourscript.py
Generally this doesn't work. You have to import
a module (or use other techniques) for it to be compiled.
Python does not store a .pyc or .pyo file for the main
module, perhaps surprisingly.
-Peter
--
http://mail.python.org/mailman/listinfo/python-list
What are other techniques I can use?
I want my users run *.pyo or *.pyc files instead of *.py.

-Danny
Jp Calderone
2003-04-02 21:55:03 UTC
Permalink
[snip]
What are other techniques I can use?
I want my users run *.pyo or *.pyc files instead of *.py.
Why?

Jp
--
Lowery's Law:
If it jams -- force it. If it breaks, it needed replacing anyway.
--
up 13 days, 18:00, 7 users, load average: 0.10, 0.09, 0.08
danny
2003-04-04 02:43:34 UTC
Permalink
I'll ignore the why's and wherefore's and give you what I consider a
straight answer.....
pydoc compileall
Python Library Documentation: module compileall
NAME
compileall - Module/script to "compile" all .py files to .pyc (or
.pyo) file.
FILE
/usr/local/lib/python2.2/compileall.py
DESCRIPTION
When called as a script with arguments, this compiles the
directories given as arguments recursively; the -l option prevents
it from recursing into directories.
Without arguments, if compiles all modules on sys.path, without
recursing into subdirectories. (Even though it should do so for
packages -- for now, you'll have to deal with packages separately.)
See module py_compile for details of the actual byte-compilation.
FUNCTIONS
compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None)
Byte-compile all modules in the given directory tree.
..... snip .....
I use this for yes you've guessed it compiling my applications, on the
fly as they are installed.
Regards
Martin
For Martin,
Thanks for your advice. That's what I need.
If this mailing list got more people like you, there are less junk
messages to read.
Steven Taschuk
2003-04-04 04:42:07 UTC
Permalink
Quoth danny at iamlearning.com:
[...]
Post by danny
For Martin,
Thanks for your advice. That's what I need.
If this mailing list got more people like you, there are less junk
messages to read.
I wonder whether you intended to imply that the previous responses
were "junk". If you did, I urge you to reconsider; the previous
posters were actually being helpful. It is unfortunate (for you)
if you don't realize that.
--
Steven Taschuk Aral: "Confusion to the enemy, boy."
staschuk at telusplanet.net Mark: "Turn-about is fair play, sir."
-- _Mirror Dance_, Lois McMaster Bujold
Matt Gerrans
2003-04-03 06:08:48 UTC
Permalink
I wanted to release some Python scripts that would be on a very large number
of systems and had to be reliable. I wanted to have have at least a little
protection from tinkering, so used this little compiler:

# pycompile.py
if __name__=='__main__':
import os
exec( 'import %s' %
os.path.splitext(os.path.basename(os.sys.argv[1]))[0])

It was run as part of a release script which put together the package as
well as updating version stamps and whatnot.

Later I added a little MD5 self-check of the whole package (many Python and
other files), so the Python scripts can be included, but the first thing I
can ask someone when they report that my stuff "doesn't work" is to look at
a log file and see if it says anything about failing a self-integrity check.
That lets me know whether I'm the guilty party or they are, which is a good
starting point. ;-)

By the way, this is the kind of script you only use on an internal project
that you use yourself, because it uses exec() on a command line parameter
(probably a bad idea in most cases) and has no error handling. It could
also be improved to use glob(), for instance, if you want to compile many
scripts in one swell foop.
Peter Hansen
2003-04-02 22:43:57 UTC
Permalink
I want my users run *.pyo or *.pyc files instead of *.py.
To expand on Jp's response a little: please explain why you think
you want this, so we can better understand the need that's driving
it. Maybe you are trying to do something you shouldn't be trying
to do, or maybe you're just going about it the wrong way. What
makes you think doing what you are asking is a good thing?

-Peter
Nagy László Zsolt
2003-04-03 18:37:24 UTC
Permalink
I want my users run *.pyo or *.pyc files instead of *.py.
To expand on Jp's response a little: please explain why you think
you want this, so we can better understand the need that's driving it.
Maybe you are trying to do something you shouldn't be trying
to do, or maybe you're just going about it the wrong way. What
makes you think doing what you are asking is a good thing?
-Peter
I am working in test environment, and I didn't like testers alter my
script (this happened to me). Basicly, I want my scripts are under control
with my knowledge. If the problem come up, I know where to start
troubleshooting.
You should sing your code in a way. E.g. with M2Crypto or PyCrypto. You
can keep your private key's
password. You can re-sign your files when you distribute them to testers
and you can check anytime if they
changed something. Note: you should include a last modification date in
your file to be totally secure
because they are able to replace a file/signature pair with an older
pair of the same file.

Laci 1.0

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030403/f122006d/attachment.html>
danny
2003-04-03 17:47:32 UTC
Permalink
I want my users run *.pyo or *.pyc files instead of *.py.
To expand on Jp's response a little: please explain why you think
you want this, so we can better understand the need that's driving it.
Maybe you are trying to do something you shouldn't be trying
to do, or maybe you're just going about it the wrong way. What
makes you think doing what you are asking is a good thing?
-Peter
I am working in test environment, and I didn't like testers alter my
script (this happened to me). Basicly, I want my scripts are under control
with my knowledge. If the problem come up, I know where to start
troubleshooting.

Thanks,

Philippe Lafoucrière
2003-04-02 20:59:59 UTC
Permalink
I'm new on this, please bear with we.
--------------------------
#!/usr/bin/env python
x = 'Hello Python'
print x,
--------------------------
How can I compile this script to *.pyo?
Thanks in advance,
Danny
python -O yourscript.py
A. Lloyd Flanagan
2003-04-08 14:14:55 UTC
Permalink
Peter Hansen <peter at engcorp.com> wrote in message news:<3E8B53BA.DBB0C84C at engcorp.com>...
...
Generally this doesn't work. You have to import
a module (or use other techniques) for it to be compiled.
Python does not store a .pyc or .pyo file for the main
module, perhaps surprisingly.
-Peter
Yes, that's surprising. Does anyone happen to know why this is the
case? It doesn't make sense to me.
Peter Hansen
2003-04-02 21:18:50 UTC
Permalink
I'm new on this, please bear with we.
--------------------------
#!/usr/bin/env python
x = 'Hello Python'
print x,
--------------------------
How can I compile this script to *.pyo?
Thanks in advance,
Danny
python -O yourscript.py
Generally this doesn't work. You have to import
a module (or use other techniques) for it to be compiled.
Python does not store a .pyc or .pyo file for the main
module, perhaps surprisingly.

-Peter
Irmen de Jong
2003-04-05 19:42:32 UTC
Permalink
Post by danny
For Martin,
Thanks for your advice. That's what I need.
If this mailing list got more people like you, there are less junk
messages to read.
Are you serious? If you are, I think you clearly
misunderstood the other replies you got in this thread.

Peter Hansen's remark:
"(My advice: use education of the users to solve your problem, not
fancy technical solutions.)"

is very valuable advice in my opinion, and not 'junk' at all!

--Irmen de Jong
Martin Franklin
2003-04-03 19:24:07 UTC
Permalink
I'm new on this, please bear with we.
--------------------------
#!/usr/bin/env python
x = 'Hello Python'
print x,
--------------------------
How can I compile this script to *.pyo?
I'll ignore the why's and wherefore's and give you what I consider a
straight answer.....

pydoc compileall

Python Library Documentation: module compileall

NAME
compileall - Module/script to "compile" all .py files to .pyc (or
.pyo) file.

FILE
/usr/local/lib/python2.2/compileall.py

DESCRIPTION
When called as a script with arguments, this compiles the directories
given as arguments recursively; the -l option prevents it from
recursing into directories.

Without arguments, if compiles all modules on sys.path, without
recursing into subdirectories. (Even though it should do so for
packages -- for now, you'll have to deal with packages separately.)

See module py_compile for details of the actual byte-compilation.

FUNCTIONS
compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None)
Byte-compile all modules in the given directory tree.


..... snip .....



I use this for yes you've guessed it compiling my applications, on the fly
as they are installed.



Regards
Martin
Klaus Alexander Seistrup
2003-04-03 07:31:47 UTC
Permalink
How can I compile this script to *.pyo?
See the py_compile and compileall modules.


// Klaus
--
<> unselfish actions pay back better
danny
2003-04-02 18:40:19 UTC
Permalink
I'm new on this, please bear with we.

Let's say I have this script:

--------------------------
#!/usr/bin/env python
x = 'Hello Python'

print x,

--------------------------

How can I compile this script to *.pyo?

Thanks in advance,

Danny
Loading...