3

Based on the answer to Referencing Qt UIs external to plugin with python. I'm trying to utilize the "Layer Properties / Fields / Python Init function" to execute a file (test_file.py) that is located in the folder of the plugin that I'm making.

I've made sure that the path to my plugin folder is in PYTHONPATH via "Settings / Options... / System / Environment" ... and restarted QGIS.

When the layer, for which I'm utilizing Init function, is initialized and a feature is selected test.py is executed. However, I get three consecutive error messages saying:

1) An error occured during execution of following code:
      import test_file
      Traceback (most recent call last):
        File "", line 1, in 
        File "C:/PROGRA~1/QGISVA~1/apps/qgis/./python\qgis\utils.py", line 453, in _import
          mod = _builtin_import(name, globals, locals, fromlist, level)
      ImportError: No module named test_file

2) An error occured during execution of following code:
      if hasattr(test_file,'DEBUGMODE') and test_file.DEBUGMODE:    reload(test_file)
      Traceback (most recent call last):
        File "", line 1, in 
      NameError: name 'test_file' is not defined


3) An error occured during execution of following code:
      _qgis_feature_form_20140526104937007 = test_file.py(_qgis_featureform_2, _qgis_layer_workgroups20140526104723778, _qgis_feature_20140526104937007)
      Traceback (most recent call last):
        File "", line 1, in 
      NameError: name 'test_file' is not defined

Any suggestions how to fix this?

1

There are basically two possibilities to get this working

Standalone python file

The file has to be in the same folder as your .qgs project file and its name has to be specified as the module name.

E.g. you have a folder /home/user/project with the following content:

  • project.qgs
  • formFile.py with the function def formOpen defined toplevel

In your project specify formFile.formOpen as init function

As part of a plugin

If your plugin is called myPlugin (and hence its root folder is called myPlugin) you can put the file formFile.py into its root folder and set the init function to myPlugin.formFile.formOpen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.