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?
-
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.pyBut 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!
-
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.
-
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.
-
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
-
thanks that did it!