2

I try to setup QGIS 3 with 2 bat file like that

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass76\etc\env.bat

path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass76\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%

cmd.exe

and

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass76\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass76\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 "VisualCode QGIS" /B "C:\Microsoft VS Code\Code.exe" %*

but when I try to import qgis.core by standalone VS code I got error like that

Traceback (most recent call last):
  File "c:\Users\KTLab\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\ptvsd_launcher.py", line 21, in <module>
    import ptvsd
  File "c:\Users\KTLab\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\__init__.py", line 13, in <module>
    from ._vendored import force_pydevd
  File "c:\Users\KTLab\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\_vendored\force_pydevd.py", line 18, in <module>
    pydevd_constants = import_module('_pydevd_bundle.pydevd_constants')
  File "C:\OSGEO4~1\apps\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "c:\Users\KTLab\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_constants.py", line 180, in <module>
    from _pydev_imps._pydev_saved_modules import thread
  File "c:\Users\KTLab\.vscode\extensions\ms-python.python-2019.6.24221\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_imps\_pydev_saved_modules.py", line 8, in <module>
    import socket
  File "C:\OSGEO4~1\apps\Python37\lib\socket.py", line 49, in <module>
    import _socket
ImportError: DLL load failed: %1 is not a valid Win32 application.

Internal error detected. Please copy the above traceback and report at
https://github.com/Microsoft/vscode-python/issues/new

Could you please explain it for me?

  • I would suggest the free Visual Studio Community where you could attach the Python debugger to the running QGIS process using PTVS. And no headaches during setup :-) – christoph Jul 30 '19 at 16:02
  • Thanks for you information, but could you please explain how to do it because I am is very new to Python. Thank you – Cá Khô Jul 31 '19 at 1:00
1

Here is a short step by step guide to install and configure VS-Community:

1) Download and run the installer from https://visualstudio.microsoft.com/de/vs/community/

2) Select Workloads > "Python development" and click Install

3) Set up a custom Python Environment:

Description:
QGIS3 

Prefix path:
C:\OSGeo4W64\bin

Interpreter path:
C:\OSGeo4W64\bin\python3.exe

Windowed interpreter:
C:\OSGeo4W64\bin\pythonw3.exe

Language version:
3.7

Architecture:
64-bit

Path environment variable:
PYTHONPATH

To open a Python file via Explorer right click, you have to run regedit.exe and change the faulty Application entry "VisualStudio.15.0" to "VisualStudio.16.0":

[HKEY_CLASSES_ROOT\VisualStudio.py.0cea6d2b\shell\Open\ddeexec\Application]
@="VisualStudio.16.0"

When you start debugging, you have to set your breakpoints and attach to the running qgis.exe process.

That's it ...no config file hassle ;-)

  • Thanks for you help! – Cá Khô Aug 1 '19 at 1:57
  • Let me add a small supplement to my answer, because it took me several days to find out why i couldn't debug a python file that was executed from the QGIS 3 Python Console. – christoph Nov 5 '19 at 10:24
  • 1
    QGIS 3 uses "exec(open ..." to execute a python script, preventing Visual Studio to debug the code. Changing the line with "exec(open" in console_editor.py to >>> self.parent.pc.shell.runCommand("with open('{0}'.encode('{1}')) as f: code=compile(f.read(),'{2}','exec'); exec(code);\r".format(filename.replace("\\", "/"), sys.getfilesystemencoding(),os.path.basename(filename))) << enables debugging again. – christoph Nov 5 '19 at 10:33
  • BTW the file, where you have to change the lines, is here: C:\OSGeo4W64\apps\qgis\python\console\console_editor.py I have a ticket open to have this set by default, but unfortunately nobody cares :-( – christoph Apr 7 '20 at 9:30

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.