Search code examples
pythoninitializationlazy-loading

Need help finding which script Python runs on startup


I'm trying to find which Python script loads whenever python is first run. For example, if you run python -v, you see some output like:

# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
import site # precompiled from /lib/python2.7/site.pyo
import os # precompiled from /lib/python2.7/os.pyo
import errno # builtin
import posix # builtin
import posixpath # precompiled from /lib/python2.7/posixpath.pyo
import stat # precompiled from /lib/python2.7/stat.pyo
import genericpath # precompiled from /lib/python2.7/genericpath.pyo
import warnings # precompiled from /lib/python2.7/warnings.pyo
import linecache # precompiled from /lib/python2.7/linecache.pyo
import types # precompiled from /lib/python2.7/types.pyo
import UserDict # precompiled from /lib/python2.7/UserDict.pyo
import _abcoll # precompiled from /lib/python2.7/_abcoll.pyo
import abc # precompiled from /lib/python2.7/abc.pyo
import _weakrefset # precompiled from /lib/python2.7/_weakrefset.pyo
import _weakref # builtin
import copy_reg # precompiled from /lib/python2.7/copy_reg.pyo
import traceback # precompiled from /lib/python2.7/traceback.pyo
import sysconfig # precompiled from /lib/python2.7/sysconfig.pyo
import re # precompiled from /lib/python2.7/re.pyo
import sre_compile # precompiled from /lib/python2.7/sre_compile.pyo
import _sre # builtin
import sre_parse # precompiled from /lib/python2.7/sre_parse.pyo
import sre_constants # precompiled from /lib/python2.7/sre_constants.pyo
import _sysconfigdata # precompiled from /lib/python2.7/_sysconfigdata.pyo
import encodings # directory /lib/python2.7/encodings
import encodings # precompiled from /lib/python2.7/encodings/__init__.pyo
import codecs # precompiled from /lib/python2.7/codecs.pyo
import _codecs # builtin
import encodings.aliases # precompiled from /lib/python2.7/encodings/aliases.pyo
import encodings.utf_8 # precompiled from /lib/python2.7/encodings/utf_8.pyo

I want to find the script that is running these commands, in order to lazy load some of the modules for use when running in a low memory environment. I think it might be built into libpython2.7.a which makes it difficult to lazy load. I have found that alot of these modules are object files such as

posixmodule.o errnomodule.o  pwdmodule.o  _sre.o  _codecsmodule.o  _weakref.o  zipimport.o  symtablemodule.o  xxsubtype.o

Which then gets compiled to produce the final libpython2.7.a.

What I would like is some place in the source, perhaps even before building it, where I can replace:

import linecache

or whichever the imports that can be deferred after loading, to

linecache = lazy_import.lazy_module("linecache").

Does anyone know where/if this script is in the Python source code? Thanks


Solution

  • You're probably after loading Python without site

    python -S
    

    You might also be interested in -s (lowercase), which the subset of USER_SITE packages