16

Since the update to PyCharm 2019 I am unable to load the QGIS Python modules. I can not use auto completion either. I already deleted the cache (by invalidating the cache in PyCharm and also by deleting the "system" folder in the user settings directory), nothing seems to work.

When starting the Python console inside of PyCharm and typing "import qgis.core", I get the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\OSGEO4~1\apps\qgis-ltr\python\qgis\core\__init__.py", line 27, in <module>
    from qgis._core import *
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.

Something seems to be broken, in PyCharm 2018.3 everything was ok. The sys.path environments and interpreter settings seem to be correct:

['C:\\Program Files\\JetBrains\\PyCharm Community Edition '
 '2018.2.2\\helpers\\pydev',
 'C:\\OSGEO4~1\\apps\\qgis-ltr\\python',
 'C:\\OSGEO4~1\\apps\\qgis-ltr\\python\\plugins',
 'C:\\Program Files\\JetBrains\\PyCharm Community Edition '
 '2018.2.2\\helpers\\third_party\\thriftpy',
 'C:\\Program Files\\JetBrains\\PyCharm Community Edition '
 '2018.2.2\\helpers\\pydev',
 'C:\\OSGeo4W64\\apps\\Python37\\python37.zip',
 'C:\\OSGEO4~1\\apps\\Python37\\DLLs',
 'C:\\OSGEO4~1\\apps\\Python37\\lib',
 'C:\\OSGeo4W64\\apps\\Python37',
 'C:\\OSGEO4~1\\apps\\Python37',
 'C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages',
 'C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages\\win32',
 'C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages\\win32\\lib',
 'C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages\\Pythonwin']

Has anyone updated PyCharm to 2019 and can confirm this? I also reinstalled the whole QGIS installation, but nothing seems to work.

It looks like the DLLs containing the stubs are incompatible now?

My bat for starting PyCharm looks like this:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass-7.4.2\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.4.2\lib
path %PATH%;%OSGEO4W_ROOT%\apps\Qt5\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Python37\Scripts
path %PATH%;C:\Program Files\Docker\Docker\Resources\bin
path %PATH%;C:\Program Files\7-Zip

set QT_PLUGIN_PATH=C:\OSGeo4W64\apps\Qt5\plugins

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37

start "PyCharm aware of Quantum GIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\bin\pycharm64.exe" %*
7
  • Show me your *.bat file that you use to run PyCharm, compatible with QGIS. Commented Apr 1, 2019 at 11:04
  • C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.2\bin\pycharm64.exe points to your new PyCharm 2019? Commented Apr 1, 2019 at 11:31
  • yes it does.. PyCharm is starting, but it seems to be unable to load the qgis DLLs
    – TheGrudge
    Commented Apr 1, 2019 at 11:35
  • I tried to set the PYTHONHOME in C:\OSGeo4W64\etc\ini\python-core.bat to C:\OSGeo4W64\apps\Python37 as well, but nothing seems to work.
    – TheGrudge
    Commented Apr 1, 2019 at 11:36
  • I used to have such problems with DLLs, but with additional asyncio libraries. I found that I installed double identical libraries in the root directory C:\OSGeo4W64\lib and the side directory lib C:\OSGeo4W64\apps\Python37\lib. After removing the side lib installation (using PyCharm), the problem was resolved. But your problem is not identical. Commented Apr 1, 2019 at 11:42

6 Answers 6

3

This still works with PyCharm >= 2019. You may just need to change the Python interpreter that you reference in your PyCharm project. I use the below .bat file to start PyCharm then set the project interpreter to point to OSGEO4W_ROOT\bin\python-qgis.bat, python3.exe in the same directory may also work.

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat

@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.6.0\lib
path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37

set PATH=C:\Program Files\Git\bin;%PATH%

start "PyCharm aware QGIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.2\bin\pycharm64.exe" %*
1
  • worked for me, thanks Commented Feb 5, 2021 at 12:27
3

Updated

I reinstalled Windows 10 (clear installation), QGIS 3.10 (clear OSGeo4W installation), PyCharm 2019.3.1 (Community Edition Build #PC-193.5662.61, built on December 18, 2019) and I confirm that I have no errors while execute from qgis._core import * with PyCharm launched with such *.bat file:

@ECHO off

set OSGEO4W_ROOT=C:\OSGeo4W64

call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

set GDAL_FILENAME_IS_UTF8=YES

set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

SET PYCHARM="C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\bin\pycharm64.exe"

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python37\lib\site-packages;%PYTHONPATH%

set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

start "PyCharm aware of QGIS" /B %PYCHARM% %*
6
  • I took your bat file and only changed the path to pycharm, but unfortunately it is not working. I invalidated the cache again and re-indexed the project, but the QGIS imports are not working (the same DLL load error)
    – TheGrudge
    Commented Apr 1, 2019 at 13:06
  • could this be a problem: PyDev console: starting. Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 sys.platform 'win32' I've just seen that the python console seems to think it is running on win32
    – TheGrudge
    Commented Apr 1, 2019 at 14:15
  • @user782457 Here is my python startup message (same as yours): PyDev console: starting. Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Commented Apr 2, 2019 at 4:09
  • yeah I guess Python under Windows is not telling you the correct architecture, there seems to be other modules that can print the correct information. Anyway I installed QGIS 3.4.6 with the netinstaller and PyCharm 2019 on a new machine, and I'm still unable to load the DLLs. So it doesn't seem to be a problem with my installation.
    – TheGrudge
    Commented Apr 2, 2019 at 8:05
  • @TheGrudge Now I got no problems with PyCharm. Answer updated. Commented Jan 14, 2020 at 11:35
2

Edit 1: Seems to be only a PyCharm 2019.1 issue. As a temporary solution the start-script works fine with PyCharm 2018.3.6, wich can be downloaded from https://www.jetbrains.com/pycharm/download/previous.html

This errors occurs with PyCharm Professional 2019.1 as well, but it is possible to import the qgis modules from a "normal" python that was started with the same startup script / environmental settings as described above:

set OSGEO4W_ROOT=C:\Program Files\QGIS 3.6
set PYCHARM="C:\Program Files\JetBrains\PyCharm 2019.1.1\bin\pycharm64.exe"
@ECHO off

call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

set GDAL_FILENAME_IS_UTF8=YES

set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python37\lib\site-packages;%PYTHONPATH%

set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

:: A python able to call "import qgis.core"
start python

:: A python unable to call "import qgis.core"
start "PyCharm unfortunatelley not-aware of QGIS" /B %PYCHARM% %*
1

This Stack Overflow answer points to there being a known PyCharm issue (with Anaconda at least) in PyCharm 2019.1 causing the ImportError: DLL load failed: The specified module could not be found issue.

It's intended to be fixed in PyCharm 2019.1.2

1
  • Unfortunately it is still present in 2019.1.3
    – TheGrudge
    Commented Jun 5, 2019 at 8:37
1

I have verified this issue. Works fine in PyCharm up to 2018.3.7, broken in 2019.1.3. My environment variables are set up correctly, as I can access the QGIS python modules via the command line and earlier versions of PyCharm when launching them with the same .bat file.

Definitely a PyCharm bug. Other users have reported similar issues.

1
  • I am back to PyCharm 2018.3.7, since I was unable to get anything working for nearly 5 months. Now the DLL load error is gone, I can run my unittests and other commands in PyCharm again. I don't know what they've changed in 2019.x, but it causes nothing but problems for me.
    – TheGrudge
    Commented Jul 21, 2019 at 16:31
0

In addition to Jonny's answer and TheGrudges's comment to that:

It's still not fixed in PyCharm 2019.2

Not the answer you're looking for? Browse other questions tagged or ask your own question.