pyc files not automatically compiled on import

Discussion in 'Python' started by Baz Walter, Jul 26, 2009.

  1. Baz Walter

    Baz Walter Guest

    hello

    i thought that python automatically compiled pyc files after a module is
    successfully imported. what could prevent this happening?


    Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.mkdir('/home/baz/tmp/foo')
    >>> os.chdir('/home/baz/tmp/foo')
    >>> f = open('foo.py', 'w')
    >>> f.write('print "hello world"\n')
    >>> f.close()
    >>> os.listdir('.')

    ['foo.py']
    >>> import foo

    hello world
    >>> os.listdir('.') # why no pyc file?

    ['foo.py']
    >>> import py_compile
    >>> py_compile.compile('foo.py')
    >>> os.listdir('.')

    ['foo.py', 'foo.pyc']
    Baz Walter, Jul 26, 2009
    #1
    1. Advertising

  2. Baz Walter

    Fuzzyman Guest

    On Jul 26, 5:22 pm, Baz Walter <> wrote:
    > hello
    >
    > i thought that python automatically compiled pyc files after a module is
    > successfully imported. what could prevent this happening?
    >
    > Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25)
    > [GCC 4.3.2] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >  >>> import os
    >  >>> os.mkdir('/home/baz/tmp/foo')
    >  >>> os.chdir('/home/baz/tmp/foo')
    >  >>> f = open('foo.py', 'w')
    >  >>> f.write('print "hello world"\n')
    >  >>> f.close()
    >  >>> os.listdir('.')
    > ['foo.py']
    >  >>> import foo
    > hello world
    >  >>> os.listdir('.') # why no pyc file?
    > ['foo.py']
    >  >>> import py_compile
    >  >>> py_compile.compile('foo.py')
    >  >>> os.listdir('.')
    > ['foo.py', 'foo.pyc']


    Works for me I'm afraid (Mac OS X and Python 2.6).

    Michael Foord
    --
    http://www.ironpythoninaction.com/
    Fuzzyman, Jul 26, 2009
    #2
    1. Advertising

  3. Baz Walter

    Peter Otten Guest

    Baz Walter wrote:

    > i thought that python automatically compiled pyc files after a module is
    > successfully imported. what could prevent this happening?
    >
    >
    > Python 2.6.1 (r261:67515, Apr 12 2009, 03:51:25)
    > [GCC 4.3.2] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> import os
    > >>> os.mkdir('/home/baz/tmp/foo')
    > >>> os.chdir('/home/baz/tmp/foo')
    > >>> f = open('foo.py', 'w')
    > >>> f.write('print "hello world"\n')
    > >>> f.close()
    > >>> os.listdir('.')

    > ['foo.py']
    > >>> import foo

    > hello world
    > >>> os.listdir('.') # why no pyc file?

    > ['foo.py']
    > >>> import py_compile
    > >>> py_compile.compile('foo.py')
    > >>> os.listdir('.')

    > ['foo.py', 'foo.pyc']


    You did not set the PYTHONDONTWRITEBYTECODE environment variable in a former
    life, or did you?
    Peter Otten, Jul 26, 2009
    #3
  4. Baz Walter

    Baz Walter Guest

    Peter Otten wrote:
    > You did not set the PYTHONDONTWRITEBYTECODE environment variable in a former
    > life, or did you?


    thanks peter

    no i didn't, but i've just discovered a script in /etc/profile.d that
    did. now i'll have to try to find out how that script got in there :-|
    Baz Walter, Jul 26, 2009
    #4
  5. Baz Walter

    Aahz Guest

    In article <>,
    Baz Walter <> wrote:
    >
    >i thought that python automatically compiled pyc files after a module is
    >successfully imported. what could prevent this happening?


    Looks like you got your problem fixed, but for the record, not having
    write permission on a directory also causes this. It's even uglier when
    the .pyc already exists but does not have write perms.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Many customs in this life persist because they ease friction and promote
    productivity as a result of universal agreement, and whether they are
    precisely the optimal choices is much less important." --Henry Spencer
    Aahz, Jul 27, 2009
    #5
    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. Tony C
    Replies:
    2
    Views:
    290
    Robert M. Emmons
    Feb 13, 2004
  2. Jason Smith

    Prevent pyc or pyo compiled output

    Jason Smith, Jun 25, 2004, in forum: Python
    Replies:
    2
    Views:
    370
    Peter Otten
    Jun 25, 2004
  3. Shankar
    Replies:
    1
    Views:
    703
    Serge Orlov
    May 5, 2006
  4. Anastasios Hatzis

    Out-dated compiled modules (*.pyc)?

    Anastasios Hatzis, Aug 26, 2006, in forum: Python
    Replies:
    3
    Views:
    553
    Cliff Wells
    Aug 26, 2006
  5. Laszlo Nagy

    Module baldy compiled to pyc?

    Laszlo Nagy, Sep 27, 2012, in forum: Python
    Replies:
    1
    Views:
    206
    Steven D'Aprano
    Sep 27, 2012
Loading...

Share This Page