Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. NameError: name 'Qt' is not defined
QtWS25 Last Chance

NameError: name 'Qt' is not defined

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 6 Posters 9.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_hobbyist
    wrote on last edited by john_hobbyist
    #1

    Hello I am searching for circular imports in my code and I try to avoid them and debug the code but I face this error:

    File "code.py", line 535, in __init__
       sharedLayout.setAlignment(Qt.AlignTop | Qt.AlignLeft)
    NameError: name 'Qt' is not defined
    

    I have imported PyQt5...What is this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      If you did just:

      import PyQt5
      

      Then it won't be able to find the Qt namespace because you only made the PyQt5 namespace available.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        john_hobbyist
        wrote on last edited by
        #3

        Yes but if I write

        from...import....
        

        I get circular imports! What should I do?

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Use the fully qualified name.

          How do you create circular imports by using PyQt5 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • J Offline
            J Offline
            john_hobbyist
            wrote on last edited by
            #5

            This is what I am trying to find out. What is the command of what you are writing?

            1 Reply Last reply
            0
            • J john_hobbyist

              Yes but if I write

              from...import....
              

              I get circular imports! What should I do?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @john_hobbyist
              Which circular imports where?

              You can do this from PyQt5:

              from PyQt5.QtCore import Qt
              

              and then your original code should work.

              In fairness, it is a bit complicated. https://stackoverflow.com/questions/39316693/how-to-import-from-qt-namespase-qt5-python3-x, the solution there, describes the situation and why this works, if you care.

              1 Reply Last reply
              2
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7
                PyQt5.QtCore.Qt.Whatever
                

                Or more simply:

                from PyQt5.QtCore import Qt
                

                and then use Qt.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • J Offline
                  J Offline
                  john_hobbyist
                  wrote on last edited by john_hobbyist
                  #8

                  Amazing! It works!!! Thanks a lot for your time!

                  Pablo J. RoginaP 1 Reply Last reply
                  0
                  • J john_hobbyist

                    Amazing! It works!!! Thanks a lot for your time!

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by
                    #9

                    @john_hobbyist said in NameError: name 'Qt' is not defined:

                    It works!!!

                    so please don't forget to mark your post as solved!

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      sneha_chavan_07
                      wrote on last edited by
                      #10

                      Detail ModuleNotFoundError: No module named 'PyQt5' im still facing this issue

                      jsulmJ 1 Reply Last reply
                      0
                      • S sneha_chavan_07

                        Detail ModuleNotFoundError: No module named 'PyQt5' im still facing this issue

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @sneha_chavan_07 Then please provide more information. Did you install PyQt5 and how? On which OS?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved