.py and .pyc files in read-only directory

T

Terry

I'm having a problem with my iPhone/iPad app, Python Math, a Python
2.7 interpreter. All the Python modules are delivered in what Apple
calls the app bundle. They are in a read-only directory. This means
that Python cannot write .pyc files to that directory. (I get a deny
write error when doing this.) I tried using compileall to precompile
all the modules, but now I get an unlink error because Python
apparently wants to rebuild the .pyc files.

I've considered two solutions:
1) Delete all the .py files, delivering only the .pyc, or
2) Redirecting the .pyc files into a separate, writable directory.

Will solution 1) work? I don't know how to do 2) and the only
reference I can find to that are a withdrawn PEP, 304.

Suggestions?
 
C

Chris Rebert

I'm having a problem with my iPhone/iPad app, Python Math, a Python
2.7 interpreter. All the Python modules are delivered in what Apple
calls the app bundle. They are in a read-only directory. This means
that Python cannot write .pyc files to that directory. (I get a deny
write error when doing this.) I tried using compileall to precompile
all the modules, but now I get an unlink error because Python
apparently wants to rebuild the .pyc files.

You can stop Python from trying to write .pyc files by using the
environment variable PYTHONDONTWRITEBYTECODE, the interpreter's -B
command line option, or by setting sys.dont_write_bytecode to True.

Cheers,
Chris
 
I

Ian Kelly

You can stop Python from trying to write .pyc files by using the
environment variable PYTHONDONTWRITEBYTECODE, the interpreter's -B
command line option, or by setting sys.dont_write_bytecode to True.

This won't make Python use the .pyc files provided, though. It will
just recompile the .py files and then not try to write out the
bytecode. If you really want to force it to use the .pyc's, then
don't include the .py files. Note that if you do this, you'll need to
make sure that the version of Python used to compile the .pyc files is
the same minor release as the version used to run them (more
specifically, the two versions must return the same string from
imp.get_magic()).

HTH,
Ian
 
T

Terry

Thanks, that's very useful. And it explains why Python Math wants to rewrite the .pyc files: imp.get_magic() returns (null) whereas on my Mac where I compiled them, get_magic() returns '\x03\xf3\r\n'.

Now I just have to figure out why I'm getting nothing useful from get_magic().

I assume this would have to be fixed to try solution 1), i.e., leaving out the .py files and delivering only the .pyc.

Terry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top