Bug report #11396

NameError: global name ‘QgsMapLayerRegistry’ is not defined

Added by CANO Kathy about 10 years ago. Updated about 10 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:-
Affected QGIS version:2.4.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:19679

Description

Hello, I tried to follow this article :

http://anitagraser.com/2014/04/26/getting-started-writing-qgis-2-x-plugins/

and met a problem when I “reloaded” my plugin “plop” in Qgis :

“Une erreur est apparue lors de l’exécution du code Python :

Traceback (most recent call last):
File “C:/Users/Utilisateur/.qgis2/python/plugins\\plop\\plop.py”, line 186, in run
layers = QgsMapLayerRegistry.instance().mapLayers().values()
NameError: global name ‘QgsMapLayerRegistry’ is not defined

Version de Python :
2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]

Version de QGIS :
2.4.0-Chugiak Chugiak, 8fdd08a”

I tried to declare QgsMapLayerRegistry at the beggining of “plop.py” withy from PyQt4 import QgsMapLayerRegistry, but it’s say “cannot import name QgsMapLayerRegistry”

What is the problem ?

Thank you !!!

History

#1 Updated by gillian milani about 10 years ago

You need to :

from qgis.core import QgsMapLayerRegistry

In breif, when your object start with Qgs*, it comes from the qgis package, not from the PyQt4.

#2 Updated by Giovanni Manghi about 10 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

please post questions in the users/developers mailing lists, cheers!

Also available in: Atom PDF