I installed GRASS GIS 7.8.1 on Windows 10 using a OSGeo4W.

I would like to use in a stand alone Python 3 script the i.segment GRASS tool.

I saw some related questions (e.g., Using GRASS GIS in Python standalone scripts and call grass modules via python from outside) but they all relevant to Python 2.7, or Linux, or some old versions of GRASS or asking you to install GRASS (which is already installed on my computer via OSGeo4W). I don't mind using subprocess to exceute the i.segment function or to use it "fully" in my script.

Any easy and straightforward way to achieve that?

1 Answer 1


Standalone Python scripts which run GRASS GIS commands

The "grass-session" Python library is an easy way to control GRASS GIS from "outside"-Python. To install the current version, run:

pip install grass-session

A usage example is also found in the GRASS GIS Wiki, Section:

Python: GRASS GIS 7 with an external library: grass-session

Example for usage of i.segment in a Python script

A Python 3 script (addon) which uses i.segment is i.segment.uspo (Python code) which you may look at for inspiration.

How to write Python GRASS GIS 7 addons

An alternative to "grass-session" is to write a proper Python GRASS GIS 7 addon and then run it through the --exec interface of the GRASS GIS start program grass7 --exec ... like a batch job. The advantage is that GRASS offers a real parser to handle flags and parameters which also generates help texts, the GUI instructions, offers JSON output for REST API and much more.

See for example the tutorial "How to write a Python GRASS GIS 7 addon" for details: https://github.com/wenzeslaus/python-grass-addon

  • I saw it before but I'm not sure how to use it (or if it is complete), can you help me with that? Meaning, it looks like the proper algorithms in Python, should I just copy-past it in my Python IDE (which is Jupyte notebook)? and how do I actually use it -where do I insert the input and parameters? Thanks
    – user88484
    Commented Dec 8, 2020 at 5:52
  • Good point: I have added a link to a useful tutorial.
    – markusN
    Commented Dec 8, 2020 at 8:17
  • Excuse me @markusN, perhaps it can be totally a new question on GIS SE, but can you refer me to any tutorial(s) about the setting&using of a standalone Python GRASS GIS.
    – Taras
    Commented Dec 8, 2020 at 8:44
  • 1
    @Taras I have rewritten my answer, hope it is better understandable now.
    – markusN
    Commented Dec 8, 2020 at 17:34
  • Thank you @markusN
    – Taras
    Commented Dec 9, 2020 at 7:46

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.