[Python] Installing cmdstanpy fast on Google Colab

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    After finding out that I would need to reinstall certain Python packages in Google Colab every time I refresh a runtime, I quickly lost interest in trying to use Google Colab to run stan code. In particular, the last step in installing cmdstanpy

    !pip install cmdstanpy
    import cmdstanpy
    cmdstanpy.install_cmdstan()


    takes about 10 or so minutes!

    However, I have noticed this page provided a clever solution that would only require me to install cmdstanpy once. This solution saves all of the stan and c++ files, compresses them into a .tar.gz, and then reads that back in at the beginning of each session.

    Unfortunately, some of the source code appears to be outdated. The code in that notebook throws an error after complaining a binary executable isn't getting the right command line parameters.

    My question: how can I regenerate this .tar.gz every so often? How can I save all the files created by calling cmdstanpy.install_cmdstan() to be read back in later?

    This is my current attempt, but I don't think it's grabbing everything. When I re-upload everything, and decompress it, it complains it can't find everything.

    !pip install cmdstanpy
    import cmdstanpy
    cmdstanpy.install_cmdstan() # this takes a while

    # write everything out to disk
    import os
    import shutil
    cmdstan_dir = cmdstanpy.__path__
    tar_filename = '/content/cmdstan_files.tar.gz'
    shutil.make_archive(tar_filename.replace('.tar.gz', ''), 'gztar', cmdstan_dir)

    Continue reading...

Compartilhe esta Página