zarch / grass-session

Handle GRASS GIS sessions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with core.py

andrew-plowright opened this issue · comments

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

Dear @andrew-plowright ,

sorry for the delay, I think you believe to use python37, but actually the osgeo grass binary file is using python27.
Please, check in your script which python version are you using with the following lines:

import sys
print(sys.version)

Let me know the result.

Hi @zarch . No worries. I can confirm that I've set all the environment variables to ensure that Python37 is being used:

import sys
print(sys.version)
3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)]

GRASS 7.8 will be the first version with support for Python 3. So, I would say this is no grass-session issue.

@andrew-plowright, please test the release candidate that will be made available the next days...

FYI: Meanwhile GRASS GIS 7.8.0 with Python 3 support is out

Thanks for the update!