0

I'm trying to use PyQGIS with Streamlit. I installed streamlit into PyQGIS build with "C:\OSGeo4W\bin\python-qgis.bat -m pip install streamlit" and solved the dependancies. I'm trying to run streamlit application with "C:\OSGeo4W\bin\python-qgis.bat -m streamlit run myapp.py".

Python-qgis.bat normally adds all the system paths etc. to load a dll file. With streamlit, however, running "from qgis.core import *" fails with the following:

ImportError: DLL load failed while importing _core: No se encontró el proceso especificado.
Traceback:
File "C:\OSGeo4W\apps\Python39\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
File "C:\Users\Tambet\eclipse-workspace\qgis test\strl1.py", line 27, in <module>
    from qgis.core import *
File "C:\OSGeo4W\apps\qgis\python\qgis\core\__init__.py", line 25, in <module>
    from qgis._core import *

I cannot find the exact information from internet, which DLL files QGIS core needs, but I found qgis_core.dll from "C:\OSGeo4W\apps\qgis\bin" and I tried to add it with the following commands:

os.environ['PYTHONPATH'] += ";C:\\OSGeo4W\\apps\\qgis\\bin"
os.add_dll_directory("C:\\OSGeo4W\\apps\\qgis\\bin")
sys.path.append("C:\\OSGeo4W\\apps\\qgis\\bin")

Under streamlit, I printed PYTHONPATH and PATH, and they are exactly the same as I get, when I run python-qgis.bat python console and ask from it about the paths (where importing from qgis.core works perfectly). So it could be somehow about the dll directories.

Which dll directories qgis.core needs and where they should be added? Has anybody any experience about using qgis with streamlit?

  • Some troubleshooting steps:
    • Run C:\OSGeo4W\bin\python-qgis.bat . Type import qgis.core. Does it throw errors?
    • Run C:\OSGeo4W\bin\python-qgis.bat again. Type import streamlit. Does that throw errors?
    • Run C:\OSGeo4W\bin\python-qgis.bat -m http.server to check if calling python-qgis.bat with the -m switch works in your setup. (Terminate the webserver with Ctrl-C twice.)

    The interactive python shell can be closed by typing exit().

    – til_b Apr 21 '23 at 10:17
  • 1
    Running python-qgis.bat and importing qgis.core works perfectly. Import streamlit also does not throw any errors. Running -m http.server says "Serving HTTP on :: port 8000 (http://[::]:8000/) ...", so it works. – Tambet Väli Apr 21 '23 at 10:20
  • Streamlit makes use of python virtual environments. See https://docs.streamlit.io/library/get-started/installation . So maybe the correct question to ask would be "How can I use QGIS in a venv?" This may be a starting point: https://gis.stackexchange.com/questions/201115/how-to-access-qgis-module-from-my-virtualenv – til_b Apr 21 '23 at 10:32
  • Please edit your original question so that the community can review whether it is ready to be re-opened. – PolyGeo Apr 21 '23 at 10:33
  • Linked question has been removed. – Tambet Väli Jul 31 '23 at 08:39

0 Answers0