Search code examples
pythonpycharmqgis

How to run QGIS python editor through Pycharm in Ubuntu os


Is there a way to run the QGIS-editor through Pycharm? QGIS offers a nice set of functions to analyze raster files. For example zonalstatisticsfb calculates the zonal stats of a raster file:

import processing


rasterfile = '/home/iliamous/PycharmProjects/winbank/indicators/tmean/rcp_2_6/cnrm_cerfacs_cm5_r1i1p1_knmi_racmo22e/tmean_rcp26_CNRM-CERFACS-CNRM-CM5_KNMI-RACMO22E_r1i1p1_20210101-20301231.tiff'
shapefile = '/home/iliamous/PycharmProjects/winbank/tk_egsa_dissolved/tk_egsa_disolved.shp'
ofile = '/home/iliamous/PycharmProjects/winbank/zonal_stats.csv'

processing.run("native:zonalstatisticsfb",
{'INPUT':shapefile,
'INPUT_RASTER':rasterfile,
'RASTER_BAND':1,
'COLUMN_PREFIX':'_',
'STATISTICS':[0,2],
'OUTPUT':ofile})

The QGIS editor though lacks important features of the Pycharm editor like debugging. I was wondering if there is a way to run QGIS-editor through Pycharm. There is a lot of information online on how to do that, but it is mostly for Windows and fuzzy for Ubuntu.

My first though was to bypass this entire process by downloading the qgis package that includes the processing function. To do so, I followed the instructions on https://anaconda.org/conda-forge/qgis but it threw at me the following error:

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/gateways/repodata/__init__.py", line 161, in conda_http_errors
        yield
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/gateways/repodata/__init__.py", line 127, in repodata
        response.raise_for_status()
      File "/home/iliamous/.local/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://conda.anaconda.org/conda-forge/label/broken/linux-64/current_repodata.json
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/iliamous/.local/lib/python3.10/site-packages/requests/models.py", line 971, in json
        return complexjson.loads(self.text, **kwargs)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/simplejson/__init__.py", line 525, in loads
        return _default_decoder.decode(s)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/simplejson/decoder.py", line 370, in decode
        obj, end = self.raw_decode(s)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/simplejson/decoder.py", line 400, in raw_decode
        return self.scan_once(s, idx=_w(s, idx).end())
    simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/exceptions.py", line 1132, in __call__
        return func(*args, **kwargs)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/cli/main.py", line 69, in main_subshell
        exit_code = do_call(args, p)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/cli/conda_argparse.py", line 122, in do_call
        return getattr(module, func_name)(args, parser)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/notices/core.py", line 121, in wrapper
        return func(*args, **kwargs)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/cli/main_install.py", line 20, in execute
        install(args, parser, 'install')
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/cli/install.py", line 264, in install
        unlink_link_transaction = solver.solve_for_transaction(
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/solve.py", line 134, in solve_for_transaction
        unlink_precs, link_precs = self.solve_for_diff(update_modifier, deps_modifier,
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/solve.py", line 177, in solve_for_diff
        final_precs = self.solve_final_state(update_modifier, deps_modifier, prune, ignore_pinned,
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/solve.py", line 282, in solve_final_state
        ssc = self._collect_all_metadata(ssc)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/common/io.py", line 84, in decorated
        return f(*args, **kwds)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/solve.py", line 449, in _collect_all_metadata
        index, r = self._prepare(prepared_specs)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/solve.py", line 1064, in _prepare
        reduced_index = get_reduced_index(self.prefix, self.channels,
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/index.py", line 267, in get_reduced_index
        new_records = SubdirData.query_all(spec, channels=channels, subdirs=subdirs,
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/subdir_data.py", line 169, in query_all
        result = tuple(chain.from_iterable(executor.map(subdir_query, channel_urls)))
      File "/home/iliamous/anaconda3/lib/python3.10/concurrent/futures/_base.py", line 621, in result_iterator
        yield _result_or_cancel(fs.pop())
      File "/home/iliamous/anaconda3/lib/python3.10/concurrent/futures/_base.py", line 319, in _result_or_cancel
        return fut.result(timeout)
      File "/home/iliamous/anaconda3/lib/python3.10/concurrent/futures/_base.py", line 458, in result
        return self.__get_result()
      File "/home/iliamous/anaconda3/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
        raise self._exception
      File "/home/iliamous/anaconda3/lib/python3.10/concurrent/futures/thread.py", line 58, in run
        result = self.fn(*self.args, **self.kwargs)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/subdir_data.py", line 158, in subdir_query
        return tuple(
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/subdir_data.py", line 174, in query
        self.load()
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/subdir_data.py", line 264, in load
        _internal_state = self._load()
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/core/subdir_data.py", line 366, in _load
        raw_repodata_str = self._repo.repodata(cache.state)  # type: ignore
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/gateways/repodata/__init__.py", line 120, in repodata
        with conda_http_errors(self._url, filename):
      File "/home/iliamous/anaconda3/lib/python3.10/contextlib.py", line 153, in __exit__
        self.gen.throw(typ, value, traceback)
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/gateways/repodata/__init__.py", line 208, in conda_http_errors
        raise RepodataIsEmpty(
      File "/home/iliamous/anaconda3/lib/python3.10/site-packages/conda/exceptions.py", line 462, in __init__
        body = response.json()
      File "/home/iliamous/.local/lib/python3.10/site-packages/requests/models.py", line 975, in json
        raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
    requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

`$ /home/iliamous/anaconda3/bin/conda install -c conda-forge/label/broken qgis`

  environment variables:
                 CIO_TEST=<not set>
        CONDA_DEFAULT_ENV=base
                CONDA_EXE=/home/iliamous/anaconda3/bin/conda
             CONDA_PREFIX=/home/iliamous/anaconda3
    CONDA_PROMPT_MODIFIER=(base)
         CONDA_PYTHON_EXE=/home/iliamous/anaconda3/bin/python
               CONDA_ROOT=/home/iliamous/anaconda3
              CONDA_SHLVL=1
           CURL_CA_BUNDLE=<not set>
          LD_LIBRARY_PATH=/usr/local/cuda-11.7/lib64:
               LD_PRELOAD=<not set>
                     PATH=/home/iliamous/anaconda3/bin:/home/iliamous/anaconda3/condabin:/usr/lo
                          cal/cuda-
                          11.7/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
                          /usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/iliamous/anacond
                          a3/bin
       REQUESTS_CA_BUNDLE=<not set>
        SNAP_LIBRARY_PATH=/var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void
            SSL_CERT_FILE=<not set>
               WINDOWPATH=2

     active environment : base
    active env location : /home/iliamous/anaconda3
            shell level : 1
       user config file : /home/iliamous/.condarc
 populated config files : 
          conda version : 23.3.1
    conda-build version : 3.23.3
         python version : 3.10.9.final.0
       virtual packages : __archspec=1=x86_64
                          __glibc=2.35=0
                          __linux=5.19.0=0
                          __unix=0=0
       base environment : /home/iliamous/anaconda3  (writable)
      conda av data dir : /home/iliamous/anaconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/conda-forge/label/broken/linux-64
                          https://conda.anaconda.org/conda-forge/label/broken/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/iliamous/anaconda3/pkgs
                          /home/iliamous/.conda/pkgs
       envs directories : /home/iliamous/anaconda3/envs
                          /home/iliamous/.conda/envs
               platform : linux-64
             user-agent : conda/23.3.1 requests/2.28.2 CPython/3.10.9 Linux/5.19.0-40-generic ubuntu/22.04.2 glibc/2.35
                UID:GID : 1001:1001
             netrc file : None
           offline mode : False


An unexpected error has occurred. Conda has prepared the above report.

If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers? [y/N]: 
Timeout reached. No report sent.

Preferably I would like someone to help me use the QGIS-editor through Pycharm. If this is not possible, to point me to some qgis package to install and use through Pycharm. My setup is Ubuntu 22.04.2 LTS, QGIS 3.22.7-Białowieża, PyCharm 2023.1 (Community Edition).


Solution

  • I solved the problem. To do so, I installed QGIS from source, following the instructions from https://github.com/qgis/QGIS/blob/master/INSTALL.md#3-building-on-gnulinux.

    Then, I set up Pycharm for QGIS debugging following the instructions from https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/plugins/ide_debugging.html?highlight=pycharm.