Python Forum
NameError: name 'processing' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'processing' is not defined
#1
Hello python experts
I use to run this piece of code in QGIS, but it has been awhile (6 months)
It has been working perfect before but now I get this error that I can not solve.

"processing" is not defined, is the error message

Is this related to this code or I have to look into QGIS groups for an answer?

Thank you
Regards

from datetime import datetime

trolls = '/Users/username/Desktop/test.gpkg|layername=test'

rules_per_code = {
    'FA_058': [("Ordertype = 2 AND PLD_1 = 1", 53),
               ("Ordertype = 5 AND PLD_1 = 2", 78)],
    'FA_030': [("Ordertype = 2 AND PLD_1 = 1", 32),
               ("Ordertype = 5 AND PLD_1 = 2", 54)]
    }

results = {}
my_timestamp = datetime.strftime(datetime.now(),'%Y%m%d_%H%M%S')

for code, rules in rules_per_code.items():
    print('Processing {} ...'.format(code))
    results[code] = []
    for rule in rules:
        expression = rule[0]
        percentage = rule[1]
        extracted = processing.run("native:extractbyexpression",
            {'INPUT':trolls,'EXPRESSION':expression,'OUTPUT':'memory:'}
            )['OUTPUT']
        random_sample = processing.run("qgis:randomextract",
            {'INPUT':extracted,'METHOD':1,'NUMBER':percentage,'OUTPUT':'memory:'}
            )['OUTPUT']
        results[code].append(random_sample)

for code, results in results.items():
    merged = processing.run("native:mergevectorlayers", 
        {'LAYERS':results,'CRS':None,
        'OUTPUT':'/Users/username/Desktop/{}_{}.gpkg'.format(code,my_timestamp)}
        )['OUTPUT']
    iface.addVectorLayer(merged, '', 'ogr')  
Reply
#2
Indeed, on line 21, processing is referred to, but not defined anywhere prior to that. Where should it come from? It it a module that you import, or should it be a variable you create? I can't say I know anything about GIS, so can't help you there.

You should get into the habit of keeping track of changes to your source code by using some kind of version control system (e.g. Git). That way, you'd at least have a log of what you changed, when and why and could go back to an earlier version if needed. It's also useful if you want to work with others on the same codebase.
Reply
#3
OK thanks,
Python is part of QGIS and it calls process-modules like "native:extractbyexpression".
I have not build this myself, something must have been changed in later versions...

Might see if I can go back to an earlier version and see if that helps or see if someone in QGIS groups can give me a hint.

Regards
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 257 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,090 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,872 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,301 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,502 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,911 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,331 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,935 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,356 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,872 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020