I want to change the data source of a layer (non geometric, just a table of fields) created from a .txt file. I know how to do it manually but I want to do it with the Python console. I have tried the setDataSource() command but I don't quite understand the arguments.

from qgis.core import QgsDataSourceUri

uri = QgsDataSourceUri()

This is the code I tried to use to change the source file, it didn't get me any error but it didn't change the source file either.

1 Answer 1


You need to call the setDataSource() function from the actual layer, not from the class itself:

layer = iface.activeLayer()

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.