How to write all modules as a package in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to write all modules as a package in python

I want to create a python package and modules inside it. For example I want to use it like `from mypackage.a import A` or `from mypackage.b import B`. For that I have created a folder named 'mypackge' inside that created some subfolders 'a' , 'b', etc. Inside subfolders I have created python files which contains different classes. Problem is that when I want to import any claas from a particular module I have to write `from mypackage.subfolder.filename import classname`. Can anyone help me to find out the good way to make modules of this type in python?

28th Jan 2021, 4:26 PM
KUMAR SHANU
KUMAR SHANU - avatar
7 Answers
+ 2
This is why your "from flask import Flask" is working: https://github.com/pallets/flask/blob/master/src/flask/__init__.py Notice the line "from .app import Flask" That import references app.Flask directly but by importing it in flask/__init__.py, you can then import with "from flask import Flask". "from flask import Flask" is like saying "from flask.__init__ import Flask". You could add something like that for each and every class in your project too.
29th Jan 2021, 6:24 AM
Josh Greig
Josh Greig - avatar
+ 2
Don't forget to also add __init__.py files in all of those folders. The __init__.py files can be empty but they must exist for Python to recognize your directory as a Python package. Your import statement will be different based on what directory your .py file is in too. Python's packages are much less intuitive than Java's so keep that in mind if you're trying to compare Python and Java. More details are at: https://docs.python.org/3/tutorial/modules.html
28th Jan 2021, 5:12 PM
Josh Greig
Josh Greig - avatar
+ 1
No. You must indicate the .py file's name(minus the .py) to import a class in it. Python lets you dynamically import modules and get classes and any variables defined in it, though. It can make your code harder to follow but it is close to what you're asking for. There is some related discussion here that I turned into a working example: https://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class I wrote a working example for Python 2.7. The most important part is this function: def get_class_by_name(class_name): module_name = 'myclasses.' + class_name.lower() the_module = getattr(__import__(module_name), class_name.lower()) return getattr(the_module, class_name) obj = get_class_by_name('A')() print(obj.get_x()) # prints 0 obj = get_class_by_name('B')() print(obj.get_x()) # prints 1 The complete example also included the folder structure: myclasses /__init__.py myclasses/a.py myclasses/b.py myclasses/a.py contained: class A: def get_x(self): return 0 myclasses/b.py contained: class B: def get_x(self): return 1
29th Jan 2021, 5:50 AM
Josh Greig
Josh Greig - avatar
+ 1
You're welcome. I'll remove my github repository now just to keep my repo list cleaner.
29th Jan 2021, 6:39 AM
Josh Greig
Josh Greig - avatar
0
Josh Greig Yes, I have added __init__.py in all folders and subfolders. And I don't know java. The thing I want is I want to import class name from a subpackage without mentioning the name of the file. For example it is now as "from package.subpackage.module import class" But I want "From package.subpackage import class" Is it possible??
29th Jan 2021, 4:03 AM
KUMAR SHANU
KUMAR SHANU - avatar
0
Josh Greig I have seen flask source code on their repository. They have defined flask as package and inside that they have created module named app.py inside this module they have defined Flask class. But we don't import flask like "from flask.app import Flask". We import it like "from flask import Flask"
29th Jan 2021, 5:55 AM
KUMAR SHANU
KUMAR SHANU - avatar
0
Ohhh, I wanted this. I thought it will be empty file so I didn't open it. Now I understood. Josh Greig thank you for your effort. 👍🏼
29th Jan 2021, 6:36 AM
KUMAR SHANU
KUMAR SHANU - avatar