python - How to re-use a custom function based on Qgis processing tools in another script? - Stack Overflow

I'm working in QGIS with python script.I created a script that use processing tools from QGIS usi

I'm working in QGIS with python script. I created a script that use processing tools from QGIS using the processing.run(...) function. Everything worked fine. Then, I embedded my code in a function to re-use it in another script. So, I modified the code to make it a function (called my_func) and I saved this function in a file (my_function.py). Then, I import it in my new script (import my_function as mf) in a new script (saved in the same folder). I call my function (a = mf.my_func(...)) but I always have this error message:

NameError: name 'processing' is not defined

I tried to import 'procesing' in both of my script but I always have the same problem. What I find strange is that importing my script doesn't seem to make a problem, and I didn't have this problem when I tested the function as a script before.

Does anyone have some idea to solve this issue ?

Thanks!

I'm working in QGIS with python script. I created a script that use processing tools from QGIS using the processing.run(...) function. Everything worked fine. Then, I embedded my code in a function to re-use it in another script. So, I modified the code to make it a function (called my_func) and I saved this function in a file (my_function.py). Then, I import it in my new script (import my_function as mf) in a new script (saved in the same folder). I call my function (a = mf.my_func(...)) but I always have this error message:

NameError: name 'processing' is not defined

I tried to import 'procesing' in both of my script but I always have the same problem. What I find strange is that importing my script doesn't seem to make a problem, and I didn't have this problem when I tested the function as a script before.

Does anyone have some idea to solve this issue ?

Thanks!

Share Improve this question asked Nov 19, 2024 at 16:10 YvanouYvanou 1051 gold badge3 silver badges7 bronze badges 1
  • OK, I finally found a workaround. I created a processing tool based on my function and I used it in the other script. – Yvanou Commented Dec 12, 2024 at 8:49
Add a comment  | 

2 Answers 2

Reset to default 0

Hard to tell without seeing your code. It seems you execute your function inside the QGIS python console but if not you have to add the processing path to your python path. Such as : "/path/to/qgis/processing/share/qgis/python/plugins"

Kanash

OK, I finally found a workaround. I created a processing tool based on my function and I used it in the other script.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742414953a4439560.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信