When the PyXLL add-in is loaded it imports the modules configured in the pyxll.cfg file. If it takes a long time to import these modules that will impact how quickly the add-in loads, and how quickly Excel starts.
If Excel is loading slowly, the first thing to check is how long each module is taking to load, and to do that you need to enable debug logging and look at the log file.
1. Determine what's taking a long time
To enable debug logging, update the following log setting in your pyxll.cfg file:
[LOG]
verbosity = debugOnce you have done that, restart Excel. In your PyXLL log file you will find entries for when each import was started, and when it was completed. You can use these to determine how long each module is taking to load. Once you know which modules are taking a long time to load you can then work on making them import faster!
The most common reason modules take a long time to load is because they are importing large or heavy dependencies such as pandas or scipy.
The Python command line option "-X importtime" can be useful to identify which modules are taking a long time to import (https://docs.python.org/3/using/cmdline.html#cmdoption-X). This option can be enabled in PyXLL using the xoptions setting in the PYTHON section of your pyxll.cfg file:
[PYTHON]
xoptions =
importtimeOnce you know which configured modules are taking a long time to load, and have identified which imports are causing them to be slow, you can restructure your code to improve how long it takes to import.
2. Speed up imports
Often these dependencies don't actually need to be imported when the module is imported, and so one technique to speed up the import time is "lazy importing". By importing modules lazily the actual import is deferred until the module is actually needed.
In Python 3.15 modules can be imported lazily using "lazy import" (see https://docs.python.org/3/whatsnew/3.15.html#whatsnew315-lazy-imports). For example,
# Lazily import this so as not to slow down importing this module
lazy import some_large_dependency
@xl_func
def func():
# The module is only imported when needed
some_large_dependency.function(...)
...Versions of Python prior to 3.15 don't have lazy import included as a language feature, but there are other ways to achieve the same thing. For example, the following works in earlier versions of Python:
@xl_func
def func():
# import this now, when needed, instead of in the module body
import some_large_dependency
some_large_dependency.function(...)
...Other Potential Causes
- Code in the body of a module
Anything you do inside the body of a module will be executed when the module is imported.
If you have something that takes a long time on import, such as loading data from a database or file, that will slow down Excel when it starts.
Using the information about module import times collected above, examine your slowest loading module and add logging to it so you can see how long different parts of it are taking to load.
Move anything slow out from the body of the module and only do that work when needed, not every time the module is imported.
- Finding "setuptools entry points"
Python has a mechanism for discovering installed packages with certain advertised features -- setuptools entry points!
PyXLL uses these to look for packages with functions that should be exposed to Excel (see https://www.pyxll.com/docs/userguide/distribution.html#entry-points).
If your Python environment is particularly large, or on a network drive, scanning these entry points can take some time. The debug log file collected earlier will show how long looking for these entry points is taking.
- Using a slow network drive
If you are storing your Python modules, or your Python environment, on a network drive then that will affect how long it takes to import your Python modules.
Instead of using a network drive, consider moving the files to a local drive. You can use a startup script (see https://www.pyxll.com/docs/userguide/distribution.html#startup-script-for-deployment) to deploy your code to the local PC and keep it up to date.
Even having a slow network drive listed in your pythonpath can affect import times. If you have some code on a network drive, but most of your code on the local PC, ensure the network drive is listed on your pythonpath last. That way, Python will only have to scan the network drive after it's checked the local drives rather than scanning the network drive before checking the local drive.