I have installed Python and Grass6.4 using Macports. When I tried to use "import grass.script" in python environment, it gives "no module found" error. I'd spend much time to fixed it but could not succeed. Please help me to fix this issue in MacOS Maverick Lion 10.9 When U issued the following commands I get,

which python /usr/bin/python

which grass64 /opt/local/bin/grass64

1 Answer 1


Why did you install Python from MacPort while there are many versions of Apple Python pre-installed ? (look at QGIS on Mavericks)

I will try to help you but I don't use MacPorts and I don't know where the GRASS libraries are installed. I think you'd have to ask a MacPorts list. The problem is that the great majority of users do not know what is MacPorts and do not use it because the easiest solution to install GRASS GIS is the KyngChaos version. It uses the Apple Python (version 2.7.x, in /usr/bin/).

MacPorts do not take into account what is already installed and install all in /opt/ which can create real problems in the management of library paths (ie which version of Python, Apple, in /usr/bin/ or MacPorts, in /opt/local/bin/python2.7 ?). The Homebrew solution is "cleaner" (in /usr/local/Cellar with symlinks in /usr/local/bin) and is based on existing libraries if they are up to date, otherwise it installs its own versions of the libraries (in /usr/local/).

In order to use GRASS functionality via Python from outside, some environment variables have to be set, look at Creating Python scripts that call GRASS functionality from outside and Working with GRASS without starting it explicitly and the discussion in Using Python scripts that call GRASS modules or access GRASS layers from outside the GRASS.app with Mac OS X: a little summary, for the KyngChaos version.

1) First Solution

You need to export:

  • GISBASE needs to be set to the top-level directory of the GRASS installation.

    export GISBASE= "MacPorts GISBASE path"

  • LD_LIBRARY_PATH -> .../lib/ folder of GRASS GIS

    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GISBASE/lib" # in MacPorts ?

  • Add the GRASS Python module to PYTHONPATH (.../etc/Python/ of GRASS GIS)

    export PYTHONPATH="${PYTHONPATH}:$GISBASE/etc/python/" # but is it the PYTHONPATH of MacPorts ?

  • Add the path to GRASS settings file (.grassrc6 file)

    export GISRC="/.../.grassrc6" # in MacPorts ?

And after:

import grass.script as grass
import grass.script.setup as gsetup
gisbase = os.environ['GISBASE']
location="a location"
mapset=" a mapset"
gsetup.init(gisbase, gisdb, location, mapset)
# table
desc = grass.parse_command('db.describe', flags='c', table="a_table"

2) second solution, all in Python

 import os
 os.environ['GISBASE'] = "MacPorts GISBASE path"
 os.environ['PYTHONPATH']="MacPorts /etc/python/"
 # or
 import sys
 sys.path.append("MacPorts /etc/python")
 os.environ['LD_LIBRARY_PATH']="MacPorts /lib path"
 os.environ['GISRC'] = "MacPorts .grassrc6 path"
 import grass.script as grass

I hope I have been able to help you.

  • Hi gene, Thank you very much for the reply. I would definitely follow your instructions and will let you know for sure.
    – user158795
    Oct 11, 2014 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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