xlOil Python Distributing Addins¶
We might like to distribute our code as a packaged addin so users don’t have to edit their xlOil.ini to load it. To do this, run the following at a command prompt:
xloil create myaddin.xll
This will create a myaddin.xll and myaddin.ini in the current directory. By default, the XLL will try to load myaddin.py in the same directory, so let’s create it:
import xloil as xlo
@xlo.func
def MySum(x, y, z):
'''Adds up numbers'''
return x + y + z
Now drop myaddin.xll into an Excel session and try to use MySum
.
Users of myaddin.xll will need access to a python distribution which contains the xlOil package, however they do not need the xlOil XLL addin installed.
In fact the myaddin.ini created by the above process is much more complicated that necessary. The minimum required file is simply:
Plugins = ["xlOil_Python"]
This relies on the user having an %APPDATA%\xlOil\xlOil.ini
which gives
the location of the xlOil binaries and the python installation to use. If
you want to change the python location or remove the requirement that the
xlOil addin is installed, see below.
Pointing the addin to a python distribution¶
You can modify the PYTHONEXECUTABLE
and PATH
environment variables in
myaddin.ini to point to any python installation, for example of a shared drive
for ease of distribution.
However, if a user of myaddin.xll has an ini file at %APPDATA%\xlOil\xlOil.ini
the core xlOil.dll is loaded using those settings before myaddin.ini is
processed. The assumption that the user has the xlOil addin installed in Excel.
Since only one instance of xlOil and only one python interpreter can be hosted in Excel, one settings file must take precedence. You can make your addin take precedence using the LoadBeforeCore flag in myaddin.ini.
Installing the Addin¶
If you copy myaddin.xll and myaddin.ini to your %APPDATA%\Microsoft\Excel\XLSTART
directory, you’ll notice that Excel opens the ini file, which is not ideal! xlOil will
look for myaddin.ini in %APPDATA%\xlOil
, so install the ini to there. You’ll also need
to choose a directory to hold your python modules and ensure myaddin.ini points to it:
%APPDATA%\xlOil\myaddin
could be a sensible choice.