Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with core.py #8

Open
andrew-plowright opened this issue Jan 29, 2019 · 5 comments
Open

Issue with core.py #8

andrew-plowright opened this issue Jan 29, 2019 · 5 comments

Comments

@andrew-plowright
Copy link

andrew-plowright commented Jan 29, 2019

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

@zarch
Copy link
Owner

zarch commented Apr 17, 2019

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.

@andrew-plowright
Copy link
Author

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)]

@ninsbl
Copy link
Contributor

ninsbl commented Aug 16, 2019

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...

@neteler
Copy link
Contributor

neteler commented Sep 26, 2019

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

@andrew-plowright
Copy link
Author

Thanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants