.py and .pyc files in read-only directory

Discussion in 'Python' started by Terry, Oct 14, 2011.

  1. Terry

    Terry Guest

    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?
    Terry, Oct 14, 2011
    #1
    1. Advertising

  2. Terry

    Chris Rebert Guest

    On Fri, Oct 14, 2011 at 11:04 AM, Terry <> wrote:
    > 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
    Chris Rebert, Oct 14, 2011
    #2
    1. Advertising

  3. Terry

    Ian Kelly Guest

    On Fri, Oct 14, 2011 at 1:31 PM, Chris Rebert <> wrote:
    > On Fri, Oct 14, 2011 at 11:04 AM, Terry <> wrote:
    >> 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.


    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
    Ian Kelly, Oct 14, 2011
    #3
  4. Terry

    Terry Guest

    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
    Terry, Oct 14, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keithb
    Replies:
    2
    Views:
    7,987
    keithb
    Jun 7, 2006
  2. dmitrey
    Replies:
    1
    Views:
    330
    Arnaud Delobelle
    Sep 2, 2007
  3. Amit Gupta

    seperate directory for .pyc files

    Amit Gupta, Feb 16, 2008, in forum: Python
    Replies:
    2
    Views:
    277
    Jeff Schwab
    Feb 16, 2008
  4. Mike Kent
    Replies:
    0
    Views:
    612
    Mike Kent
    Apr 16, 2009
  5. Mircescu Andrei
    Replies:
    3
    Views:
    76
    Mircescu Andrei
    Feb 20, 2014
Loading...

Share This Page