Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
4

How to use custom python packages in OpenStudio Measures

asked 2025-06-29 23:35:00 -0500

TomB's avatar

updated 2025-06-30 10:22:24 -0500

The python measures within OpenStudio Application use the python 'standard library' that comes with the sofware. Located for example at C:\openstudioapplication-1.9.0\EnergyPlus\python_lib.

I would like to use non standard python packages, including ones I create to share common utility code between measures.

At the moment I have a very clunky approach - I basically copy my custom package into that location. It is horrible to develop and distribute this way.

Can I get OpenStudio application to look in other locations for python packages?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2025-06-30 08:31:57 -0500

You have a few options:

  • python -m pip install --target=C:/openstudioapplication-1.9.0/EnergyPlus/python_lib -r requirements.txt

    • This is actually how we package extra python modules such as pandas, numpy, pytest etc in the OS SDK CLI
  • Start your script with import sys; sys.path.insert(0, "/path/to/custom/folder"). That custom folder could be in the resources/subdirectory of your measure.

    • Caveat: This would only work if you don't have or depend on another module that has a native extension (a compiled C/C++ extension) that is not packaged by the OS SDK yet.
  • Running in the OS SDK CLI directly, you have more flags

Options:

 Python Options:
  --python_path DIR                Add additional directory to add to front of PYTHONPATH environment variable (may be used more than once)
  --python_home DIR                Set PYTHONHOME environment variable
edit flag offensive delete link more

Comments

Thank You ! the second option works a treat. I don't understand the caveat yet, but hopefully won't hit that blocker. This works for developing measures

CURRENT_DIR_PATH = Path(__file__).parent.absolute()
sys.path.insert(0, str(CURRENT_DIR_PATH.parent))

but not for running measures in the Application. This is not elegant, but works for now in the Application

MY_MEASURES_DIR=r"C:\Users\tombe\git\openstudio-python-measures"
sys.path.insert(0, MY_MEASURES_DIR)
TomB's avatar TomB  ( 2025-07-03 00:10:16 -0500 )edit

You should put your helper files in the resources/ subdirectory to avoid this issue, for eg resources/helpers.py

in measure.py

measure_dir = Path(__file__).resolve().parent
if str(measure_dir) not in sys.path:
    sys.path.append(str(measure_dir))
from resources.helpers import my_helper
Julien Marrec's avatar Julien Marrec  ( 2025-07-03 03:16:41 -0500 )edit

If you need one helper file in several measure directories, put that wherever, write a (python) script that would copy it to every measure that uses it (or just use a symlink)

Julien Marrec's avatar Julien Marrec  ( 2025-07-03 03:18:39 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Question Tools

1 follower

Stats

Asked: 2025-06-29 23:35:00 -0500

Seen: 60 times

Last updated: Jun 30