There are a lot of legitimate questions with regards to the installation fails of the snappy SNAP Python API. I also had a series of issues.
@marpet I don’t want to complain to much because it helped me a lot, but I think the guide Configure Python to use the SNAP-Python (snappy) interface should somehow be at least partly rewritten because it contains some issues.
First, it should specify directly from the beginning that the most recent version of Python are NOT COMPATIBLE.
Secondly, more important, it should at least mention the importance of creating a dedicated environment.
Here-below, I listed the steps that are required to create a python environment dedicated to SNAP.
- Install python3.6
sudo apt update
sudo apt upgrade
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.6
sudo apt install libpython3.6-dev
From the Linux manual, the two first commands will re-synchronize the package index files from their sources and install the newest versions of all packages currently installed on the system, and are often used together. The third command will add a new Personal Package Archive (PPA) to our Linux sources. In this case, we are interested in deadsnakes, dedicated to more recent python versions. After resynchronizing the package indexes, we can finally install the python3.6, mandatory to use snappy. We also installed the libpython-dev to extend the python interpreter. Note that some steps might be skippable, depending on your system.
- Configure snappy
Using the SNAP installer, launch the command
./esa_snap_xxxxxxxx
with xxxxxxxx the snap version you downloaded.
When asked during the installation, provide the path to the python
executable. This path can be given using the command which python3.6
.
- Installing Virtualenv
sudo apt install pip
python3.6 -m pip install virtualenv
The first command will install pip, allowing you to install new python libraries. In the second command, we install the virtual environment library.
- Create and setup the virtual environment
virtualenv --python=/usr/bin/python3.6 snappy-env
source ~/snappy-env/bin/activate
python ~/.snap/snap-python/snappy/setup.py install
From the first command, we create a new environment called snappy-env that specifically uses the python3.6. We can enter the virtual environment using the second line. From there, we are in a safe place where we are sure we don’t mess things up and destroy long-established dependencies. The last command add the snappy library in your python environment. From there, snappy can be imported in your python codes. To exit the environment, simply type the command deactivate
.
- Usage
To test the installation, write python in a terminal and try to import snappy using import snappy
. Or directly use
python -c "import snappy"
- Installing other libraries
Since you are using a new virgin python version in a dedicated environment, you probably need new libraries, suh as matplotlib, numpy, spectral, and so forth.
python -m pip install numpy matplotlib spectral
I hope it can help.