-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Starting HELICS-Example with helics run does not work, seperate calling does #104
Comments
This issue came up during a tutorial I gave earlier this week and the fact that the co-simulation will run when the federates are launched individually but fail when using the runner makes me think something might be up with the runner re-work the two of you have been doing (but not completed?). Note that I haven't had this problem on macOS (ARM-based). This bug was manifesting when starting from a clean virtual environment. |
@carwegka, the error you're seeing at the end of the co-simulation is somewhat expected (depending on variation in the OS scheduler). It happens on the very last time step when one of the federates ends early enough that it exits the co-simulation before the other federate has published its final value. When this happens, HELICS throws this warning to indicate that there is nobody to receive the sent publication (which is generally not good). Since it is the last time step, there will be no new calculations even if the federate was there to receive the publication. I usually add a function that I call on the last step to advance the federates to infinite time (well, the maximum time HELICS can track) and doing so outside the main simulation loop which means all the federates will be granted that time and receive an pending publications prior to exiting the co-simulation. I've made this change and pushed it up into the "complete" version of the example. |
@nightlark and @josephmckinsey, the example I was using can be found at HELICS-Examples/unmaintained/python/osmses_2024_battery_charger using the "osmses_2024_runner.json" which runs the "complete" version of the example. |
The pull requests for the re-work of the web UI/CLI haven’t been merged to main yet (or pushed to PyPI). Unless the pyhelics you used is from one of those branches, this is a pre-existing bug and wasn’t introduced by those changes. |
Could the runner be launching the scripts outside of the virtual environment? |
They were just pulling from PyPI so whatever the latest released version that's out there publicly. |
@nightlark We (a colleague also had the same problem) tried it with global python interpreter (outside of virtual environment) and it also didn't work |
Yea... the runner code hasn't changed in over 2 years, so it's likely a very long standing bug (@kdheepak is the most familiar with this area) or maybe a change in recent Python versions on some operating systems. For a minimal reproduction, the runner appears to just be spawning subprocesses: https://github.com/GMLC-TDC/pyhelics/blob/main/helics/cli.py#L260-L266 You could try writing a Python script that just hardcodes those subprocess calls, replacing |
I've also been unable to reproduce the error using fresh virtual environments on macOS (M2, Python 3.9 and 3.12), and RHEL 8 Linux (x64, Python 3.9) -- this may be a Windows-only issue, but I don't have a Windows system to test on. |
I think the problem here is that the runner spawns subprocesses that don't have the same environment has the parent process, and Add the following for good measure at the top of each python file and share the output here again: import sys
import os
print("Environment:", os.environ)
print("Python Version:", sys.version)
print("Python Interpreter Location:", sys.executable) |
For the battery file: Environment: environ({'ALLUSERSPROFILE': 'C:\ProgramData', 'APPDATA': 'C:\Users\wegkamp\AppData\Roaming', 'CHROME_CRASHPAD_PIPE_NAME': Python Version: 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] Python Interpreter Location: C:\Users\wegkamp\Dokumente_lokal\helics_example.venv\Scripts\python.exe For the charger file: Environment: environ({'ALLUSERSPROFILE': 'C:\ProgramData', 'APPDATA': 'C:\Users\wegkamp\AppData\Roaming', 'CHROME_CRASHPAD_PIPE_NAME': '\\.\pi Python Version: 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] Python Interpreter Location: C:\Users\wegkamp\Dokumente_lokal\helics_example.venv\Scripts\python.exe PS: I deleted some of the domain part of the environment, which I think is not important, but I don't want to have published. I hope this is not that important. |
It seems to be using the correct interpreter, i.e. Can you open a new command prompt, and without activating your |
So if I do that and afterwards run This then also gives different interpreters for the two files: Nonetheless, the error occurs |
Without |
I can, yes. And it doesn't throw back any error/warning. |
So just to clarify, when you run
but when you run
Is that correct? |
If I am in the venv, I get |
Do you have both |
This is the reason you are getting the import error. How have you installed Python? How did you create the .venv virtual environment to begin with? Can you uninstall this version of Python? |
|
@carwegka, is this issue closed out as far as you're concerned or do we need to keep digging into it? |
Hi @trevorhardy and @nightlark. I recently had another academic user report a similar issue where the helics run command is not properly using the python virtual environment python executable on windows. I can also confirm this is still an issue. A work around seems to be to explicity specify the python executable for your virtual env in the "exec" string in your runner configuration file. for example: { When doing this I can confirm it's using the correct python environment. However, I am running into another issue. If I attempt to run this little test python federate and a helics_broker explicitly it runs successfully. if I try to run using helics run --path= |
@afisher1 are you escaping the backslashes correctly? You might need up to quadruple backslashes in some cases based on how many times the string is getting decoded/processed by things that handle backslash escaped characters... Usually using Unix style path separators even on Windows is far easier. |
@nightlark yes I did. sorry that was odd. Must've been an odd copy and paste issue there. I've tried with \ and \\ and \\ in the exec string. |
Ok GitHub keeps executing the escape lol. I've tried with 2, 3, and 4 backslashes all with the same result. |
Did you also do the "directory" string? |
To rule out crazy escaping issues, I'd highly recommend using |
You can also add a call to |
Ok I must've missed an escaping backslash somewhere. My subsequent error was due to unescaped backslash. And yes it also works correctly with a /. So no additional issues besides helics runner not using the virtual env it was executed under when starting the federate processes in windows. So current workaround solution on windows with the helics runner is to use the full path to the virtual env python executable in the "exec" key. |
Sorry for the delayed answer, I completely got lost of this thread! So to me it seems that there is a different python version in the venv compared to the global python version. And with the two different calls of the helics example (1 via helics run and 2 via separate calling) they might use the different version (maybe 2 does not use the venv python?). So one of the two does fail because the python version is "wrong" and thus cannot find helics in the global environment? But why does the second version (separate calling in different terminals) not fail no matter if I activate the environment before or not? And to answer at least some of your questions, the environment was created using |
@nightlark. After looking into this more this appears to be a known issue with python's subprocess.run and windows OS. The "official" fix is to use the result of sys.executable rather than 'python' in the exec command passed into subprocess. Please note that you should not use sys.exectuable in the python runner configuration file. It needs to be used in place of the 'python' in the exec string internally in the subprocess.run() function call. see here. https://stackoverflow.com/questions/65283987/venv-not-sticking-across-subprocess-run-on-python-windows. @carwegka this is the underlying issue causing it to use the system installed python executable rather than the one created with your virtual environment regardless of whether you called helics run under the virtual environment or not. So giving the full file path to your desired virtual env python executable in the exec key should fix your issues. Even with the proper fix implemented on our end you would still need to make sure you executed helics run in the activated virtual env. |
Right now the HELICS CLI just passes the exec string verbatim to subprocess.run as-is with no further processing. I think adding special treatment for occurrences of I think this gets at an underlying issue of managing what environment a command should run in, with the ability to support multiple Python federates that are installed in their own venvs to avoid dependency conflicts with each other. I guess we sort of already support that right now in the form of specifying the full path to the python interpreter -- though that's not really portable if you're sharing a config file with others. I'm not sure we have a way to make a fully portable config file, since the location/creation of a venv would be dependent on a user creating it in the right place for the example/demo they are trying to run... |
Thanks for the help and explanations, this clarifies the general problem to me (although I'm probably not that much into the topic). Real thanks! |
A change that should help address this issue will be in the 3.6 release of pyhelics. |
When trying to run the osmses example via the helics run command, the execution cannot find the helics package:

This is although helics is clearly installed with pip:

But when running the example with the 3 separate starts as in



$ python battery_cosim_complete.py & (or launch in its own shell)
$ python charger_cosim_complete.py & (or launch in its own shell)
$ helics_broker -f 2 (or launch in its own shell)
it seems to work:
Nonetheless, the broker prints out a warning at the end:
The "Debugging Command" prints out the following:

If you need additional information, let me know!
The text was updated successfully, but these errors were encountered: