4

I'm having trouble importing and getting code inspection/autocompletion working for all the PyQGIS modules I need to do scripting in VS Code. After looking at these questions:

I see this is a super common issue. These helped me move the needle a bit but can't seem to find a definitive answer on what exactly a Windows user needs in a PYTHONPATH environment to import all necessary modules from qgis.core and processing.

Setup:

I have QGIS (3.18) installed to C:\QGIS3 without OSGEO4W.

Using VS Code 1.60.2, which supports a {workspace}/.env file to define PYTHONPATH with some precision.

Problem:

I have created the .env file in the appropriate workspace, and while VS Code is able to import qgis.core, it cannot import from it (e.g., QgsProcessing, QgsProcessingAlgorithm, QgsProcessingParameterFeatureSource, etc. are all unavailable to me).

I can import from PyQt5.QtCore and autocomplete shows algfactory as an option when I try to import from processing. So I'm close but not there yet.

Here's what my .env file looks like in the workspace:

PYTHONPATH="C:\QGIS3\apps\Python37;C:\QGIS3\apps\qgis\python;C:\QGIS3\apps\qgis\python\qgis\processing;C:\QGIS3\apps\qgis\python\qgis;C:\QGIS3\apps\qgis\python\plugins\processing"

For easier reading, that's:

  • C:\QGIS3\apps\Python37
  • C:\QGIS3\apps\qgis\python
  • C:\QGIS3\apps\qgis\python\qgis\processing
  • C:\QGIS3\apps\qgis\python\qgis
  • C:\QGIS3\apps\qgis\python\plugins\processing

I'm guessing that some of these paths are unnecessary, but I'm just doing everything I can think of to see what works. What am I leaving out?

Because I'm running VS Code via gitbash script where I'm doing some general SSL token stuff to negotiate a local gitlab instance, I'd like to stay away from external batch files (so that I can swap quickly between say, PyQGIS, arcpy, and a generic conda FOSS4G environment within the same VS Code session), and would like to be able to do this via the .env config file, if possible.

4
  • Are they not available (code does not run) or does VS Code not autocomplete/suggest them? Commented Nov 17, 2021 at 22:03
  • @bugmenot123 I put a few import calls in a try and it didn't throw errors, but I'm not sure why pylance is not picking up on the autocomplete (which is why one would use VS Code to work with PyQGIS in the first place, presumably).
    – auslander
    Commented Nov 18, 2021 at 17:56
  • Any update on this issue from your side @auslander?
    – Riccardo
    Commented Jul 19, 2023 at 11:53
  • 1
    @Riccardo Nope. Honestly, I gave up on QGIS scripting. It just doesn't have much of a place in our Esri-heavy work environment and I couldn't justify the time and friction involved with getting it running. (Though I do still use the QGIS app itself for occasional one-off things).
    – auslander
    Commented Jul 20, 2023 at 14:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.