3

in the 'System Variables' i set

Path =C:\OSGeo4W64\apps\qgis\bin

This is where the 'qgis_core.dll' is located.

PYTHONPATH=C:\OSGeo4W64\apps\qgis\python

But when i import qgis.core i get:

   run o-help for a list of available commands
C:\>python
Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\OSGeo4W64\apps\qgis\python\qgis\__init__.py", line 27, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "C:\OSGeo4W64\apps\qgis\python\qgis\core\__init__.py", line 30, in <module>
    from qgis._core import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
>>>

When I change the paths to osGeo4W directories (Rather then the osGeo4W64) I get:

run o-help for a list of available commands
C:\>python
Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\OSGeo4W\apps\qgis\python\qgis\__init__.py", line 27, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "C:\OSGeo4W\apps\qgis\python\qgis\core\__init__.py", line 30, in <module>
    from qgis._core import *
ImportError: DLL load failed: The specified module could not be found.
>>>

Can sombody understand what I'm doing wrong?

2

Your system python appears to be different. It is 2.7.4 32-bit.

C:\OSGeo4W64\apps\qgis\python is a 64-bit instance.

I do not recommend trying to make the python that ships with QGIS your system python - this is possible to a degree but you run the chance of creating more problems than fixing.

Instead I suggest just using the OSGeo shell that comes with the Windows installation. It can be found under path\to\osgeo4w\OSGeo4W.bat and is meant for exactly this purpose.

Start this, and you will have a scripted command shell from which you can now call qgis.core:

C:\Users\username>python
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSV v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
>>>
3
  • Thank you for your answer! The problem is that I'm working in pycharm and I need to import the dll there. Also I can't find the osgeo4w that comes with windows. – Carmel Han Feb 27 '18 at 8:04
  • 1
    @Carmel Han, have a look at this blog post from Nathan Woodrow for steps on how to set up a PyQGIS development environment for Pycharm. nathanw.net/2014/05/10/setting-up-pycharm-for-pyqgis-and-qt – Ben W Feb 27 '18 at 11:49
  • @Carmel Han, I do the same thing daily - Ben's comment should get you squared away. – auslander Feb 27 '18 at 12:08
2

I found a simple solution using OSGeo4W. In the main QGIS folder you can find a batch script named OSGeo4W.bat that define a lot of environment variables including PATH, PYTHONPATH, GDAL_DATA, etc. After that you can run o-help in order to see all the available commands. One of those commands is `python-qgis-ltr`` or similar that allow you to get a python console supporting all the QGIS libraries.

Certainly you can install other utilities (like IPython) in order to have a more convenient command interface.

0

What it worked for me was to check the environment paths (for path in sys.path) in the QGIS Python ("C:\Program Files\QGIS 3.10\apps\Python37\python.exe") and then add them to 'PYTHONPATH', so they are available to other python interpreters. It is recommended though to do it at runtime to avoid potential conflicts.

0

I have multiple python installations. Found this method worked:

Start OSGeo shell (part of QGIS 3.16 windows install)

Start python's interactive prompt with C:\>python-qgis

If I started python with C:\>python, the wrong python installation started.

You can run a python script in the OSGeo shell: C:\>python-qgis -i E:\test.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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