omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    How to import user defined modules?

    Pythonista
    7
    13
    12660
    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.
    • Webmaster4o
      Webmaster4o last edited by Webmaster4o

      This isn't a bug, you're supposed to put them insite-packages. If you really want, you could put import sys,os;sys.path.append(os.path.expanduser('~/Documents')) at the beginning of scripts.

      EDIT:
      Actually, Documents seems to be the first element in sys.path for me. Is this not the case for you?

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        The first sys.path entry might be the directory of the last script run, and not always ~/Documents itself.

        1 Reply Last reply Reply Quote 1
        • huub
          huub last edited by

          No, Documents is not on the path at all. Strange that we have different paths... I was aware of site-packages but it is much easier to develop in anorher directory and import directly from there. When the module is complete it can be moved to site-packages.

          Thanks for the tip about os.path.expanduser! I considered adding a directory to sys.path but typing an absolute path is nearly impossible. Expanduser makes it possible.

          Thanks!

          1 Reply Last reply Reply Quote 1
          • huub
            huub last edited by

            Dgelessus was right, the directory from which the last script was run is the first entry in sys.path. This inspired the following workaround: put an empty script in the directory from which you want to import. Run it, and the directory is on the path. You can import modules from that directory!

            1 Reply Last reply Reply Quote 0
            • Webmaster4o
              Webmaster4o last edited by

              @huub Actually, I just realized that it's more permanent if you put my above code in pythonista_startup. Create a file pythonista_startup.py in site-packages, and put my above code inside. Whatever goes in this file will be run automatically when pythonista starts up

              1 Reply Last reply Reply Quote 0
              • dgelessus
                dgelessus last edited by

                @Webmaster4o Unfortunately that isn't very permanent either. Any modifications to sys.path are lost the next time you run a script, presumably because Pythonista adjusts the first path entry.

                I ran into this issue when trying to make pip work in Pythonista. Both setuptools and pip are not installed directly into the root of site-packages, so they require the use of pth files to be loadable by Python. These pth files are recognized when the site module is imported, which happens automatically, but only once when the app is launched. When Pythonista resets sys.path the site module is not fully reloaded, so the additional path entries are lost.

                1 Reply Last reply Reply Quote 2
                • tmillermillert
                  tmillermillert last edited by

                  How can I add the twisted module?

                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by

                    @tmillermillert Please search the forum for the word twisted and then if you do not have what you need, please reask your question on one of those that has more twisted related content. (Twisted is supercool)

                    1 Reply Last reply Reply Quote 0
                    • huub
                      huub last edited by

                      Thank you very much for this discussion, I understand a lot more abuot the mechanisms behind imports and sys.path and I believe that I can solve or at least work around most issues. However, I think it is a good idea to provide a bit more control about the contents of sys.path.

                      Would it be possible to add a setting where you can check for each directory in your user space if it has to be included in sys.path?

                      1 Reply Last reply Reply Quote 1
                      • AtomBombed
                        AtomBombed last edited by

                        All you have to do is put the module file inside of the site-packages folder that comes with Pythonista when you download it. (If you have deleted it, just make a new one with that exact folder name), and then when you make an "import" statement, just type in the name of the .py file for the module you just put in the site-packages folder without the .py.

                        Example:

                        # I make a module here, with a filename called 'mymodule.py'
                        def testfunction():
                            return True
                        

                        If I want to have another file use this module, just do this, in a new file:

                        import mymodule # notice how there isn't a .py
                        print mymodule.testfunction()
                        

                        When I run that file, it will print "True" in the output console. Does any of that help?

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors