flopy The program mf6 does not exist or is not executable - flopy

when I execute sim.run_simulation() error is showing "The program mf6 does not exist or is not executable".

You need to set the model executable to a valid executable path. If your model is a MODFLOW 6 model, then you should set your executable path to the MODFLOW 6 executable.
Example:
import flopy
mf = flopy.modflow.Modflow.load('model.nam', exe_nam='my-modflow6-executable')
Replace my-modflow6-executable with the full file path (including the file name and extension) to the appropriate executable.
You can see the attribute here.

Related

relative imports on CDSW

I have a project on CDSW organized as follow :
/home/cdsw/my_project_v2.1
|_>input
|_>output
|_>scr
|_>__init__.py
|_>main.py
|_>utils
|_>__init__.py
|_>helpers.py
in my current code, I use sys.path.append to perform my imports.
import sys
sys.path.append("/home/cdsw/my_project_v2.1/src/utils/")
from helpers import bar
This works fine but it is a bad practice because if the version change, then I need to change all my scripts that use the path.
I wanted to replace it with some relative path :
from .utils.helpers import bar
But I got the error :
$ pwd
/home/cdsw
$ python3 my_project_v2.1/src/main.py
Traceback (most recent call last):
File "my_project_v2.1/src/main.py", line 1, in <module>
from .utils.helpers import bar
ModuleNotFoundError: No module named '__main__.helpers'; '__main__' is not a package
what do I need to change in my architecture or in my code to make it work ?
Just use
from utils.helpers import bar
A short excerpt from the documentation of the Python command line arguments:
If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the __main__ module.
The first half of the sentence means that you can use absolute module names when referring to the contents of your directory, because Python will search for module there. The fact that you can not use relative imports is a consequence of the second half of the sentence.
As a side note, you may also consider omitting the version number from the name of the directory, or better yet, put your code directly in /home/cdsw. The latter may sound strange as you would never do that on a regular machine, but here everything is in a container and actually this is the way your code is supposed to be organized in CDSW. You can confirm this by creating a new project based on a template or a git URL – both will put code directly in the home directory.

Where can i find the pyc file?

The python3 version is Python 3.5.3 in my os.
mkdir workspace
cd workspace
vim print.py
print("i am learning")
Saved and exit.
python3 print.py
i am learning
As far as i knew, python source file was parsed and compiled into pyc file when to execute it.
ls
print.py
There is no pyc file in workspace directory,where is the complied print.py file then?
sudo find / -name ".pyc"
The find command still can't search pyc file such as print.pyc .
python3 -m compileall can create the compiled file for print.py manually,where is the compiled file for print.py created by python itself?
Does python3 delete the print.pyc after executing python3 print.py?
Ok this is one big of a problem I ever had when I'm started to learn python few years back. Python is just like any other oop programming languages which does compilation before program execution. When python compiles its program, it creates the bite code which is you can see by standard library called dis.
import dis
print(dis.dis(your_program))
Sometimes (not always) python creates .pyc file for the running programs to improve the speed up the loading of import modules but not to improve the execution time. So hope you get intuition behind .pyc, furthermore .pyc only creates when your module is import by another module.
As an example, Imagine you have this print.py (Let's modify it shall we)
def return_print_statment(statement):
print('Printed version: ', statement)
Suppose this module imported by another custom module called views.py. In views.py there is a module_view which will use the return_print_statment
from print import return_print_statment
def module_view():
...
return_print_statment(output)
So in the compilation, since you have imported the print.py python will generate print.pyc file for it. In python 2.0 python will put the .pyc to right next to your program in the same folder, but in python3 instead of creating in the same folder python will create separate folder called __pycache__ in the same directory to put these byte codes.
python3 -m compileall .
To compile all .py files in your current directory.
http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm

os.path.abspath is not producing the absolute path for the directory python

its a simple program which outputs the full file path using a given path that does not include the root. But It just prints out the given path. why?
operating system Ubutu18.04 Lts, IDE Pycharm , python 3.7
I've tried installing pathlib and path.py but it still does not work.
from os.path import abspath, relpath
x = '/python/100 exercises/24.py'
print(abspath(x))
The expected output is
/home/tasif/Documents/python/100 exercises/24.py
actual result is
/python/100 exercises/24.py
By putting a / at the beginning of your path, your system is interpreting x as a root system path already. Instead, write your path as x = 'python/100 exercises/24.py' and try again.
I think this is true but I could be corrected: You need to run it as os.path.abspath(x) otherwise the script does not know where to get that function from and is doing nothing but printing your string with a non-function ran on it.
You should also rename your directory to 100_exercises/ or something more posix friendly.

return error of 126 when compiling with idle 3.5

subprocess.call('/Users/siddarthkrishnan/Desktop/Lion.jpg', shell = True)
126 giving me this in python 3.5 return of 126.
Why is the file not opening? It is giving return error 126.
Based on the path you pasted I assume you are on Windows. subprocess.call tries to invoke your jpg file as an executable (like *.exe, *.bat etc.) and fails because it isn't an executable. This is not a bug, this is the correct behavior.
If you want to launch the Windows Photo Gallery (or whatever program is set for the *.jpg file extension in your file associations), you can use os.startfile like this:
import os
os.startfile('/Users/siddarthkrishnan/Desktop/Lion.jpg')
Note: this solution works only on Windows.

importing a python function from a file in other directory which depends on another file

This is my directory structure
-directory1
|-currentlyWritingCode
-directory2
|-__init__.py
|-helper.py
|-view.ui
The helper.py is a UI from pyqt4 framework. It needs the view.ui file .
it has following code to load the ui data
viewBase, viewForm = uic.loadUiType("view.ui")
now in directory1 in the currently writing code I did this
import directory2.helper
when I run the code in currentlyWritingCode it is throwing an error that
FileNotFoundError: [Errno 2] No such file or directory: 'view.ui'
What to do now?
using python3.5 from anaconda on Centos 7
Use os.path.join(os.path.dirname(os.path.realpath(__file__)),'view.ui') in place of view.ui. This will ensure you correctly reference the folder that the python file lives in, regardless of the code that imports it.
Note: Make sure you have import os with your other imports.
How does this work?
__file__ is an attribute of the module you are importing. It contains the path to the module file. See this answer. However, this path is not necessarily an absolute path. os.path.realpath returns the absolute path (it even follows symlinks if there are any). At this point we have a full path to the module, so we take the path to the directory (os.path.dirname) and join it with the original filename (which we assumed to be relative to the original module and so should be in the aforementioned directory). os.path.join ensures that the correct \ or / is used when constructing a file path so that the code works on any platform.

Resources