As part of a topic that came up over in the PyStan issues page, it seems it would be helpful to share some instructions on getting a Python environment set up for doing development and tinkering with PyStan.
PyStan uses Poetry for dependency management and packaging. It also makes for a convenient way to set up a development environment. While I’m sure there are other ways to get up and running for PyStan development, the below steps show how to go from installing Poetry to getting a proper dev environment set up.
Install Poetry – the introduction documentation gives the recommended installation for your particular system.
Clone the PyStan repository from GitHub:
git clone https://github.com/stan-dev/pystan.git
Navigate into the freshly cloned
pystandirectory and install it via Poetry:
cd pystan poetry install
At install, Poetry creates an isolated virtual environment where all the dependencies are installed, and PyStan is installed in develop mode. This allows local changes to PyStan to be immediately reflected in this environment without breaking any other projects that use the library.
The Poetry-created virtual environment is the backbone of your dev environment. It can be used directly via Poetry’s command line interface. For example,
poetry run my_script.py
Will run the file
my_script.pyusing the PyStan environment.
The environment can be directly activated as well if you want more flexibility:
For those who prefer to use IDEs like PyCharm or VS Code, you can locate the path of the Poetry virtual environment by running:
poetry env info
Pointing your IDE to use the Python interpreter at that path will allow you to use all the tools/features of the IDE as normal.
Lastly, Poetry gives an easy interface to run dev tools like
black, calling these commands with Poetry should do the trick, for example for running tests:
poetry run pytest
There are plenty of other commands/more complex behavior in Poetry that one could take advantage of, check out the documentation if you’re interested. Hopefully these steps are useful for others who want to tinker with PyStan. If anyone else has a different approach or wants to share some tips of their own, feel free to share!