I am following the instructions in https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly#Python:_GRASS_GIS_7_with_existing_location to setup GRASS 7.8 to work with Python 3.6 for Windows. GRASS 7.8 was installed with a QGIS 3.10 installation.

The script throws an error on the last line of my code and I am not able to get any further - TypeError: a bytes-like object is required, not 'str' .

Here is my code:

import os
import sys
import subprocess

grass7bin_win = '\QGIS 3.10\bin\grass78.bat'
gisdb = os.path.join(os.path.expanduser("~"), "grassdata")
location = "/newLocation"
mapset = "mapset_first"

if sys.platform.startswith('win'):
    grass7bin = grass7bin_win
    raise OSError('Platform not configured.')

startcmd = [grass7bin, '--config', 'path']

p = subprocess.Popen(startcmd, shell=False,
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
    print(sys.stderr, "ERROR: Cannot find GRASS GIS 7 start script (%s)" % startcmd)
gisbase = out.strip('\n\r')

I have tried using gisbase = out.strip(b'\n\r'), and this prevents the error from occurring on this line but it causes an error on the next line - TypeError: str expected, not bytes - for os.environ['GISBASE'] = gisbase


gisbase is the top level directory for the GRASS installation, so by setting it to this you can avoid this error. gisbase = r'C:/GRASS GIS 7.8'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.