1

I have a python script that uses a package called flopy. My script generates a series of inputs to a fortran executable. Flopy writes these into text files and then calls the fortran executable, which uses the text files to run a model.

I'm using a mac (OSX) and I downloaded python 2.7 from python.org- i.e. I'm not using the Apple system version of python. The version of python I'm using is in Library/Frameworks/Python.Frameworks/

I can run my script if I call it from the Terminal window (by typing:

Python myscriptname.py

However if I run my script through IDLE (the version that came with python which I downloaded it) it returns an error:

Traceback (most recent call last):
  File "/Users/neilthomas/RotatedModel_v4_Tr_mfnwt.py", line 355, in <module>
    success, mfoutput = mf.run_model(silent=False, pause=False)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flopy/mbase.py", line 638, in run_model
normal_msg=normal_msg)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flopy/mbase.py", line 1034, in run_model
stdout=sp.PIPE, stderr=sp.STDOUT, cwd=model_ws)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory 

The file 'mfnwt' absolutely does exist. I'm sure I'm missing something obvious, but is there something I need to do to allow IDLE to run programs/subprocesses via the shell it uses? Thanks.

Mike T
  • 41,085
  • 18
  • 152
  • 203
Neil Thomas
  • 31
  • 1
  • 2
  • "The file 'mfnwt' absolutely does exist"...Are you sure? – xrisk Jan 15 '17 at 04:07
  • How did you run idle? And do you use a relative file name? Your current working directory may not be what you think it is and that will affect relative path names. Print `os.getcwd()` to see what it is. – tdelaney Jan 15 '17 at 05:21
  • @Rishav: Yes, the file mfnwt does exist- as noted above, I can run the same script through Terminal. – Neil Thomas Jan 15 '17 at 05:35
  • @tdelaney: I run IDLE by right clicking on the script and opening the python script with IDLE. The script is in the same file directory as the file 'mfnwt'. – Neil Thomas Jan 15 '17 at 05:37
  • I don't know about OSX specifically, but right-clicking doesn't usually set the current directory. Try right-clicking a simple script like `import os;print(os.getcwd());input("end")` to see what it is. – tdelaney Jan 15 '17 at 06:05
  • When you right click, do you `edit with IDLE` and then run either with IDLE's `Run` menu or the`F5` shortcut, or do actually `run` from the context menu? In either case, the traceback says you are running "/Users/neilthomas/RotatedModel_v4_Tr_mfnwt.py". Is this correct? Which patch release of 2.7 did you download? The current one, about a month ago, is 2.7.13 and I would recommend that. – Terry Jan Reedy Jan 15 '17 at 06:52
  • The line numbers in my copy of the 2.7.13 version of subprocess.py are about 300 less than in the traceback. That is why I think you are running an older version. What I understand from the traceback is that IDLE ran your program, which called flopy.mbase.run_model, which called subprocess.??? to run 'something' in a subprocess. There was an error in either the child part of subprocess.POpen._execute_child or in the 'something' itself when executed in the child process. This is reported as an OSError, which you believe is connected with a file called 'mfnwt'. (see next comment) – Terry Jan Reedy Jan 15 '17 at 07:49
  • From my reading of _execute_child, the `child_exception` raised should have a `child_traceback` attribute, which was not printed. I would want to see it. To do so, I would try the following in the original program, which where it calls run_model: `try: \nexcept OSError as e: print(e.child_traceback)`. – Terry Jan Reedy Jan 15 '17 at 07:56

1 Answers1

1

The problem here is that you have to identify the specific MODFLOW executable file you are calling ('mfnwt' in your case). I do the same with a MODFLOW 2000 file:

mf = flopy.modflow.mf.Modflow(modelname,namefile_ext='nam',version='mf2k',exe_name='/home/MODFLOW-and-related-codes/build-08/bin-windows/mf2k.exe')

In your case, you would do something similar, only replacing the version='mf2k' and exe_name=path to match where you are storing your MODFLOW file.

See the documentation for further details: https://modflowpy.github.io/flopydoc/mf.html

Jacob
  • 58
  • 9