I have written a python script in the QGIS editor which works as expected when I launch it from there.
import processing
import os
from PyQt5.QtWidgets import QFileDialog
from qgis.core import QgsVectorLayer, QgsProject, QgsCoordinateReferenceSystem
from qgis.utils import iface
root = QgsProject.instance().layerTreeRoot()
#Insert DummyLayer and set it to active so the layers from the
#model import don't have to be moved in the layer tree
DummyLayer = QgsVectorLayer("Point?crs=EPSG:4326", "DummyLayer", "memory")
QgsProject.instance().addMapLayer(DummyLayer, False)
node_DummyLayer = root.insertLayer(0, DummyLayer)
iface.setActiveLayer(DummyLayer)
# Add group for csv-file import to project
root = QgsProject.instance().layerTreeRoot()
QSgroup = root.insertGroup(1,"QS")
# User Dialog to pick Import_QGIS folder
Folder_Path = QFileDialog.getExistingDirectory(caption="Choose folder 'Import_QGIS'.")
# Folder_Path for uri
Folder_Path_uri = "file:///"+ Folder_Path + "/"
# Make a tuple with the csv-filenames, first 3 letters are numbers
i=0
my_tuple=[]
for file in os.listdir(Folder_Path):
First3Letters = file[0:2]
try:
int(First3Letters) < 300
#print(file)
my_tuple.append(file)
i+=1
except:
continue
iNum = int(i)
#Importing the csv-files
for x in range(iNum):
try:
uri = Folder_Path_uri + my_tuple[x] + "?type=csv&delimiter=,&detectTypes=yes&xField=X_Pos&yField=Y_Pos&crs=epsg:25832&spatialIndex=no&subsetIndex=no&watchFile=no"
mem_layer = QgsVectorLayer(uri, my_tuple[x][:-4], 'delimitedtext')
QgsProject.instance().addMapLayer(mem_layer, False) # needs to be False to avoid duplicate layers in group and outside the group
# add layer to group
QSgroup.addLayer(mem_layer)
mem_layer.loadNamedStyle(r"C:\Users\mystyle.qml")
# Show Feature Count
root = QgsProject.instance().layerTreeRoot()
myLayerNode = root.findLayer(mem_layer.id())
myLayerNode.setCustomProperty("showFeatureCount", True)
mem_layer.triggerRepaint()
x+=1
except:
break
# Getting data from the model
# User Dialog to pick folder where the 3 files Form* are stored
from processing.modeler.ModelerDialog import ModelerDialog
Folder_Path = QFileDialog.getExistingDirectory(caption="Choose folder 'QS'.")
sForm1 = Folder_Path + "/Form1.xlsx"
sForm2 = Folder_Path + "/Form2.xlsx"
sForm3 = Folder_Path + "/Form3.xlsx"
#Launching the model
dlg = ModelerDialog()
dlg.loadModel('C:/Users/myModel.model3')
processing.runAndLoadResults(dlg.model, {'form1':sForm1,'form2': sForm2,'form3':sForm3,'CRS':QgsCoordinateReferenceSystem('EPSG:25832'),'native:mergevectorlayers_4:Border 1':'memory:','native:mergevectorlayers_6:Border 2':'memory:',
'native:mergevectorlayers_7:Plots':'memory:','qgis:createpointslayerfromtable_1:Trees':'memory:'})
#Remove DummyLayer
root.removeLayer(DummyLayer)
I thought the script was ready to be added to the tool box. I saved it on my hard drive. I clicked the scripts icon and "add script to tool box". Doing so, the script is immediately executed. If I go along and fill in the user dialogs with valid entries, the script gets correctly executed, but is not added to the tool box. When I restart my computer and launch QGIS, it also auto-executes the script.
Why is QGIS running the script and not adding it to the script tool box?
EDIT: I have reinstalled QGIS 3.4 and the problem has not changed. I have tried to add a different script with just a few lines of code, but its not added to the tool box either. The script symbol is not showing up either. Tool Box
EDIT: I went to a colleague and tried to add the script to the processing tool box and the result was the same: QGIS is running the script (maybe to check the code?) and in my case launching the 2 user dialogs. However, the script does not get added to the tool box.
4 Answers 4
QGIS processing scripts have a specific structure/template.
For QGIS 3.2/3.4/3.6, please review "Processing script template for QGIS3".
For QGIS 3.6+, please review "Easy Processing scripts comeback in QGIS 3.6".
Why is QGIS not adding it to the script tool box?
The reason is that your code doesn't match the template.
Why is QGIS running the script?
Because QGIS runs immediately any script tried to add to the toolbox if it's a proper script or not. It means that the code lines in root scope are executed. When you review the posts, you will notice that the structure of QGIS script file doesn't have any executable lines in root scope except import
part and just has definitions of some required methods.
-
1Im having the same problem as OP. I would just like to stop the script from running on start up. Ive tried moving my script file, uninstalling qgis, but the script still runs each time i start a new qgis instance. Do you know how i can stop this script from running?MatthewJSlezak– MatthewJSlezak2020年03月17日 14:56:00 +00:00Commented Mar 17, 2020 at 14:56
QGIS can't recognize your script as processing script. See below links:
In QGIS 3.4, the only way to write Processing algorithms using Python is to extend the QgsProcessingAlgorithm class.
Source: https://docs.qgis.org/3.4/en/docs/user_manual/processing/scripts.html
There are new "decorators to make processing scripts easier" in QGIS 3.6:
https://github.com/qgis/QGIS-Enhancement-Proposals/issues/134 https://anitagraser.com/2019/03/02/easy-processing-scripts-comeback-in-qgis-3-6/
I also had the same problem, but I solved.
My script was also not added into the toolbox, but everytime I've opened my QGIS, the script was running.
To stop it you have to:
- Go to Processing Toolbox >> Options.
- In Options you have to find the path of the scripts folder (s)
- Copy file path into your File explorer to look for the scripts folder
- Erase the problematic scripts from the script folder
What happens if you empty the path of the scripts folder(s) in options?
Because I don't want QGIS to check every script over again when it starts up. If I want to use a certain script, I will open it from within the toolbox (open existing script).
-
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.2023年11月02日 16:18:12 +00:00Commented Nov 2, 2023 at 16:18