-
Notifications
You must be signed in to change notification settings - Fork 317
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
bug: location of mf6 executable using relative path #1633
Comments
Suggested solution: In |
@mbakker7, I like your solution, though it should be implemented in mbase.py run_model, and only if the attempt to find the executable using shutil.which fails. |
@mbakker7, I added code that tries converting the executable path to an absolute path (os.path.abspath) when all else fails. Also, updated the comments for the documentation. The changes are checked in to the develop branch. Please let me know if this fixes the problem. |
Sorry, @spaulins-usgs, still doesn't work. My relative path is Is there a way to ask |
@mbakker7, to output the executable path being used try setting the silent flag to False:
I am not 100% sure what is wrong and I do not have a Mac to test this, though I do have some ideas. If you are willing to test these, there are some potential fixes on my fork of flopy here: The only modified file in this fork is flopy/mbase.py Please let me know if these updates fix the issue. |
@spaulins-usgs, sorry for the late reply. I was on break for a while, but I am back and determined to help fix this issue. The message I get when running modflow is: For your information, the full trace back is:
|
I think I found the problem. In
When a relative path is passed to the
returns
Hence, |
I just reproduced this same error on windows. When I supply a relative path without the extension ( |
I can confirm the same error on Windows: If you specify a relative path you must also specify '.exe'. This is odd, as flopy just removed the requirement to add An executable program on a Mac doesn't have an extension but a special mode flag that the OS recognizes. May I (re)suggest to always use |
@mbakker7, I think it would be fine to use os.path.abspath as long as the exe_name supplied is more than just an executable name (i.e. it is either an absolute path or contains at least one folder name in it). However, if just an executable name is supplied, FloPy should resolve the full path to the executable by looking for the executable in the Windows/Mac defined paths (Windows/Mac "PATH" variable). The which command will not do this with an absolute path. For example, on Windows which(os.path.abspath("MF6.exe")) will not search the "PATH" variable paths for "MF6.exe", but which("MF6.exe") will. Therefore, I think the behavior should be:
|
@mbakker7, I implemented the behavior described in my post above here: https://github.com/scottrp/flopy/blob/develop/flopy/mbase.py Let me know if that works for you. |
Still won't work, because I understand you want to use
and replace it with the simple
Not sure what to do with the exe extension. You can remove that on Windows if you want (I cannot test that). Will this work for you? It works for me! |
I too can confirm that this does not work on Windows: sim = flopy.mf6.MFSimulation.load("mfsim.nam", exe_name=r"..\..\dev\wrdapp\bin\mf6")
sim.run_simulation()
# ....
# Exception: The program ..\..\dev\wrdapp\bin\mf6 does not exist or is not executable. but adding I removed from subprocess import Popen, PIPE
r = Popen(r"..\..\dev\wrdapp\bin\mf6", stdout=PIPE, stderr=PIPE)
stdout, stderr = r.communicate()
assert "Normal termination of simulation" in stdout.decode() Windows doesn't need file extensions for executables, and should accept absolute or relative paths. |
I think I am getting to the root of the problem.
runs mf6 fine on a mac. But if I specify a
So I tried to specifiy |
@mbakker7, sorry it looks like I misread how the code is suppose to work. I did not originally write this section code, so I am trying to figure out its behavior just like you. I thought when the code was doing this:
That it was then using the resulting "exe" variable when calling Popen, but it is actually using exe_name in the Popen call. My change therefore did not actually do anything. I still think this is the correct general approach, I was just setting the wrong variable. I fixed this and tested it with various exe_name values on Windows (just the exe name with extension, just the exe name without extension, relative path with extension, relative path without extension, full path with extension, full path without extension), and it works for all of them. The updated code is here: https://github.com/scottrp/flopy_exe_path/blob/develop/flopy/mbase.py Regarding replacing the code with:
This fails when a partial path to the executable without the .exe extension is supplied ("bin\mf6"). In this case shutil.which returns None and abspath raises an exception because of this. Anyway, I think much of the confusion here (at least the confusion on my part) is that the which call is not actually doing anything other than testing for the existence of the executable. The fix I am now proposing both fixes this existence test and converts any relative paths to absolute for Popen. @mwtoews, the problem is that "which" will not detect a partial path to the executable unless the extension is included. The updated code fixes this problem. |
The updated code works on my Mac. For both an absolute path but now also for a relative path! |
* fix(exe path): FloPy now correctly resolves relative paths to mf6 executable (#1633) * feat(solvers): support for multiple solver types #1706 * Revert "fix(exe path): FloPy now correctly resolves relative paths to mf6 executable (#1633)" This reverts commit 14335cf. * Update mfsimulation.py feat(solver support): rolling back ims deprecation warning --------- Co-authored-by: scottrp <[email protected]>
…cutable (#1633) (#1727) * feat(exe path): support for relative paths --------- Co-authored-by: scottrp <[email protected]>
- support pathlike or str paths in public APIs - add tests for supported path arguments - update docstrings and add type hints - update some notebooks to use pathlib - update most autotests to use pathlib - add verbose flag to more util functions - add sim_path property to MFSimulation - expand run_model() tests to cover #1633 - update resolve_exe(), add tests
tentatively closing as this should now be fixed and covered by tests |
Just wanted to confirm that the current develop version works fine with a relative path on my Mac. Thanks again for fixing this!
|
It appears that in the current version of flopy, the executable still has to be specified as |
@mbakker7 I'm unable to reproduce this on Windows with 3.4.2 or the develop branch. Provided an mf6 executable is on the system path, passing
That is done here, the motivation being to accommodate flopy scripts which were developed on windows and then moved to a different platform.
Yes, the extension should never be required. Flopy does not currently add .exe on windows internally either — my understanding is it is not needed unless Would it be possible to share the script you are seeing the issue with, as well as the Windows and Python versions, the value of |
Thanks for looking into this (again), @wpbonelli
@vincentpost can you confirm this and give the flopy version that you are using? |
Hi @mbakker7 thanks for the extra info! I understand the issue now. The .exe extension is indeed necessary if the binary is located by absolute or relative path on Windows. This is consistent with the behavior of I don't see any downsides to supporting extensionless paths — this could easily be added for the next minor version release, unless there are considerations I'm missing. |
Ok. So it works as designed. If I understand it correctly:
Suggested solution: Isn't it the case that |
Yes, currently the
I've opened #1957 to support this. |
I am stumbling with flopy to locate the mf6 executable on my Mac.
I define the location with a relative path, as per documentation (
Relative path to MODFLOW 6 executable from the simulation working folder
).exe_name='../bin/mf6'
But when I run the model I get:
FileNotFoundError: [Errno 2] No such file or directory: '../bin/mf6'
When in the next code cell I do:
ls ../bin/
I get
mf6* mp7*
So mf6 clearly exist in the right location and is executable. I can even run it
!../bin/mf6
Gives
ERROR REPORT: 1. mf6: mfsim.nam is not present in working directory.
When I move this mf6 to the same directory as the notebook and change exe_name to
path = os.getcwd()
exe_name=path + '/mf6'
But then it is an absolute path.
My colleague claims that
exe_name='../bin/mf6.exe'
works on his Windows machine.
Is this a Mac problem?
The text was updated successfully, but these errors were encountered: