seacas
seacas copied to clipboard
exodus.py module and python 2/3 compatibility
Turning an email thread into an issue
Dr. Sjaardema,
Sorry it took so long to get around to testing it, but I’ve built and tested the new python-exodus with both Python 2 and 3 (versions 1.13 and 1.16 respectively). It appears to work very well.
Is there any reason you can’t put the standard “from future import …” at the top of version 1.16 and use a single file for both Python 2 and 3? At a glance I don’t see any incompatible syntax. Things like left-side unpacking or type-cast-hints in function signatures aren’t fixed by “future”, but everything I see in 1.16 should work in either Python 2 or 3 after adding the “future”.
If you use the “inspect” module to get the path of the script, the user doesn’t have to set the “ACCESS” environment variable or have the path hard-coded into the script. The following will give the absolution path to the directory containing the script:
parentDirectoryOfScript = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
With this change, users can build exodus on a computer with internet (much easier to build the TPL’s) and then simply copy-paste it to another machine (assuming it’s a similar-enough OS). I think most people that need to run on standalones or HPCs (like me) also have a computer with internet for doing things like this.
I found a “setup.py”, but it didn’t work for me. I think all you need to do is put the “exodus.py” script into a directory named “exodus” and also make an empty file called “init.py” in that folder so Python knows it’s a module. If done, this also allows for packaging the module for distribution on PyPI. If you do this, no one will ever need to build it themselves. They can simply “pip install exodus”. See this link for details: https://packaging.python.org/tutorials/packaging-projects/
Thank you,
RE: “use of future” – I wanted to minimize any potential changes to users existing workflows and that meant minimal touching of the existing python 2 version of exodus.py. As more people switch to using python 3 version, at that point I will probably drop the python 2 version and use the “from future import” to provide something that the remaining python 2 users can use.
Does the “inspect” module assume that the exodus.py and libexodus.so are in the same location? I will look into using that; thanks for the suggestion.
I will also look into the init.py suggestions. Again, I need to minimize any perturbation of existing workflows, so moving exodus.py to a different location might cause issues although I can probably symbolically link if from the new location back to current location. I will look at the link you provided.
..Greg
Dr. Sjaardema,
Your users greatly appreciate your attention to backward compatibility. I myself have multiple workflows, some of which require Python2 and others that are 2/3 safe. I don’t think a unified python 2/3 safe file would disrupt existing python 2 workflows, but moving the file into a module certainly would. As you mentioned, symlinks could solve the problem, assuming Exodus is only used on OS’s that support symlinks.
“inspect” is going to give you the path of the python file in which it is executed, so for you this is the path of the exodus#.py file. As the developer you control where that python file is relative to the compiled C, so you can simply build the relative path using “os.path.join”. For example, if you had a directory structure (folders in parenthesis, symlinks with arrows) like the following I think you’ll get the best of both worlds:
exodus.py --> ./exodus/exodus2.py (exodus) -init.py -exodus2.py -exodus3.py -(lib) -libexodus.so
In exodus2.py and exodus3.py, to get the path to libexodus.so:
parentDirectoryOfScript = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
pathToLibExodusDotSo = os.path.join(parentDirectoryOfScript, “lib”, “libexodus.so”)
In this example you can see that I included a “init.py” file. If this file contains the following it would be a Python 2/3 safe module, but users could still continue to use the scripts by path and not as a module if they wanted.
__all__ = [‘exodus’]
import sys
pythonMajorVersion = sys.version_info[0]
if pythonMajorVersion==2:
from exodus2 import exodus
elif pythonMajorVersion==3:
from exodus3 import exodus
else:
raise Exception(‘only python 2 and 3 supported’)
Thank you, B.. O..
Stale issue message