'Import custom modules in python
I have segmented some code into several files (for better readability) with several dependencies from other standard libraries (numpy, matplotlib).
E.g,
**ploting.py**
import matplitlib.pyplot as plt
import numpy as np
plot something
**calculating1.py**
import numpy as np
some custom calculation functions using numpy routines
**calculating2.py**
import numpy as np
some custom calculation functions using numpy routines
Afterwards in a main.py I am calling the modules has
import numpy as np
from plotting import funcA,funcB,funC
from calculating1 import funcA,funcB,funC
from calculating2 import funcA,funcB,funC
I would like to know if there is any problem having to import matplitlib and numpy in several modules? Is there a way to make these available "globally"?
Additionally, does python have a name for this sort of structure? A .py file calling other modules? A package? if so, does main.py have a particular name?
Solution 1:[1]
From the docs:
The first place checked during import search is
sys.modules. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths.
So it's ok to import a module in several files (if you need that module in that file).
A package is is a directory that contains one or more .py files. A file called __init__.py is required to make Python treat directories as packages.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hoxha Alban |
