SolverStudio & CPython

SolverStudio can integrate with a version of Python installed on your machine independently of any SolverStudio setup. For example, you might have installed ActivePython or a version of Python downloaded from http://python.org. These are technically called “CPython” installations (to distinguish them from the IronPython that SolverStudio has built in), and are referred to by SolverStudio as “Python (external)” in the Language menu.

SolverStudio will work with Python versions 2.7 and later (including version 3 and later). Note that version 3 introduced many changes, is not compatible with all older code, and is not yet as popular as version 2.7.

You can see in the SolverStudio… About box under “CPython Path:” the path to any version of CPython found on your machine. This is the Python that will be used when the “Python (external)” language is selected.

SolverStudio makes it easy to work with Excel from CPython. If you include:
from SolverStudio import *
at the start of your model, then you can access all the named data items on the sheet in the normal way (i.e. just as if you were running with the language “Pulp (IronPython)”). You can also use “Application“, “ActiveSheet” and “ActiveWorkbook” to refer to Excel, the current sheet and the current workbook respectively. (Use of “Application”, “ActiveSheet” and “ActiveWorkbook” requires you to have “Python for Windows Extensions” installed on your machine. SolverStudio will give a warning if this is not installed, but will still work with Data Items in the usual way.)

An an example, you can use the following code to access a named range on the active sheet:

print ActiveSheet.Range("Test").Value2

Code written for IronPython using “Application“, “ActiveSheet” and “ActiveWorkbook” will often work without change under CPython. However, in some cases, IronPython requires a trailing (). We have used .Value2 above because this works identically under both IronPython and CPython. However, if we use .Value, then it must be ActiveSheet.Range(“Test”).Value in CPython and ActiveSheet.Range(“Test”).Value() (with brackets) in IronPython.

You can test your SolverStudio CPython support by: choosing the Data tab in Excel, selecting Show Model, then File… View SolverStudio Examples.  Then open the “Python Examples.xlsx” example. Select the “External Python” tab, and then click “Solve Model”. Output that includes “## CPython run completed successfully” indicates success.

Advanced Usage SolverStudio provides dictionaries to help work with the data items. These are DataItemsDictionaryDataItemsListDataItems, and SingletonDataItems, and are described on the Developer Support page.  If you look at the “SolverStudio.py” file that is created when you run CPython, you will see that these are initialised with references to the data items themselves. However, unlike the IronPython case, any changes to the data items will result in new objects being created, and so their values will differ from those stored in these dictionaries.

2 thoughts on “SolverStudio & CPython

  1. Hello, I wanted to use a model written in python (anaconda) together with SolverStudio. But how is it possible to combine the power of SolverStudio witht the model written in python? I use the python interpreter of Anaconda. Is there an example that I can study?

Leave a Reply

Your email address will not be published. Required fields are marked *