Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Reread Setting.py while the program is running.

    Scheduled Pinned Locked Moved WiPy 2&3
    wipylopyimport
    8 Posts 2 Posters 525 Views 2 Watching
    Loading More Posts
    • 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.
    • W Offline
      Wolli01
      last edited by

      Reread Setting.py while the program is running.
      How can I reload a setting file which I read in by Import Setting at the beginning of the program without leaving the main.py or starting a reset?

      Problem: I have a setting file that I can update online. However, the new data is not taken over in the variables, but only when I reboot.
      How can I do it without restarting.
      I am using a While True loop in the main.py.

      Translated with www.DeepL.com/Translator

      robert-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @Wolli01
        last edited by robert-hh

        @Wolli01 I use this little function:

        #
        # reload module: 
        #
        def reload(mod):
            import sys
            mod_name = mod.__name__
            del sys.modules[mod_name]
            return __import__(mod_name)
        
        
        

        You would call it with:
        reload(Setting)

        W 2 Replies Last reply Reply Quote 0
        • W Offline
          Wolli01 @robert-hh
          last edited by

          @robert-hh Thank You

          1 Reply Last reply Reply Quote 0
          • W Offline
            Wolli01 @robert-hh
            last edited by

            @robert-hh Wie rufe ich das auf?

            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @Wolli01
              last edited by robert-hh

              @Wolli01
              Preload:
              from reload import *
              then:
              reload(Setting)

              Instead of this function, you can also simply use that sequence directly:

              import sys
              del sys.modules['Setting']
              import Setting
              

              You have to take care, in which namespace you are calling it.

              W 1 Reply Last reply Reply Quote 0
              • W Offline
                Wolli01 @robert-hh
                last edited by

                @robert-hh
                import sys
                del sys.modules('settings')
                time.sleep(1)
                import settings

                File "main.py", line 290, in <module>
                SyntaxError: can't delete expression

                robert-hhR 1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @Wolli01
                  last edited by

                  @Wolli01 Yes. Obviously it is:

                  del sys.modules['settings']

                  since sys.modules is a dictionary. This del does not delete the old module, but only allows to reload it.

                  W 1 Reply Last reply Reply Quote 0
                  • W Offline
                    Wolli01 @robert-hh
                    last edited by

                    @robert-hh
                    That's how it works perfectly!
                    Thank you very much.

                                import sys
                                del sys.modules['settings']
                                time.sleep(1)
                                import settings
                    
                    1 Reply Last reply Reply Quote 0

                    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                    With your input, this post could be even better 💗

                    Register Login
                    • 1 / 1
                    • First post
                      Last post

                    Pycom on Twitter

                    Pycom