0

I have tried to setup my PyQGIS development environment to use PyCharm, by opening my script in Pycharm, then change the python interpreter to the path C:/ProgramFiles/QGIS 3.28.3/bin/python-qgis.bat (so according to this Using PyQGIS in PyCharm). However, while it now recognizes the qgis library and seems to work properly, it doesn't update the view or the TOC in the open instance of QGIS.

For example this code does print the source layer, but no layer is added to the TOC of the open QGIS instance, as it would when scripting in the python code editor opened in QGIS:

from qgis.core import *
import os
import sys

# Get the data 

source = QgsVectorLayer(path='path/to/file.shp',
                        baseName = 'source',
                        providerLib='ogr')

if not source.isValid():
    print("Layer failed to load!")
else:
    QgsProject.instance().addMapLayer(source)

print(source)

Do I need to refresh QGIS? And if so, how can I do this from the script, so that it updates the view automatically?

QGIS 3.28
Windows 10

4
  • Oh, yes, I just missed to copy-paste fully my script. Have added the part where I add the layer to the canvas (see above). Still, this doesn't change it.
    – i.i.k.
    Apr 25, 2023 at 10:58
  • Further, I just noticed, that when I run the script in the QGIS editor, it recognices a xyz-file added to the QGIS project, however, this is not the case, when I run it with PyCharm. So it seems, that the python interpreter doesn't have acces to the QGIS project.
    – i.i.k.
    Apr 25, 2023 at 11:01
  • 3
    This won't work. It's not possible to load from PyCharm a layer in your current QGIS opened dialog. You can edit your files from PyCharm, but then, you need to execute your Python file from QGIS.
    – etrimaille
    Apr 25, 2023 at 11:06
  • Thank you @etrimaille for that clear answer! Can you elaborate a bit on this? Then you could set it as answer to this question.
    – i.i.k.
    Apr 25, 2023 at 12:26

1 Answer 1

2

It's not possible to load from your PyCharm, a layer in your current QGIS opened dialog.

However, you can edit your files from PyCharm, but then, you can execute it in QGIS.

Assuming you are using PyCharm to write a Python script (not a plugin, not a Processing script, not a QGIS expression etc), you can write a quick command :

enter image description here

Then, execute it with the green triangle, it will generate this line for you :

>>> exec(Path('/tmp/tmpllb063tj.py').read_text())
Hello

Then, you can replace the command by adapting the path /tmp/tmpllb063tj.py with your own file you have in PyCharm.

Finally, you can press "arrow up" and "enter" to always relaunch your Python script.

4
  • @ etrimaille I'm sorry, I think this was a misunderstanding, I know how to execute the pyqgis script in Pycharm. What I mean was, if you can elaborate why it is not possible to update the QGIS view when executing the scipt with Pycharm, even though it uses the same interpreter? It seems that the QGIS editor recognizes the active project instance, while this does not work, when using Pycharm. Something like this.
    – i.i.k.
    Apr 25, 2023 at 13:33
  • 1
    Your PyCharm is only configured to use the QGIS library installed on your computer. Using the library doesn't mean to use the QGIS application launched. For instance, you can launch your PyCharm without your QGIS desktop opened. When you launch the script provided above, don't you have a warning Application path not initialized ? As linked your question, you don't have any QgsApplication to make a standalone script. Maybe have a look to "remote debug tool" if it helps ?
    – etrimaille
    Apr 25, 2023 at 16:32
  • @etrimaillle Mh, I don't get the 'Aplication path not initialized'. Instead, in Pycharm, nothing happens, when I execute the script except for that it says it excuted and prints it.On the first glance it seems to work properly. is there no way to connect the desktop App with Pycharm?
    – i.i.k.
    Apr 26, 2023 at 6:44
  • I will open a new question for it.
    – i.i.k.
    Apr 26, 2023 at 9:40

Your Answer

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

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