[Qgis-user] How to set PyQGIS imported in VS Code in Windows for a Python file?

LI, Xin Stephen [MIT] stephenli at eduhk.hk
Mon Mar 13 22:57:12 PDT 2023


Hi, Zoltan,

Thanks so much for your help.

It is quite helpful. After changing the last line to the directory with Code.exe, it works to open the VS code and can run the Example Python file in section 1.4.1 for the PyQGIS Cookbook (GQIS Testing).

There are tow lines of information in the terminal window as follows:
QTemporaryDir: Unable to remove "D:\\Temp\\python-lWnByt" most likely due to the presence of read-only files.
QTemporaryDir: Unable to remove "D:\\Temp\\python-KrxiSz" most likely due to the presence of read-only files.

Any suggestions about it?

Best regards,
Stephen

From: QGIS-User <qgis-user-bounces at lists.osgeo.org> On Behalf Of Zoltan via QGIS-User
Sent: 2023年3月13日 2:43 PM
To: qgis-user at lists.osgeo.org
Subject: Re: [Qgis-user] How to set PyQGIS imported in VS Code in Windows for a Python file?


CAUTION: External email. Do not click links or open attachments unless you recognise the sender and know the content is safe.
警告:外來電郵。 如不認識寄件者,或不確定內容是否安全,切勿按下任何連結或開啟任何附件。
Hi Li
I am responding because I had a similar experience using PyCharm so below might be useful things for you to check whilst you are waiting for a more informed response:

Firstly Google using QGIS is VS - there will be a lot of environment variables that will need to be set up.

I start PyCharm using the following .bat job:

HTH,
Zoltan
REM https://haifengniu.com/post/how-to-set-environment-for-pyqgis-3-in-pycharm/<https://protect2.fireeye.com/v1/url?k=31323334-501cfaeb-3130cb80-454455535732-7c8ae188f1d10b08&q=1&e=48d220b7-ed97-45a6-af60-1c568fb25b63&u=https%3A%2F%2Fhaifengniu.com%2Fpost%2Fhow-to-set-environment-for-pyqgis-3-in-pycharm%2F>

REM Setting PyCharm IDE
REM Just double-click pycharm.cmd to start PyCharm.
REM In the settings of PyCharm project, set interpreter as %OSGEO4W_ROOT%\bin\python3.exe
REM In my computer, the full path of interpreter is D:\OSGeo4W64\bin\python3.exe.


@echo off
    SET OSGEO4W_ROOT=C:\OSGeo4W
    call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
REM call "%OSGEO4W_ROOT%"\bin\qt5_env.bat
REM call "%OSGEO4W_ROOT%"\bin\py3_env.bat
    call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
    @echo off
    path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
    path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass78\lib
    path %PATH%;D:\OSGeo4W64\apps\Qt5\bin
    path %PATH%;D:\OSGeo4W64\apps\Python39\Scripts

    set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
    set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python39
    set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

start "Pycharm Aware of OSGeo4W" /B "C:\\Program Files\\JetBrains\\PyCharm Community Edition 2021.2.2\\bin\\pycharm64.exe"


On 2023/03/13 09:30, LI, Xin Stephen [MIT] via QGIS-User wrote:
The OS is Windows 10,
The QGIS version is 3.28 LTR
The Python version is 3.11

The steps following the steps in stection 1.4.1 for the PyQGIS Cookbook (GQIS Testing), i.e. https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html#python-applications
The “PAHT” and “PYTHONPYTHON” have been added.

The problem is:
When  running the Python program file with “import qgis.core”, there is the error
“ModuleNotFoundError: No module named 'qgis' ”

Moreover, after reinstalling the Python 3.9.5 as the QGIS related, then run the Python file in VS Code by Python 3.9.5, the Error is the same.

Any suggestions could help modify this setting?

Thanks.



_______________________________________________

QGIS-User mailing list

QGIS-User at lists.osgeo.org<mailto:QGIS-User at lists.osgeo.org>

List info: https://lists.osgeo.org/mailman/listinfo/qgis-user

Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user



--



=============================================

Zoltan Szecsei GPrGISc 0031

Director, Geograph (Pty) Ltd.

GIS and Photogrammetric Services



Cape Town, South Africa.



Mobile: +974-5058-8729 (Signal, not WhatsApp)

        +27-83-6004028 (Signal, not WhatsApp)



www.geograph.co.za<https://protect2.fireeye.com/v1/url?k=31323334-501cfaeb-3130cb80-454455535732-fb1c67394740518e&q=1&e=48d220b7-ed97-45a6-af60-1c568fb25b63&u=http%3A%2F%2Fwww.geograph.co.za%2F>

=============================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230314/3c69d1c2/attachment.htm>


More information about the QGIS-User mailing list