views:

120

answers:

2

Hi All,

I am getting an Access is denied error while I am trying to run the .pyo file by double click or from the command prompt.

Lets say I have abc.py (keeping main method entry point) which imports files xyz.py and imports wx etc.

I generate the .pyo file. But once I try to run abc.pyo I get the access is denied error.

I am not getting why this happening? Any help will really appreciated.

(I am using windows xp as os). I am making .pyo from .py as following.

  1. I am having a .bat file CompileAllToPyo.bat which have python -O Compileall.py
  2. The Compileall.py keep the follwoing things

import os import compileall os.popen3(cmdLine, 'b') compileall.compile_dir('.', force=1)

This is all the info Thanks

+1  A: 

You don't "run" a .pyo file, as it's not an executable. You can give it to the python interpreter in lieu of the .py file, but in general, you should use a .py file as your entry point, so that the .pyc or .pyo file can be recreated when necessary.

$ python imported.pyo
Success!
$ ./imported.pyo
bash: ./imported.pyo: Permission denied
JimB
@JimB, actually, on Windows a .pyo file (as with a .pyc or a .py) effectively appears executable (to a rookie) because there's an association between the file extension and the Python interpreter. When you double-click the icon for the file you invoke the interpreter on it, much as the hash-bang line does on a posix-y system. And I suspect "access denied" means he's on a Windows box, but of course we hardly know anything useful from the question, so who knows...
Peter Hansen
Ahh! I didn't even think that he meant "run" the pyo file in that way.
Charles Duffy
@Peter - Thanks, I didn't know that Windows would do that (I've been clean going on 8 years ;).
JimB
My meaning of running .pyo is double click or by command prompt of that pyo file.
mukul sharma
+1  A: 

You can tell the system that your hw.pyo file is "executable", for example (in Linux, MacOSX, or any other Unix-y system) by executing the command chmod +w hw.pyo at the terminal shell prompt. Consider, for example, the following short and simple shell session:

$ cat >hw.py
print('hello world')
$ python2.5 -O -c'import hw'
hello world
$ ./hw.pyo
bash: ./hw.pyo: Permission denied
$ chmod +x hw.pyo
$ ./hw.pyo
hello world
$ 

By default, .pyo (and .pyc) files are not marked as executable because they're mostly meant to be imported, not directly executed (indeed, note that we're explicitly using a Python import statement to create the .pyo file!); however, as this example shows, it's quite easy to make one of them "executable as the main script". BTW, observe also:

$ cat >hw.py
print('hello world from ' + __name__)
$ python2.5 -O -c'import hw'
hello world from hw
$ chmod +x hw.pyo
$ ./hw.pyo
hello world from __main__
$ 

The __name__ is what tells the module whether it's being imported (so the first "hello world" says "from hw") or run as the main script (so the second one says "from __main__"). That's the reason modules that are designed to be used both ways normally end with if __name__ == '__main__': main() or the like, where main is a function that, this way, gets called iff the module's running as the main script (it's always best to have all substantial code execute in a function, not at a module's top level).

Alex Martelli