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.


    ImportError

    Pythonista
    4
    6
    5531
    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.
    • nev
      nev last edited by

      I have been using PyCharm on an iMac and have developed some modules that are imported into my python script (MYmain.py) using the statement "from MYsubs import sub1,sub2,sub3". This all works fine in PyCharm with the MYsubs.py and MYmain.py in the same project file structure, but the same statement with Pythonista on my iPad results in "ImportError - No module named MYsubs". I have copied MYmain.py and MYsubs.py into the External Files structure and also tried with a copy of MYsubs.py in the script library (This iPad) location, but cannot satisfy the import command, what is the correct method to define the path to external custom modules in Pythonista? If you could provide details of how to import custom modules from an external file, please advise.

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

        It is all about the path.

        First, understand that only site-packages start out in sys.path. When you hit play, the current script path is added to the start of sys.path until the next script you run.

        So, files colocated in a folder under Documents can import each other. Also, any files in site-packages can find each other.

        I suspect External Files, except iCloud, work differently, since the running script folder is not actually owned by pythonista, and possibly the folder appears different to python. Someone else reported similar problems, which they couldnt actually articulate but they also mentined External Files.

        the best is to copy files into Documents, same folder. note these will nit be avaailable to import in the console, unless you explicitly add the folder to to sys.path, but running scripts should work. Another option is to put any commonly used building blocks in site-packages, or a folder in site packages (eg from mypackage import mymodule). Things in site packages dont get reloaded when they change, so another option is to use sys.path.insert to add a relative path before importing.

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

          @JonB The directory of the last run script stays in sys.path after the script has finished running. That means after you run a script, you can import modules from its folder in the console.

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

            Are your modules in an external app such as Working Copyand you are trying to import them? Is that the external files you mention, an external file provider? There’s a limitation to those because even if the path is set properly iOS limits searching of a file providers files. There are some ugly workarounds for that though

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

              Thanks for your help, as JonB suspected my problem was having scripts located in "External Files".

              After creating a new folder in the "SCRIPT LIBRARY" in "THIS iPAD" then importing the scripts from my iCloud drive (MYsubs.py and MYmain.py) into this new folder the "from MYsubs import sub1,sub2,sub3" statement in MYmain.py correctly located my custom modules (sub1, sub2, sub3) from MYsubs,py.

              I was so happy to discover Pythonista and it worked like a breeze when I first opened it up and clicked on "EXTERNAL FILES"/"Open" selected and ran some python scripts from the iCloud Drive, which I'd been developing on the iMac/PyCharm, that I tried several scripts which again all worked until the aforementioned import error. Of course I hadn't read any documentation or run tutorials, but reading your responses then looking at the Pyhonista app, it's pretty obvious (now) that I should have imported the scripts into the "SCRIPT LIBRARY" (Dooh!).

              Apologises for taking your time, next time I'll do a little more homework, but again thanks for your responses, it got me to the solution pretty quick. Next task is to do platform or screen resolution identification as although the scripts work now on iPhone & iPad the formats will need much massaging.

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

                I was able to work around a similar problem without needing to move the project from Working Copy to Pythonista using a custom import hook. It's a hack but it works for me. Each file seems to need to be opened by Pythonista in it's external location at least once before importing via the hook will work.

                https://github.com/robnee/importhack

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