Hi Joseph, You just have to import the module:
import grass.script as gscript See also: https://grass.osgeo.org/grass72/manuals/libpython/script_intro.html Cheers Stefan From: grass-user [mailto:[email protected]] On Behalf Of Joseph Kariuki Sent: onsdag 5. april 2017 14.43 To: Andres Solarte <[email protected]> Cc: GRASS user list <[email protected]> Subject: Re: [GRASS-user] GRASS Environment Variables Hi Andres, I have run the code in pycharm but still raises an error /usr/bin/python2.7 /home/hempire/PycharmProjects/GRASS/ingrass.py Traceback (most recent call last): File "/home/hempire/PycharmProjects/GRASS/ingrass.py", line 43, in <module> gscript.run_command('g.region', n=float(n), s=float(s), e=float(e), w=float(w), ewres=float(res), nsres=float(res)) NameError: name 'gscript' is not defined Process finished with exit code 1 However on terminal it executes with no errors. I will stick to using terminal. PyCharm is limiting. Kind Regards, Joseph Kariuki Geospatial Engineer | GIS / Web Developer On Wed, Apr 5, 2017 at 2:12 AM, Andres Solarte <[email protected]<mailto:[email protected]>> wrote: Hello Joseph, This is the way I work with grass from python: #!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys #1. Define function def conectar(location,mapset): # path to the GRASS GIS launch script grass7bin = '/usr/bin/grass72' # Set GISDBASE environment variable gisdb = "/media/andres/Andres/TesisGrass" os.environ['GISDBASE'] = gisdb # query GRASS 7 itself for its GISBASE gisbase = "/usr/lib/grass72" os.environ['GISBASE'] = gisbase # define GRASS-Python environment gpydir = "/usr/lib/grass72/etc/python" sys.path.append(gpydir) # import GRASS Python bindings import grass.script.setup as gsetup ############ launch session gsetup.init(gisbase, gisdb, location, mapset) #2. Now you can connect with your projects location = "your location" mapset = "your mapset" conectar(location,mapset) #3. Call function (examples) gscript.run_command('g.region', n=float(n), s=float(s), e=float(e), w=float(w), ewres=float(res), nsres=float(res)) gscript.run_command("r.in<http://r.in>.gdal", input="/home/..../image.tif", flags='e',output = "image") 2017-04-04 11:12 GMT-03:00 Joseph Kariuki <[email protected]<mailto:[email protected]>>: Hi? I have been trying to call GRASS modules using python outside GRASS session, I am coding in PyCharm however, I am having a problem importing grass scripts and the solution being provided is setting GRASS environment variables. How do I go about that so to import GRASS modules in Pycharm? Kind Regards, Joseph Kariuki Geospatial Engineer | GIS / Web Developer _______________________________________________ grass-user mailing list [email protected]<mailto:[email protected]> https://lists.osgeo.org/mailman/listinfo/grass-user
_______________________________________________ grass-user mailing list [email protected] https://lists.osgeo.org/mailman/listinfo/grass-user
