OSGeo / grass

GRASS GIS - free and open-source geospatial processing engine
https://grass.osgeo.org
Other
828 stars 301 forks source link

[Bug] os.add_dll_directory in wxgui.py (necessary in Python 3.9) #1423

Open hellik opened 3 years ago

hellik commented 3 years ago

Describe the bug

taken from libgrass_gis.7.8 Not Found - OSGeo4W trac

wxpython updated to 4.1.1 and corresponding wxwidgets and added use of 
os.add_dll_directory in wxgui.py (necessary in Python 3.9). 
Changes see ​jef-n/OSGeo4W@9443c34538ae) 

fix see https://github.com/jef-n/OSGeo4W/commit/9443c34538aed62944f30db98867fa52ccdadf6c

nilason commented 3 years ago

Just wondering, maybe os.add_dll_directory() could be placed in lib/python/ctypes/loader.py (7.8 branch) instead of gui/wxpython/wxgui.py?

hellik commented 3 years ago

@petrasovaa any idea?

petrasovaa commented 3 years ago

@petrasovaa any idea?

Well, I am not an expert on this, what @nilason suggests sounds reasonable. Here is my attempt, without any testing:

+++ b/python/grass/ctypes/loader.py
@@ -238,6 +238,12 @@ class _WindowsLibrary(object):
 class WindowsLibraryLoader(LibraryLoader):
     name_formats = ["%s.dll", "lib%s.dll"]

+    def __init__(self):
+        super().__init__()
+        for p in os.getenv("PATH").split(";"):
+            if os.path.exists(p) and hasattr(os, 'add_dll_directory'):
+                os.add_dll_directory(p)
+
     def load(self, path):
         return _WindowsLibrary(path)

I have no idea if it's necessary to add all the folders on PATH.

ninsbl commented 3 years ago

Can that also help with using ctypes-based components in external python scripts (e.g. in Jupyter):

https://pypi.org/project/grass-session/ for example uses the workaround here: https://github.com/zarch/grass-session/blob/6097f834be20ee0d703deb2af90f22d6dc66f368/grass_session/session.py#L332-L370

In any case it would be great to get that fixed one way or the other for packaging the next release on MS Windows, which is an important platform...

Will see if I can test Annas suggestion on MS Windows...