Using Python from Analytica/Setting up a Python environment

< Using Python from Analytica
Revision as of 22:41, 22 August 2025 by Calvin.lu (talk | contribs) (Created page with "This page explains how to prepare a Conda-based Python environment and configure Analytica to use it. Python 3.8+ is '''recommended''' for better compatibility and support. ''...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This page explains how to prepare a Conda-based Python environment and configure Analytica to use it. Python 3.8+ is recommended for better compatibility and support. Other Python installations can be used if you provide the full Python home directory path in Analytica’s Python system variables.

What is Conda, Anaconda, and Miniconda

Conda is a cross-platform package and environment manager that lets you create isolated Python environments and install packages with precise dependency control. Anaconda and Miniconda are distributions that include Conda: - Anaconda bundles many scientific packages and tools out of the box and includes the Anaconda Navigator GUI. - Miniconda is a minimal installer that provides Conda and Python, letting you add only the packages you need.

Choosing Anaconda vs. Miniconda

- Anaconda: Convenient for those who prefer a GUI and want many packages preinstalled. Heavier download and larger disk footprint. - Miniconda: Lightweight download and smaller footprint. Install only what you need via conda install.

We do not provide step-by-step GUI instructions. The examples below use the Conda command line.

Installing Conda

Download and run the Anaconda or Miniconda installer.

IMAGE

Creating a Python environment

Open the Anaconda Prompt and create a Python environment named MyEnv with commonly used libraries:

conda create -n MyEnv python=3.11 pandas numpy

Python 3.8+ is recommended. You may choose a different version (e.g., python=3.12) if desired.

About conda-forge: conda-forge is a community-maintained collection of Conda packages. You can install from it when required (e.g., conda install -n MyEnv -c conda-forge package_name). Mixing channels can sometimes cause dependency conflicts; prefer a consistent channel strategy.

Pip inside Conda: Installing some packages with pip inside the Conda environment can be acceptable, but prefer conda install when possible for better dependency management.

Activating the environment and installing more packages

Analytica does not require activating the environment in a shell to use it, but activation is useful when installing or testing packages interactively.

To activate the environment and add more packages to the existing environment: conda activate MyEnv conda install requests pip install some_package_only_on_pip

You can add or update packages at any time after the environment is created.

Telling Analytica which Python environment to use

Open the Definition / Application integration / Python submenu. Set Python Model Environment (preferred for portability and easy switching) or Python Environment (installation-wide default). Enter either the environment’s name (e.g., MyEnv) or the full Python home folder path (e.g., C:\Users\username\anaconda3\envs\MyEnv). When only a name is provided, Analytica searches the default Anaconda and Miniconda environment locations. If none of the Python system variables are set, Analytica looks first in the Windows User registry, then the Machine registry, for a Python home. If both are set, Python Model Environment takes precedence and overrides the installation-wide setting.

If Python was already initialized in this session, close and reopen the model or restart Analytica after changing the environment.

Verifying from within Analytica

After setting Python Model Environment (recommended), run a minimal verification. The following prints the Python, pandas, and NumPy versions. Only the last line is output.

import sys import pandas as pd import numpy as np _ = sys.version /* optional capture to avoid extra output */ _ = pd.__version__ np.__version__

Troubleshooting

- Python environment not found: An error is thrown if the specified name or path is not found. See Python Environment Not Found. - Packages not found or conflicts:

 - Prefer conda install first.
 - Use -c conda-forge for packages not available on default channels, keeping channel usage consistent.
 - Use pip install as a last resort within a given environment, understanding it mixes package managers.

- Multiple Python installations: This page focuses on Conda-managed environments. Other installations can be used if you set Python Environment or Python Model Environment to the full Python home directory path of that installation.

Note: Activation in a shell is optional for Analytica usage; ensure the environment exists and Analytica’s Python system variables are set correctly.

Comments


You are not allowed to post comments.