sharppy / SHARPpy

Sounding/Hodograph Analysis and Research Program in Python
https://sharppy.github.io/SHARPpy/index.html
Other
216 stars 112 forks source link

Sharptab Scripting Requires PySide install #72

Closed aarande closed 7 years ago

aarande commented 9 years ago

Hello All,

I noticed today when attempting to script with Sharppy using the sharptab module it failed if PySide was not present. It appears that since the init.py files are not empty it has to call everything from the toplevel imports in sharppy.init.py.

This seems a little heavy handed to not only requre PySide but import everything in not only sharppy but also PySide if you only want to run some calculations using the sharptab module. To use sharptab by itself triggers an import of sharptab, io, viz, and databases as well as QtGui and QtCore from PySide.

I think it would be ideal to be able to only import the sharptab submodule and run calculations with that even if you don't have PySide etc since you do not need it and it is wasted imports. You also may be working on a machine (i.e. AWIPS) that does not directly have easy access to that module.

Thanks, Aaron

tsupinie commented 9 years ago

Yes, I see what you mean. I agree that just importing the sharptab module shouldn't require importing all the other stuff that requires PySide.

It appears that when you run import sharppy.sharptab, it executes all the statements in sharppy/__init__.py, even though we only want the sharptab submodule. I suppose this makes sense, but it's still counter-intuitive to me. The decision to have all the __init__.py files import all the submodules was made before I started working on SHARPpy. I'll have to decide what's the best course of action for resolving this.

keltonhalbert commented 9 years ago

I have personally encountered this issue when trying to import on graphic-less servers as well.

To be honest, I didn't know much about how the import system was supposed to be structured, so that's how it ended up in the state it's in. I'm all in favor of changing it though. What's the best means of resolving this?

wblumberg commented 9 years ago

Ditto. I've just been commenting out all references to PySide as a solution, however it's possible the "import pyside" statements could go in a "try, except" block with a message that it's not installed or not found. That should run upon import of any SHARPpy package.

tsupinie commented 9 years ago

@wblumberg, that could work, but that doesn't take care of the unused imports that @aarande noted. The unused imports won't break things, but it uses unnecessary time and memory to load all that stuff and then never use it.

One method (and I think my favored one) would be to remove all import statements from the __init__.py files. I've had issues with circular imports with all those in the past, so that would clear them up in the future.

The other method is try to figure out in sharppy/__init__.py if we're just importing the sharptab module and, and if we are, bypass all the rest of the imports. This is not my favored method because a) it's an awful, awful hack and b) I don't know off the top of my head how we'd do it or if there's even a way to do it.

tsupinie commented 9 years ago

Okay, I think I've successfully implemented the first method described in my previous post. It's checked into the xenia (development) branch.

aarande commented 8 years ago

Thanks for your work on this! I just recently was able to grab the xenia release and work with it. It is working fine now but I am a little worried we may have taken this too far. I think we just needed to remove the imports from the top level sharppy init.py but not from sharptab. It would still be nice to be able to do from sharppy import sharptab and then be able to call things like sharptab.profile, sharptab.interp, etc. The problem came about because by calling from sharppy import sharptab it would run the init.py in sharppy which would import all the viz stuff. I worry a little about namespace collision now since have to do a from sharppy.sharptab import * or name the module level file. It can also get confusing to a user when they are using names like profile, interp, etc. It would keep things cleaner and avoid namespace collision if we were calling them by sharptab.profile, sharptab.interp, etc.

I will play around with this some more and let you know what I find and maybe submit a pull request.

Again thanks for all your work!

tsupinie commented 8 years ago

The internal SHARPpy code makes frequent use of the import sharppy.sharptab.profile as profile constructs, so might I suggest that, if you don't want to clutter up your namespace. We (I) decided to go with the "explicit is better than implicit" philosophy when re-designing the package imports. And a lot of the internal code was already imported that way, anyway.