Bug report #18159
Bug on macros(?!)
| Status: | Closed | ||
|---|---|---|---|
| Priority: | Normal | ||
| Assignee: | - | ||
| Category: | PyQGIS Console | ||
| Affected QGIS version: | 2.18.16 | Regression?: | No |
| Operating System: | Ubuntu 16.04 | Easy fix?: | No |
| Pull Request or Patch supplied: | No | Resolution: | fixed/implemented |
| Crashes QGIS or corrupts data: | No | Copied to github as #: | 26052 |
Description
Hi,
I created an OpenProject macro butI get the following error when opening the project.
Une erreur est survenue lors de l'exécution du code suivant: qgis.utils.reloadProjectMacros() traceback.print_exception() failed Version de Python : 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] Version de QGIS : 2.18.16 'Las Palmas', d473c30 Chemin vers Python :
My macro is running well on python console.
Running the following code in python console again triggers an error :
from qgis.utils import reloadProjectMacros reloadProjectMacros()
Here is the error message :
reloadProjectMacros()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 497, in reloadProjectMacros
openProjectMacro()
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 514, in openProjectMacro
mod.openProject()
File "<string>", line 4, in openProject
NameError: global name 'QgsMapLayerRegistry' is not defined
Thanks for you support
History
#1
Updated by Salvatore Larosa over 7 years ago
- Status changed from Open to Feedback
Could be your macro missing the import to QgsMapLayerRegistry class? It seems you are using it, isn't?
#2
Updated by Simon Georget over 7 years ago
Indeed I do,
Actually my macro is quite basic :
def openProject():
from qgis.gui import QgsMapCanvas;
from qgis.gui import QgsMessageBar;
registry = QgsMapLayerRegistry.instance();
canvas = iface.mapCanvas();
layer = registry.mapLayersByName( 'mylayer' )[0];
canvas.setExtent(layer.extent());
canvas.refresh();
#3
Updated by Regis Haubourg over 7 years ago
Hi,
this works here:
from qgis.core import QgsMapLayerRegistry
from qgis.gui import QgsMapCanvas
from qgis.gui import QgsMessageBar
from qgis.utils import iface
canvas = iface.mapCanvas()
registry = QgsMapLayerRegistry.instance()
layer = registry.mapLayersByName( u'tracks (offline)' )[0]
canvas.setExtent(layer.extent())
canvas.refresh()
pass
beware of having the 4 spaces indentations and not tab. I also added import for iface and QgsMapLayerRegistry.
Under linux, the stacktrace is not raised as in windows, which is pretty annoying. See #16923
#4
Updated by Regis Haubourg over 7 years ago
- Status changed from Feedback to Closed
- Resolution set to invalid
#5
Updated by Simon Georget over 7 years ago
Hi Regis,
Thanks for support but I still have the same issue, even importing iface and QgsMapLayerRegistry (and with 4 spaces indent)
#6
Updated by Simon Georget over 7 years ago
- Status changed from Closed to Reopened
#7
Updated by Giovanni Manghi over 7 years ago
- Status changed from Reopened to Feedback
- Resolution deleted (
invalid) - Subject changed from Bug on macros !? to Bug on macros(?!)
Simon Georget wrote:
Hi Regis,
Thanks for support but I still have the same issue, even importing iface and QgsMapLayerRegistry (and with 4 spaces indent)
any error?
#8
Updated by Simon Georget over 7 years ago
Thanks Giovanni. The error message is provided on the first post.
#9
Updated by Jürgen Fischer almost 7 years ago
Please test with QGIS 3.4 - QGIS 2.18 reached it's end of life.
#10
Updated by Simon Georget almost 7 years ago
Jürgen Fischer wrote:
Please test with QGIS 3.4 - QGIS 2.18 reached it's end of life.
Hey Jürgen. Thanks for coming back to me. I still have the same error with 3.4.3-Madeira - revision 2f64a3c
!
Une erreur est survenue lors de l'exécution du code suivant:
qgis.utils.reloadProjectMacros()
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3/dist-packages/qgis/utils.py", line 499, in reloadProjectMacros
openProjectMacro()
File "/usr/lib/python3/dist-packages/qgis/utils.py", line 516, in openProjectMacro
mod.openProject()
File "", line 2, in openProject
ImportError: cannot import name 'QgsMapLayerRegistry'
#11
Updated by Nyall Dawson almost 7 years ago
QgsMapLayerRegistry doesn't exist in 3.x -- you'll need to update your code for the 3.x API.
#12
Updated by Simon Georget almost 7 years ago
Nyall Dawson wrote:
QgsMapLayerRegistry doesn't exist in 3.x -- you'll need to update your code for the 3.x API.
Indeed. Works well with QgsProject instance. Thanks
Ticket can be closed !
#13
Updated by Jürgen Fischer almost 7 years ago
- Resolution set to fixed/implemented
- Status changed from Feedback to Closed