Skip to content

Commit

Permalink
Adding comment about relative import.
Browse files Browse the repository at this point in the history
  • Loading branch information
josiahjohnston committed Jun 28, 2019
1 parent ffa02ff commit 121fb87
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions examples/3zone_toy_stochastic_PySP/rhosetter_FS_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
optimizations.
"""
# The rhosetter module should be in the same directory as this file.
from rhosetter import set_rho_values

def ph_rhosetter_callback(ph, scenario_tree, scenario):
Expand Down

2 comments on commit 121fb87

@mfripp
Copy link
Member

@mfripp mfripp commented on 121fb87 Jul 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you happen to know how rhosetter.py is found in this case? I tried moving both rhosetter files into a setters subdirectory, then using --rho-cfgfile=setters/rhosetter_FS_only.py. This worked fine, but I wouldn't actually have expected it to. If I break into the code just below this comment, I find that sys.path includes . (the main directory) but not setters. Nevertheless, when from rhosetter import set_rho_values runs, it manages to find rhosetter.py inside the setters directory. I wouldn't expect that to happen.

@josiahjohnston
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rho-cfgsetter import stack trace is roughly:
pyomo.pysp.ph:ProgressiveHedging.init() -> pyomo.pysp.util.misc:load_external_module() -> pyutilib.misc:import_file()

import_file works some magic to parse a path into a directory + module, then temporarily adds that directory to the beginning of sys.path (list of directories to search for python libraries & modules).

https://github.com/PyUtilib/pyutilib/blob/master/pyutilib/misc/import_file.py

Please sign in to comment.