4

Is it possible to integrate an external Python script into a QGIS project macro?

I have a relatively large macro that executes various Python commands at the start and end of the project. For better management and overview, I would like to store these commands externally in separate Python files.

How can I achieve this in QGIS? Are there specific methods or best practices for calling external scripts within a project macro?

1 Answer 1

3

You can run external .py script with a command like this:

exec(open('filename.py').read())

But I'm not sure if this is a good practice, since the location of the file may change.

In addition, it could lead to the execution of potentially dangerous code if someone modifies the file.

1
  • I solved the location problem by combining the project dir with the relative dir using: project_dir = os.path.dirname(QgsProject.instance().fileName()) os.chdir(project_dir)
    – MartinMap
    Commented May 24 at 8:50

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.