Answer the question
In order to leave comments, you need to log in
Paths to Python modules - how to do it right?
I am new to Python, and with writing my own modules and connecting third-party code, several questions immediately arose.
1) Why in Flask, even if I refer to nested modules, the module name still comes from the root?
For example, at the root is the file start.py, from which I call the function located in logic/mylogic.py. Also in the logic folder there is another folder, and in it are some of my modules, for example a.py. Regarding mylogic.py, one could do import a, but only import logic.a works, even if the call comes from mylogic.py, which is already in the logic folder.
2) If on the Internet, on github, for example, I come across some lib or someone else's code, and I want to use it, and this code inside refers to its own internal modules, then I have to put it only in the root. As soon as I move it to some other subfolder, internal modules stop working, since all their imports again come from the root (same problem as in the previous question). Of course, it would be possible to rename all imports, but in some cases (for example, some tensoflow examples have thousands or more files) this is wildly inconvenient, maybe there is some normal way to avoid depending on the specific folder where the module is located?
Thank you!
Answer the question
In order to leave comments, you need to log in
In the folder where the imported module is located, you need to create an empty module __init__.py
Well, of course, if there are many sub-folders, then you need to create each one .
Inside the folders, a relative import
from is done. import my_module
If on the Internet, on github, for example, I come across some lib or someone else's code, and I want to use it, and this code inside refers to its own internal modules, then I have to put it only in the root. As soon as I move it to some other subfolder, internal modules stop working, since all their imports again come from the root (same problem as in the previous question). Of course, it would be possible to rename all imports, but in some cases (for example, some tensoflow examples have thousands or more files) this is wildly inconvenient, maybe there is some normal way to avoid depending on the specific folder where the module is located?
pip install tensorflow
. It's the same with all other libraries.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question