最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin23浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论