Import error : cannot import name OptionsTRANUSDialog



  • I have a strange problem : ImportError: cannot import name OptionsTRANUSDialog In the file OptionsTRANUSDialog, I can get my checked_list by this method :

    model = self.scenarios.model()
    checked_indexes = model.match(model.index(0, 0), QtCore.Qt.CheckStateRole,QtCore.Qt.Checked, -1,QtCore.Qt.MatchExactly | QtCore.Qt.MatchRecursive)
    
    for index in checked_indexes:
        item = model.itemFromIndex(index)
        item_text = item.text()
        self.checked_list.append(item_text)
    
    if self.checked_list != [] :
        dialog = LaunchTRANUSDialog(parent=self)
        dialog.show()
        result = dialog.exec_()
    

    I want to use this checked_list in another file(launch_tranus_dialog), so I put the import in this file :

    from .Options_TRANUS_dialog import OptionsTRANUSDialog
    

    And when I execute I get this error : ImportError: cannot import name OptionsTRANUSDialog

    I really don't understand this import error. I verify that my files are located in the same directory.

    Could you help me please.

    Thanks.



  • @EJWA said in Import error : cannot import name OptionsTRANUSDialog:

    I really don't understand this import error. I verify that my files are located in the same directory.

    Could you help me please.

    The line from .Options_TRANUS_dialog import OptionsTRANUSDialog looks suspicious to me: is your file containing the definition of OptionsTRANUSDialog names .Options_TRANUS_dialog .py?



  • @JohanSolo
    Hi,

    The file Options TRANUSDialog is containing the class OptionsTRANUSDialog.



  • @EJWA said in Import error : cannot import name OptionsTRANUSDialog:

    The file Options TRANUSDialog is containing the class OptionsTRANUSDialog.

    With a space? I doubt this can ever work...



  • Yes the same structure works for other imports.

    Example :

    from .scenarios_model import ScenariosModel
    from .launch_tranus_dialog import LaunchTRANUSDialog



  • Usually, the files to be imported are located in another directory, with a __init__.py script, which lists the available files. Then, you can easily do from ModuleDirectory import src_file, where ModudeDirectory contains the file src_file.py file.

    I tried to quickly look at the import documentation, and found nothing about spaces (i.e. neither a forbidden statement nor a special syntax where the spaces are replaces with underscores).

    If I were you, I'd remove all spaces in the file / directory names (which sounds like heresy to me, especially when coming to programming), and then I'd give another try.



  • For more information, here is the error that I get : Couldn't load plugin OptionsTRANUS due to an error when calling its classFactory() method

    ImportError: cannot import name OptionsTRANUSDialog Traceback (most recent call last): File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 333, in startPlugin plugins[packageName] = package.classFactory(iface) File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS__init__.py", line 34, in classFactory from .Options_TRANUS import OptionsTRANUS File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 607, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\Options_TRANUS.py", line 30, in from .OptionsTRANUS_project import OptionsTRANUSProject File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 607, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\OptionsTRANUS_project.py", line 24, in from .Options_TRANUS_dialog import OptionsTRANUSDialog File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 607, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\Options_TRANUS_dialog.py", line 29, in from .launch_tranus_dialog import LaunchTRANUSDialog File "C:/OSGEO4~1/apps/qgis/./python\qgis\utils.py", line 607, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/Users/emna/.qgis2/python/plugins\OptionsTRANUS\launch_tranus_dialog.py", line 7, in from .Options_TRANUS_dialog import OptionsTRANUSDialog ImportError: cannot import name OptionsTRANUSDialog

    Python version: 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] QGIS version: 2.18.3 Las Palmas, 77b8c3d

    Python Path: C:/OSGEO4~1/apps/qgis/./python C:/Users/emna/.qgis2/python C:/Users/emna/.qgis2/python/plugins C:/OSGEO4~1/apps/qgis/./python/plugins C:\OSGEO4~1\apps\Python27\lib\site-packages\setuptools-0.9.8-py2.7.egg C:\OSGEO4~1\bin\python27.zip C:\OSGEO4~1\apps\Python27\DLLs C:\OSGEO4~1\apps\Python27\lib C:\OSGEO4~1\apps\Python27\lib\plat-win C:\OSGEO4~1\apps\Python27\lib\lib-tk C:\OSGEO4~1\bin C:\OSGEO4~1\apps\Python27 C:\OSGEO4~1\apps\Python27\lib\site-packages C:\OSGEO4~1\apps\Python27\lib\site-packages\jinja2-2.7.2-py2.7.egg C:\OSGEO4~1\apps\Python27\lib\site-packages\markupsafe-0.23-py2.7-win-amd64.egg C:\OSGEO4~1\apps\Python27\lib\site-packages\pytz-2012j-py2.7.egg C:/Users/emna/.qgis2//python


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.