简体   繁体   中英

Access is denied error with pregenerated .pyc or .pyo files

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

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 import ed (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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM