Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up the python "import" loader

Tags:

I'm getting seriously frustrated at how slow python startup is. Just importing more or less basic modules takes a second, since python runs down the sys.path looking for matching files (and generating 4 stat() calls - ["foo", "foo.py", "foo.pyc", "foo.so"] - for each check). For a complicated project environment, with tons of different directories, this can take around 5 seconds -- all to run a script that might fail instantly.

Do folks have suggestions for how to speed up this process? For instance, one hack I've seen is to set the LD_PRELOAD_32 environment variable to a library that caches the result of ENOENT calls (e.g. failed stat() calls) between runs. Of course, this has all sorts of problems (potentially confusing non-python programs, negative caching, etc.).

like image 795
10 revs, 7 users 53% Avatar asked Jan 06 '10 00:01

10 revs, 7 users 53%


People also ask

Does Python import lazy?

Imports inside class definitions or inside functions/methods are not “top level” and are never lazy. Dynamic imports using __import__() or importlib. import_module() are also never lazy.

What is __ loader __ in Python?

__loader__ is an attribute that is set on an imported module by its loader. Accessing it should return the loader object itself. In Python versions before 3.3, __loader__ was not set by the built-in import machinery. Instead, this attribute was only available on modules that were imported using a custom loader.


1 Answers

zipping up as many pyc files as feasible (with proper directory structure for packages), and putting that zipfile as the very first entry in sys.path (on the best available local disk, ideally) can speed up startup times a lot.

like image 185
Alex Martelli Avatar answered Oct 02 '22 13:10

Alex Martelli