Having trouble getting snappy to import GPF, HashMap etc

I’ve got what seems to be a simple but frustrating error using snappy within python scripts (Python 3.4.5).

I have a script called process_with_snappy.py that contains a number of functions that use snappy- if I run my script directly, everything is fine - operations like:

GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
HashMap()
ProductIO.readProduct()
GPF.createProduct()
ProductIO.writeProduct(

run through and my files write out correctly. Seems to be the same if I run everything from a Python console.

However, when I run another script that imports the functions from process_with_snappy.py, snappy errors on import.

‘from snappy import ProductIO’ throws up 'ImportError: cannot import name ‘ProductIO’’ etc.

If I import snappy instead, this seems to work, but as soon as I try and call something, for example:

snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis() I get the error: AttributeError: module ‘snappy’ has no attribute ‘GPF’

Does anyone know what might be going on here?

Haven’t use GPF that way for a while now, but it should work as import function right away, at least I did not have any trouble in these examples (just ProductIO):

Would like to see how you call the function you are importing to the new python file.

One more thing could be worth trying and that is checking for “environment variables” for python on windows or the console functionality of import “anything” for python in linux as well.

Hope something helps.

Petr

1 Like

I’m on Ubuntu for reference.

It is my thinking also that it’s just some path setting somewhere that is incorrect.

I did everything as per https://senbox.atlassian.net/wiki/spaces/SNAP/pages/50855941/Configure+Python+to+use+the+SNAP-Python+snappy+interface for configuring python and have added ‘/home/cate/miniconda3/envs/envname/lib/python3.4/site-packages/snappy’ to my sys.path, which is the snappy dir I copied to my site-packages folder.

The way I am calling the function is like this:

main.py
from process_with_snappy import process_img

image = process_img(path_to_image)

process_with_snappy.py

def process_image(image_path):

GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
ProductIO.readProduct()

etc…
return processed_image

with the python config you should just need the python path, that part seems fine, for GPT you need the SNAP toolbox path as well, but that is not an issue here.

You are importing only the function and not the script you wrote so it should be taking only a part of your code i. e. not the imports so it gets messy.

try:

main.py
import process_with_snappy as proc

image = proc.process_img(path_to_image)

process_with_snappy.py

def process_image(image_path):

GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
ProductIO.readProduct()

etc…
return processed_image

It is a minor change for syntax, but I hope it will work, based on that your processing functions are working.

Petr

Thanks for the quick reply - just made your changes and error is the same unfortunately.

Seems like it doesn’t matter whether I import the function or the whole script, as soon as main.py runs, all the libraries import from each script before any code runs. Hence getting the import error straight away before the function is called.

Do the the console commands work:

python

from snappy import GPF

GPF

Though in windows the same should go for Ubuntu, still not sure how your processing works and the other not. Do you have any other python instances? Like from ArcGIS, Anaconda, etc.?

Yep looks the same - I’m in a Python 3.4 environment (because snap isn’t supported in 3.5 https://senbox.atlassian.net/wiki/spaces/SNAP/pages/19300362/How+to+use+the+SNAP+API+from+Python)

Could still be the ArcGIS python instance if it is present or similar other than that I am running out of ideas, you can try to wrap the operators into a new function in the main file and call it, that would be one more thing that could be done, but other than that that it could get more and more complicated.

main.py
import process_with_snappy as proc

def image_output (sourcepath):
image = proc.process_img(path_to_image)

image_output(sourcepath)

Ah - might have figured it out - at the top of each script I have defined:

!/usr/bin/env python3.4
coding: utf-8

however, before one script used ‘python3’ and the other used ‘python3.5’.

Changed them both to 3.4 and things are now running - (although not confirmed working yet as I won’t believe it until the processing completes!)

Annoyed now!:confounded:

1 Like

Yeah different python versions are always funny, I had the trouble with python for the ArcMap as it sort of overwrote the path to my primary python which was pain in the neck and could figured that one out for like two hours.

Fingers crossed. ^^

1 Like

Yep that’s done the trick!

2 Likes

Hi there,
I have a very similiar problem… I cannot import GPF, ProductIO either (see screenshot)

How to fix this?