What you need
Section titled “What you need”- Visual Studio Code 1.110 or later. Check your version with Help > About (Code > About Visual Studio Code on macOS). You download it from the official VSCode website.
- A Python environment, only to run models. You can explore, edit, draw and validate a model, and chart existing results, without Python.
Install the extension
Section titled “Install the extension”-
Open VS Code and open the Extensions view, with the icon in the activity bar or with Ctrl Shift X (Cmd Shift X on macOS).
-
Search for Pywr and select the extension published by s-simoncelli.
-
Press Install.
The Python extension is installed with it, because it is used to select the Python environment.
You can also install it from its page on the Visual Studio Marketplace.
The extension starts when you open a JSON file. When the file is a Pywr model, the Pywr icon in the activity bar opens the sidebar with the components of the model.
Set up Python to run models
Section titled “Set up Python to run models”The extension runs your models with Python, so you need Python installed on your computer. Pywr v2 needs Python 3.10 or later. You can download it from python.org.
Pywr does not use the Python of your computer directly. It uses a Python
environment: a folder, usually called .venv, with its own copy of Python and of the
packages Pywr needs. Installing packages in it never changes the rest of your computer,
and each project can have its own. A conda environment works too.
Automatic setup
Section titled “Automatic setup”The extension can create the environment and install the packages for you.
-
Open the folder with your model in VS Code (File > Open Folder…).
-
Open the Command Palette with Ctrl Shift P (Cmd Shift P on macOS) and run Pywr: Set up the Python environment.
-
Pick Pywr v2 or Pywr v1, the version your model is written for.
-
Confirm the message that follows. If VS Code already uses an environment for this folder, the command installs the packages in it. Otherwise it creates a
.venvfolder in the folder you opened and tells VS Code to use it. -
Wait for the install to finish. It can take a few minutes the first time.
When everything is in place, a message says the environment is ready. If a package could not be installed, an error names it. Press Show Output to read what went wrong.
If your model has Python parameters that import other packages, install those packages as in the last step of the manual setup.
Manual setup
Section titled “Manual setup”If you prefer to manage the environment yourself, create it and install the packages from a terminal.
-
Open the folder with your model in VS Code (File > Open Folder…).
-
Open the Command Palette, run Python: Create Environment, choose Venv and pick a Python version. VS Code creates the environment in a
.venvfolder and selects it for the workspace.You can also create it from a terminal with
python -m venv .venv. -
Check that the environment is selected: the status bar shows
.venvnext to the Python version. If it does not, run Python: Select Interpreter from the Command Palette and pick.venv. -
Open a new terminal (Terminal > New Terminal). VS Code activates the selected environment in it, so the prompt starts with
(.venv). A terminal opened before you selected the environment is not activated: close it and open a new one.If the prompt does not show
(.venv), activate it yourself:Terminal window .venv\Scripts\activateTerminal window source .venv/bin/activate -
Install the packages for your version of Pywr in the activated terminal:
Terminal window pip install pyarrow pandas polars tablesPywr v2 is built into the extension, so you do not install it. pyarrow, pandas and polars read the Pandas, Polars and Python timeseries. PyTables (
tables) is used by the risk analysis and the QA report.Terminal window pip install "pywr<2" pandas tablesPywr v1 models are run by pywr itself, from your environment. PyTables (
tables) is used by the risk analysis and the QA report.Install any other package your Python parameters import in the same way.
If a run fails with a message about the Python environment, see Troubleshooting.
Follow Your first model to create, run and chart a model, or open a model you already have and read Explore the model.
Pywr for VS Code and Pywr for Azure DevOps are developed and maintained by Stefano Simoncelli.