[Qgis-user] from xxx import zzz

Johannes Kröger (WhereGroup) johannes.kroeger at wheregroup.com
Mon Mar 28 09:45:09 PDT 2022


Hey Stephen,

it's not a dumb question at all! The QGIS Python console+editor's 
autocompletion is pretty misleading as it will suggest pretty much 
anything it can find anywhere in the code base, with no regards if the 
object or class you are currently working with actually has that 
"thing". At least that how it seems to me. :}

In my experience it's best to ignore it for programming purposes and 
only treat it as way to save typing some characters.


As for your errors, "xxx is not an attribute of yyy" means that there is 
no yyy.xxx. This is not something you can import. yyy is an object and 
will be checked live at runtime when Python tries to access its xxx 
attribute (yyy.xxx). If .xxx does not exist for yyy, then that's the way 
it is, you are on the wrong path :)

You can find out which attributes and methods your objects have by using 
dir(yyy) or via the API documentation via help(yyy) or online at 
https://qgis.org/pyqgis/master/index.html (make sure to choose the 
correct version corresponsing to your QGIS).

Cheers, Hannes

Am 28.03.22 um 18:33 schrieb Stephen Sacks via Qgis-user:
> Please pardon what may be a dumb question from a self- (half-) taught 
> student of Python.  I often steal fragments of code from the internet 
> (especially including this forum) and sometimes get an error message 
> like  "xxx is not an attribute of yyy" .  My  problem, I think, is a 
> missing  " from xyz import abc".  My question to you now is whether 
> there is some way to know what to import, and from which module, for 
> any particular Python statement.  More specifically, I get this error 
> message:
>
>   AttributeError: 'QgsEditFormConfig' object has no attribute 'setHidden'
>
> when I run the following code:
>
> from qgis.core import QgsProject
> from qgis.core import *
> from qgis.utils import iface
> from qgis.core import *
> from PyQt5.QtWidgets import QInputDialog
> from PyQt5.QtWidgets import *
> from qgis.PyQt.QtCore import *
> from qgis.gui import *
>
> def oneA( lyr, columnName, option ):
>     flds= lyr.fields()
>     indx = flds.indexOf(columnName)
>     config = lyr.editFormConfig()
>     config.setHidden(indx, option)
>     lyr.setEditFormConfig(config)
>
> lyr = iface.activeLayer()
> aField =  'NAME'
> oneA (lyr, aField, True)
>
> When I type this code in the QGIS environment, autocompletion leads me 
> to believe that "setHidden" is allowed after "config." but I must 
> admit I don't understand the meaning of the several choices specified 
> in the parentheses.  I'd be grateful if someone would explain that to 
> me.  Also,  am I correct in thinking that an asterisk following 
> "import" means "everything in this module"?
>    I'm running QGIS  3.16.3  under Windows 10.
>    Thanks in advance for any help.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220328/4189ad87/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_0xBF7B268A77C202D5.asc
Type: application/pgp-keys
Size: 2476 bytes
Desc: OpenPGP public key
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220328/4189ad87/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 665 bytes
Desc: OpenPGP digital signature
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20220328/4189ad87/attachment.sig>


More information about the Qgis-user mailing list