Model parses/compiles in rstan but not in pystan

When I try:

# This fails
import pystan
sm = pystan.StanModel(model_code='model.stan')

I get this error: PARSER EXPECTED: whitespace to end of file.

Doing the same thing in R works successfully however:

# This works
sm <- stan_model('model.stan')

I’m at a loss. All this is on the same MacOS computer.

Python 3.6 info:

pystan.__version__ :

R session info:

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.17.3       StanHeaders_2.17.2 ggplot2_2.2.1     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16     grid_3.5.0       plyr_1.8.4       gtable_0.2.0     stats4_3.5.0     scales_0.5.0     pillar_1.2.2     rlang_0.2.0     
 [9] lazyeval_0.2.1   tools_3.5.0      munsell_0.4.3    yaml_2.1.19      compiler_3.5.0   inline_0.3.14    colorspace_1.3-2 knitr_1.20      
[17] gridExtra_2.3    tibble_1.4.2    


data {
 int<lower=1> N;
 int<lower=1> Nt;
 vector[N] y;
 int<lower=1> period1[N];
 int<lower=1> period0[N];
 real<lower=0> adf;

parameters {
  real<lower=0> sigma_y;
  real epsilon[Nt];
  real<lower=0> rho;
  real<lower=1> nu; // degrees of freedom for t
  real<lower=0> sigma_epsilon;

transformed parameters {
  real beta[Nt];
  real mu[N];

  beta[1] = 0;
  for (t in 2:Nt){
        beta[t] = rho * beta[t-1] + epsilon[t];


  for (i in 1:N){
       mu[i] = (beta[period1[i]] - beta[period0[i]]);

model {
  // Priors
  sigma_y ~ student_t(2,0,10);
  nu ~ exponential(adf);
  epsilon ~ normal(0, (sigma_epsilon^2 / (1 - rho)) );
  sigma_epsilon ~ normal(0,1);

y ~ student_t(nu, mu, sigma_y);


generated quantities {
  vector[N] yppc; // posterior predictions

  for (i in 1:N){
    yppc[i] = student_t_rng(nu, mu[i], sigma_y);

Take a closer look at the section “Avoiding recompilation of Stan models.” The argument model_code in that example isn’t the name of a file, but rather a string containing Stan code.

What you want to do is either this,

import pystan
sm = pystan.StanModel(file='model.stan')

or even more simply, this:

import pystan
sm = pystan.StanModel('model.stan')