6

I have a system with very little resources (embedded). Because of that I only installed the python .pyo files (=byte compiled & optimized) for my program. Now when such a program runs, python keeps looking for .py files (probably to see if the .pyo needs to be updated). The funny thing is that it does that a lot: 25000 stat64() calls (and 8304 getcwd calls()) in 5 minutes!

getcwd("/tmp", 1026)                = 9
getcwd("/tmp", 1026)                = 9
stat64("MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("/usr/local/lib/python2.5/MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("/usr/local/lib/python25.zip/MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("/usr/local/lib/python2.5/MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("/usr/local/lib/python2.5/plat-linux2/MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("/usr/local/lib/python2.5/lib-tk/MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("/usr/local/lib/python2.5/lib-dynload/MyProgram.py", 0xbeb94b0c) = -1 ENOENT (No such file or directory)
stat64("MyProgram.py", 0xbeb94cc8) = -1 ENOENT (No such file or directory)
getcwd("/tmp", 1026)                = 9
stat64("MyProgram.py", 0xbeb94bc4) = -1 ENOENT (No such file or directory)

How can I prevent python from doing this?

Caleb
  • 70,105

1 Answers1

4

You could try replacing the __builtins__.__import__ function (which is called by import statements) with your own code. You can use the imp.load_module function to load the py/pyc/pyo file.

Here is a really simple version of it:

import sys
import imp

real_import = __builtins__.__import__

def pyc_only_import(name, globals = globals(), locals = locals(), fromlist = [], level = 0):
    pycname = name + ".pyc"
    modfile = open(pycname)
    return imp.load_module(name, modfile, pycname, (".pyc", "rb", 2))

__builtins__.__import__ = pyc_only_import

import hello

This is far from a full implementation of __import__ but it works if there is a hello.pyc file in the current working directory.

stribika
  • 5,454