Google Colab + PyStan : asyncio.run() cannot be called from a running event loop

I am trying to run PyStan in Google Colab, and running into the error:
asyncio.run() cannot be called from a running event loop

This is the code I am running, copied from pystan.readthedocs.io

# copy-pasted from "https://pystan.readthedocs.io/en/latest/"

!pip install pystan
import stan

schools_code = """
data {
  int<lower=0> J;         // number of schools
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates
}
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
}
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood
}
"""

schools_data = {"J": 8,
                "y": [28,  8, -3,  7, -1,  1, 18, 12],
                "sigma": [15, 10, 16, 11,  9, 11, 10, 18]}

posterior = stan.build(schools_code, data=schools_data)
fit = posterior.sample(num_chains=4, num_samples=1000)
eta = fit["eta"]  # array with shape (8, 4000)
df = fit.to_frame()  # pandas `DataFrame, requires pandas

Error:
RuntimeError: asyncio.run() cannot be called from a running event loop

Please help!

Hi, see FAQ Frequently Asked Questions — pystan 3.5.0 documentation

@birajsubhraguha : note that Stan can also be run in Python with cmdstanpy. It works fine in Jupyter notebooks, so I assume also on Google Colab.

I’ve found Stan Notebooks in the Cloud with more info on running cmdstanpy on Google Colab. It’s from 2020, so I’m not sure if all the steps that are described there are still relevant/needed.