8

I'm trying to use Visual Studio Code (VSC) as IDE for my QGIS plugins with the Python extension, but something doesn't work as expected. I've been reading about the PyCharm config from Gary S. but I couln't apply it to VSC.

Should I use an .env file? What should I set in the settings.json? How to call batch files when VSC starts?

Midavalo
  • 29,696
  • 10
  • 48
  • 104
Guts
  • 612
  • 1
  • 6
  • 12
  • All ide's i know, a batch file is used to start them. The starting procedure is analoque to the batch file to start qgis. There are also differences between qgis2 and qgis3. – Andreas Müller Aug 07 '18 at 19:21

7 Answers7

6

We didn't want to use a script to open our workspace, so we finally found out how to configure Visual Studio Code through the settings.json file: https://github.com/isogeo/isogeo-plugin-qgis/blob/master/.vscode/settings.json

The requirement is to have QGIS LTR installed with OSGeo4W on the computer. We automated this step with the command-line: https://gist.github.com/Guts/6303dc5eb941eb24be3e27609cd46985

Guts
  • 612
  • 1
  • 6
  • 12
  • 1
    This changes settings of workspace or for everything else as well? I only want QGIS python for one of VS code's projects. – ar-siddiqui Sep 15 '21 at 04:34
4

Create an empty file named visual_studio_code_for_qgis.bat

Paste this:

@echo off
path %PATH%;C:\Program Files\QGIS 3.2\bin
path %PATH%;C:\Program Files\QGIS 3.2\apps\grass\grass-7.4.1\lib
path %PATH%;C:\Program Files\QGIS 3.2\apps\Qt5\bin
path %PATH%;C:\Program Files\QGIS 3.2\apps\Python36\Scripts

set PYTHONPATH=%PYTHONPATH%;C:\Program Files\QGIS 3.2\apps\qgis\python
set PYTHONHOME=C:\Program Files\QGIS 3.2\apps\Python36   

start "VisualStudioCode for QGIS" /B "C:\Users\Zoran\AppData\Local\Programs\Microsoft VS Code\Code.exe" %*

Update paths if necessary.

Run the visual_studio_code_for_qgis.bat

More details can be found here: http://gis-expert.com/wp/2018/10/15/how-to-setup-qgis-3-and-visual-studio-code-plugin-development-windows/

Zoran Petrović
  • 635
  • 4
  • 15
  • I've read your answer and your article (sorry for the delay) but I can't make it work following your instructions. Paths are really different, especially because I've both 2.18 and 3.4 versions installed through the OSGeo4W installer (64 bits).

    Otherwise, in your article, you should not advise to install packages in the Python system installation, especially with admin level (use of --user option is preferred). For example, PyQt5 is already included into QGIS installation (try to import it into the python-qgis interpreter launched from OSGeo4W Shell).

    – Guts Jan 17 '19 at 06:15
  • You've chosen to install QGIS differently than Zoran suggests in his instructions. Osgeo4w organizes things differently than if you use QGIS standalone installer which is how Zoran suggests. – cndnflyr Jul 04 '19 at 22:59
  • Works great, thanks a lot! – Erik Thysell Dec 21 '21 at 07:02
1

Here I explain how configure Visual Studio Code for running alone scripts in Python that executes funcions of Qgis.

In the file "Visual_Studio_Code_Qgis\Work_Space_Configuration.code-workspace" is my file configuration that set the envoirment of operatig system.

With this configuration if you run your aplication from Visual Studio code there will be no errors with Python and Qgis.

For use this information: 1.- Goto to the configuration of workspace (Control+) 2.- Select workspace 3.- In right upper square click to Open Configuration Json 4.- Paste my code insight changing Directories.

In my configuration file I have a envoirment, if you don't want or don't have, delete the last lines.

This do the same like file "C:\Program Files\QGIS 3.12\bin\python-qgis.bat".

So if you run this file in CMD, and after that in the same CMD window, you write "set" and enter, you will have a list with the configuration of your system.

RBenet
  • 780
  • 4
  • 15
1

This works for me Edit your settings.json with the variables below. It can be your workspace settings only, or you global settings. Basically it informs VSCode where to locate for QGIS packages.

Also, I have made a tutorial video showing how to set these variables from the console, without changing settings.json https://youtu.be/qobogVuXtJU

{
    // Replicate the QGIS environment
    "terminal.integrated.env.windows": {
        "OSGEO4W_ROOT": "C:\\OSGeo4W", 
        "PATH":"C:\\OSGeo4W\\apps\\qt5\\bin;C:\\OSGeo4W\\apps\\Python39\\Scripts;C:\\OSGeo4W\\apps\\qgis-ltr\\bin;C:\\OSGeo4W\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\system32\\WBem",
        "GDAL_DATA": "C:\\OSGeo4W\\share\\gdal", 
        "GDAL_DRIVER_PATH": "C:\\OSGeo4W\\bin\\gdalplugins", 
        "GS_LIB": "C:\\OSGeo4W\\apps\\gs\\lib", 
        "JPEGMEM": "1000000", 
        "OPENSSL_ENGINES": "C:\\OSGeo4W\\lib\\engines-1_1", 
        "SSL_CERT_FILE": "C:\\OSGeo4W\\bin\\curl-ca-bundle.crt", 
        "SSL_CERT_DIR": "C:\\OSGeo4W\\apps\\openssl\\certs", 
        "PROJ_LIB": "C:\\OSGeo4W\\share\\proj", 
        "QT_PLUGIN_PATH": "C:\\OSGeo4W\\apps\\Qt5\\plugins",
    "O4W_QT_PREFIX": "C:/OSGeo4W/apps/Qt5", 
    "O4W_QT_BINARIES": "C:/OSGeo4W/apps/Qt5/bin", 
    "O4W_QT_PLUGINS": "C:/OSGeo4W/apps/Qt5/plugins", 
    "O4W_QT_LIBRARIES": "C:/OSGeo4W/apps/Qt5/lib", 
    "O4W_QT_TRANSLATIONS": "C:/OSGeo4W/apps/Qt5/translations",
    "O4W_QT_HEADERS": "C:/OSGeo4W/apps/Qt5/include", 
    "O4W_QT_DOC": "C:/OSGeo4W/apps/Qt5/doc", 

    "PYTHONHOME": "C:\\OSGeo4W\\apps\\Python39", 
    "PYTHONPATH": "C:\\OSGeo4W\\apps\\qgis-ltr\\python;C:\\OSGeo4W\\apps\\qgis-ltr\\python\\plugins",
    "PYTHONUTF8": "1",

},
"python.autoComplete.extraPaths": ["C:\\OSGeo4W\\apps\\qgis-ltr\\python","C:\\OSGeo4W\\apps\\qgis-ltr\\python\\plugins"],
"python.pythonPath": "c:\\OSGeo4W\\bin\\python.exe",
"python.analysis.extraPaths": [
    "C:\\OSGeo4W\\apps\\qgis-ltr\\python",
    "C:\\OSGeo4W\\apps\\qgis-ltr\\python\\plugins"
]

}

1

Since it's 2022 now and some things that were mentioned here have changed, I decided to create a repository with a test example here:

https://github.com/MarByteBeep/pyqgis-standalone

The full explanation can be found here:

Looking for manual on how to properly setup standalone PyQGIS without GUI

Mar Tijn
  • 503
  • 3
  • 10
0

Try this batch file for an Osgeo4w QGIS install.

https://github.com/frankbroniewski/visual-studio-code-qgis

cndnflyr
  • 3,392
  • 2
  • 25
  • 43
0

For windows the simplest method is:

First copy all the paths using OSGeo4W shell. In this shell type:

py3_env

This will spit out all the paths. Copy these paths and paste inside environment variables. In windows search type env and inside environment variables click on path, edit and paste all the paths there.

The python command will start working within

rs_punia
  • 101
  • 2