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

    Make application location-independent

    Scheduled Pinned Locked Moved Discussion
    import
    5 Posts 3 Posters 558 Views 3 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.
    • M Offline
      Martinnn
      last edited by

      I have a setup where I might have to run applications in different subdirectories. Let's say instead of having

      /flash/main.py with libraries in /flash/lib, I want to place it in

      /flash/dir1/main.py with libraries in /flash/dir1/lib etc.

      If I just move an application from the root to a subdirectory, it won't find imports from the lib folder as /flash/lib is in sys.path, but /flash/dir1/lib obviously is not.

      How do I make an application location-independent? More specifically, how do I achieve that imports are found independent of the main.py location? Of course, if I know "dir1" I can append the /flash/dir1/lib path to sys.path, but how do I find out the directory of main.py? Also, there is still /flash/lib in the path which might lead to ambiguities if other library versions exist.

      Actually what I would need is a relative path import like "import lib/mylibrary", but I have found no documentation on that for micropython. Any suggestions?

      robert-hhR 1 Reply Last reply Reply Quote 0
      • P Offline
        Paul Thornton
        last edited by

        I havent had a chance to try this in micropython yet. but in standard python a trick like:

        os.path.dirname(os.path.abspath(file)) but im pretty sure that micropython is missing some of the cruicial parts of the os lib to make that work.

        Im quite curious about this myself now. Leave it with me and when I get a chance ill see what I can come up with.

        M 1 Reply Last reply Reply Quote 0
        • M Offline
          Martinnn @Paul Thornton
          last edited by

          @paul-thornton any idea yet?

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

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • P Offline
              Paul Thornton
              last edited by Paul Thornton

              I've asked internally and It does not seem like we have a good way to get the equivalent of __FILE__ in micropython. Short of adding ALL of your applications ./lib subfolders to sys.path in boot.py prior to running/importing anything else.

              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