Using Python from Analytica
Revision as of 20:59, 19 August 2025 by Calvin.lu (talk | contribs) (→Importing Python modules and objects)
New in Analytica 7.0
Requires the Analytica Developer edition.
Introduction
- Short overview
- Motivation (i.e., rich ecosystem, many libraries)
- Use cases
- Advantages of using python from within Analytica vs. plain python.
- Downsides: When it is better to stick with Analytica syntax (dependencies, array abstraction, MC, optimization).
Setting up a Python environment
- Very brief basics.
conda create -n MyEnv python=3.11- Configuring Python Environment or Model Environment sysvars in Analytica.
- Link to Setting up a Python environment for detailed info.
Importing Python modules and objects
The preferred way to make external Python libraries available in Analytica is to place their import statements in the system variable PythonStartupCode. These lines execute whenever the Python interpreter starts, so the libraries are ready for use in later Python expressions.
Note: If you edit PythonStartupCode after Python has already been loaded in the current Analytica session, you must reload the model (or close and reopen Analytica) for the modified startup code to take effect.
Python supports two main import styles:
- Module import – e.g.,
import numpy - Selective / wildcard import – e.g.,
from numpy import array, zerosorfrom numpy import *
When an import statement is the final expression in a Python block, Analytica returns its value:
Notes
- For selective imports that rename objects (e.g.,
array as arr), the returned list shows the alias (arr) rather than the original name (array). - Wildcard imports can pollute the global namespace and make code harder to read; prefer importing only the symbols you need.
- If an import raises an exception (e.g., the package is not installed), the exception propagates into Analytica like any other Python error, halting evaluation of the calling expression.
The Python namespace
- The py:: namespace prefix
Calling a Python function
- py::F(...) calling syntax
- What print(...) does
Using Python classes
- Using an existing python class
- The -> operator to access fields, methods
Using Python code in Definitions
- The Python Expression view for definitions
- Writing an Analytica UDF with python code in body
Defining your own Python classes and Python functions
- Using "def" or "class" in python code.
- The Callable
- Developing your code outside of Analytica and importing as a module.
Dependencies
- The convenience of Analytica's dependencies.
- Drawing arrows to indicate dependence where Python doesn't track it.
- When dependencies are not tracked
Functions for evaluating Python code
Non-scalar data structures
- Distinction between Python data structures and Analytica data structures. How Python objects display in «...»
- Why to sometimes keep as python data structure w/o exploding
Converting from Python data to Analytica
- Automatic conversions (including list of types)
- PyExplode
- Basic usage. Nested usage.
- Include/exclude types
- Collections: np.array, dict, set/map, pandas data frame (documented on a separate library page), etc.
- Images & Figures, PIL.Image, matplotlib.figure.Figure
Converting from Analytica to Python data structures
Using Analytica locals from Python code
- When Analytica locals are visible to python code.
- Assignment to an Analytica local from python code.
Accessing Analytica objects from Python code
- Analytica module
- Get an Analytica object
- Evaluate a variable
- Analytica.eval(...) -- evaluate an analytica expression
- Call an Analytica function (?) -- Don't have something very direct here yet
Using Python in ACP or ADE
See also
Comments


Enable comment auto-refresher