It's often helpful to have different environments so that you can test changes and let other users test your changes before deploying updates to a production environment.
See also How can I distribute my PyXLL functions to other users? and Managing multiple PyXLL configs.
- Using the "pyxll activate" command
- Using different pyxll.cfg files
- Making it clear what environment is in use
Using the "pyxll activate" command
You can maintain separate code bases for your different environments, and even different Python interpreters (which can be of different Python versions), and switch between them before starting Excel.
You can have completely separate PyXLL instances, configured independently, and switch between them using the "pyxll activate" command.
This is the easiest way to switch between different PyXLL instances and have complete separation between your environments.
The "pyxll activate" command is documented here https://www.pyxll.com/docs/userguide/installation/cli.html#pyxll-activate.
For example, to switch to a PyXLL instance installed in a dev folder you would do the following:
> pyxll activate "C:\Your Folder\Dev PyXLL Environment"
If using different Python environments it can be easier to reference the Python command using the full path, instead of relying on the "pyxll" command to be on the path. An alternative way to run "pyxll activate" is as follows:
> C:\path\to\python.exe -m pyxll activate "C:\Your Folder\Dev PyXLL Environment"
You could script this in a .bat file if you wanted to launch Excel after activating a specific PyXLL instance.
You can use the "--non-interactive" switch to "pyxll activate" to prevent it prompting for any user input.
The "--force" option will force the script to update the Excel options even if an Excel process is already running (be careful though as Excel will save the options when Excel closes, which can result in confusing behavior if an already running Excel instance is closed after calling "pyxll activate" as the options will be re-written with the previous settings).
Using different pyxll.cfg files
Another way is to leave the same pyxll.xll add-in file installed and switch which config file is used.
You can have different config files for each environment, pointing at different code folders and modules, and even different Python installations (of the same Python version).
See also Managing multiple PyXLL configs
To tell the PyXLL add-in to load a specific config file, set the "PYXLL_CONFIG_FILE" environment variable.
This can be done in a script before launching Excel, for example:
REM run-excel-dev.bat
REM Starts Excel with the dev pyxll config file
SET PYXLL_CONFIG_FILE=X:\pyxll\pyxll-dev.cfg
START /x "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
The "/x" switch used above when launching Excel starts a new Excel instance, rather than opening an already running Excel window.
You can create shortcuts on your desktop to these .bat files to make it easy to launch Excel with different settings.
Relative paths used in the config file are always relative to the location of the config file, and not the location of the pyxll.xll add-in.
Making it clear what environment is in use
If you are switching between environments it may not be immediately obvious which environment you are currently using just by looking at Excel.
Setting the name of the environment as an environment variable in your different pyxll.cfg files is a good way to keep track of what environment is active.
See also Environment Variables
One way to make it clear is to change the name of the ribbon tab in your add-in based on the current environment being used. You can do this using the "getLabel" attribute on the ribbon tab in your ribbon xml file instead of the usual "label" attribute.
For example, in your ribbon xml file you might have something that looks like this:
...
<tabs>
<tab id="myAddin" label="My Excel Add-in">
...
You can use "getLabel" instead to call a Python function that will return the label to use, for example:
...
<tabs>
<tab id="myAddin" getLabel="pymodule.getRibbonLabel">
...
Now in a Python module ("pymodule.py" in this case) you write the "getRibbonLabel" function, which can use the environment variable containing the environment name, for example:
import os
def getRibbonLabel(control):
env = os.environ.get("MY_PYXLL_ENVIRONMENT_NAME") # dev/qa/prod/etc
return f"My Excel Add-in [{env}]"
See also Customizing the Ribbon