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.


    Making and calling packages in Pythonista?

    Pythonista
    package packages
    2
    5
    2221
    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.
    • AceNinjaFire
      AceNinjaFire last edited by AceNinjaFire

      Hey everyone, I just found out about how to make packages in python and thought it was an absolutely good idea especially with how it can get repetitive typing the same code over and over again.

      My setup

      base folder
      main.py

      /mypackage
      init.py
      module1.py

      But as I’ve tried to do so in Pythonista it just keeps giving me errors.
      I’ve tried it this way

      #__init__.py
      
      from module1 import some_function
      
      

      And this way

      #__init__.py
      from .module1 import some_function
      

      Excluding and including the period.
      As I’ve seen it in many guides of making packages. But I keep getting this message when I try calling it from my main.py .

      ModuleNotFoundError: No module named 'main.module1'; 'main' is not a package

      I’m sure I’ve completely overlooked the answer somewhere. I’ve been on stackoverflow and seen quite a few posts about explicit relative imports but I can’t make heads or tails of what to do to make a package.

      I’d appreciate any help I can get!

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

        If your folder is called mypackage, you need to say

        from mypackage.mymodule1 import ....

        If mymodule1 needs to import from mymodule3 in the same folder, then it would use dot instead of my package.

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

          @JonB

          I’ve gotten that far, but I was trying to shorten what I type even more. As I’ve read in some posts that you can format the init.py file in a way that will completely dump all of the functions of the files into init file. So I wouldn’t have to reference module1 at all and just type from mypackage import some_function.

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

            In your __init__ you can define a variable __all__ which is a list of exported attributes, like if you use from package import *.

            In init you can also import any other packages, and import their full namespace if desired.

            Main.py would import mypackage

            AceNinjaFire 1 Reply Last reply Reply Quote 1
            • AceNinjaFire
              AceNinjaFire @JonB last edited by

              @JonB

              thanks that did it!

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