-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Hello. I've been able to get grass.script running in a Python script using grass_session. I have encountered one issue when using OSGeo4W, which I've been able to solve by using a very inelegant solution. I am wondering if this could be solved by updating grass_session?
I am using version 7.4.2 of grass, installed with the latest version of OSGeo in Windows. My Python script contains the following code:
import os
os.environ["GRASSBIN"] = "C:/OSGeo4W64/bin/grass74.bat"
from grass_session import Session
import grass.script as gs
I am calling it from the command line using the Python37 interpreter that comes with Osgeo. I set the following environmental variables first.
set OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Python37\Scripts
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
%OSGEO4W_ROOT%\apps\Python37\python.exe C:\myfiles\myScript.py
This produces the following error:
Traceback (most recent call last):
File "C:\OSGEO4~1\apps\grass\grass-7.4.2\etc\python\grass\script\core.py", line 39, in <module>
import __builtin__
ModuleNotFoundError: No module named '__builtin__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Andrew\Desktop\scratch\delete_me.PY", line 5, in <module>
import grass.script as gs
File "C:\OSGEO4~1\apps\grass\grass-7.4.2\etc\python\grass\script\__init__.py", line 5, in <module>
from .core import *
File "C:\OSGEO4~1\apps\grass\grass-7.4.2\etc\python\grass\script\core.py", line 44, in <module>
from os import environb as environ
ImportError: cannot import name 'environb' from 'os' (C:\OSGEO4~1\apps\Python37\lib\os.py)
I did some digging and, as the error message suggests, the problem lies within \grass-7.4.2\etc\python\grass\script\core.py. After having made a back-up, I changed the lines 37-45 of this file from this:
try:
# python2
import __builtin__
from os import environ
except ImportError:
# python3
import builtins as __builtin__
from os import environb as environ
unicode = str
To this:
import builtins as __builtin__
from os import environ
unicode = str
After having made this change, everything works. I don't entirely understand why, but obviously having to edit grass files is not a great solution. Do you know if this is something that can be resolved using grass_session, or by perhaps changing some environmental variables that I wasn't aware of?
Thank you for your time