I'm trying to import a few classes from a module in another level. I can type all the classes, but I' trying to do it dynamically
if I do:
from ..previous_level.module import *
raise: SyntaxError: import * only allowed at module level
the same from myapp folder:
from myapp.previous_level.module import *
raise: SyntaxError: import * only allowed at module level
So I thought:
my_classes = ['Class_Foo', 'Class_Bar']
for i in my_classes:
from ..previous_level.module import i
raise: ImportError: cannot import name 'i'
and also:
my_classes = ['Class_Foo', 'Class_Bar']
for i in my_classes:
__import__('myapp').previous_level.module.y
raise: AttributeError: module 'myapp.previous_level.module' has no attribute 'y'
I've tried string format
, getattr()
, __getattr__
but no success.
It's impossible to do import this way, or I'm doing something wrong?
Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.
In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.
from module import * and exec make it impossible for the compiler to figure this out, because they add names to the local namespace that are unknowable at compile time.
We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.
Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.import_module() and built-in __import__() can also be used to invoke the import machinery.
What exactly happens when we write an ‘import’ statement? The python interpreter tries to look for the directory containing the module we are trying to import in sys.path. It is a list of directories that Python will search once it is done looking at the cached modules and Python standard library modules.
The import system — Python 3.9.0 documentation. 5. The import system ¶. Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way.
Recall the import path you saw earlier. It essentially tells Python where to search for modules. However, if Python finds a module in the module cache, then it won’t bother searching the import path for the module. In object-oriented programming, a singleton is a class with at most one instance.
The error SyntaxError: import * only allowed at module level
happens if you try to import *
from within a function. This is not visible from the question, but it seems that the original code was simething like:
def func():
from ..previous_level.module import * # SyntaxError
On the other hand, importing at the beginning of a module would be valid:
from ..previous_level.module import * # OK
That said, I would still suggest using absolute imports without *
:
from myapp.previous_level.module import ClassA, ClassB, ClassC
x = ClassA()
# or:
import myapp.previous_level.module
x = myapp.previous_level.module.ClassA()
BTW, this is completely wrong:
for i in my_classes:
from ..previous_level.module import i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With