The behavior of the system does not change due to module cache, because once the module - or package - is loaded, it will be saved to cache until the end of the execution of the program, being accessible also to the modules that import the module containing the import of the package.
However, you should take great care with the order of imports in your code. Assuming the import of sqlalchemy
stay in the main.py
, it must come before importing the module add_user.py
. Otherwise, your module code will be loaded first and the interpreter will launch a NameError
- because of the package sqlalchemy
not yet imported, to be used in your code.
import sqlalchemy
import add_user # Esse módulo precisa do "sqlalchemy", por isso vem depois.
As for the performance of the program, many imports can cause problems. The ideal is to import only the classes and functions you need instead of importing the whole package. For this, use the syntax below:
from <pacote> import <item1>, <item2>, ...
In addition, it is recommended that you always import directly into the modules that depend on them to work. The reason for this is to avoid possible confusion about where a certain class or function comes from. Imagine you’re working on a project with dozens of modules, and in one of them, you come across this code, right in the first few lines:
db = Database(core.URL)
db.init()
# ...
Now what? Where did this class come from Database
or that core
? Even in small and personal projects, you may end up getting confused at some point, especially if you leave the project for a few months - or maybe years.
Also, when you don’t import the package directly into the module that depends on it, your code becomes dependent on your other module. And this is very bad! The best thing to do is always create code independent. This way, you avoid problems in your program if the import is accidentally deleted.
And how is it possible for me to accidentally delete an import that I need?
Imagine that you import an X package only into your main module in order to use the package in both modules. If it takes a few weeks, or months, and you rewrite the code of the main module, replacing the X package with the Z package - because you think it is better. Since it no longer makes sense to import the X package since you no longer use it in the module, you decide to delete it.
What will happen? A beautiful NameError
will be released. Because even if your main module no longer needs the X package, your other module does. By this bias, keep your imports always separate, within each module that depends on them.
And when I must import a package or module completely?
As I said above, the ideal is to always import "pieces" from the module. But there are times when it’s okay to import a module completely - or is even recommended, for organization purposes.
One of these moments is when you need the module name to make your code more organized. A good example of module to use the "normal" import is the math
, because it is very small and leaves the code more organized, due to the fact of needing to declare the name of the module before the function and constants, avoiding again confusions.
result = 4 * pi * pow(radius, 3) / 3
result = 4 * math.pi * math.pow(radius, 3) / 3 # Bem melhor, não acha? :)
The
import sqlalchemy
no problems if done in main py. if you import it first than the moduleadd_user.py
. If you import your module first and then import the package an error will be generated, because the part that needs the packagesqlalchemy
will be executed before.– JeanExtreme002
It is recommended that you always import in the modules that use the package, so that there is no confusion about where a certain class or function comes from, and avoid problems if you end up excluding the import of the main module, when you think it is no longer useful, when in fact you need for your other module.
– JeanExtreme002
What if I need sqlalchemy in many modules ? I need to import and each one of them ?
– Iniciante
Normally you do not care about the whole module, but rather some function or class of it. For an example, I will use the library
tkinter
. Instead of importing the package completely, import the class into the main moduleTk
, in another module X the classesButton
andEntry
, in another module Z the classLabel
, etc. The syntax for this isfrom <module | package> import <item>
. Usually only the whole package matters when it is used in only one module, in the Python language - in other languages it may be different how to import - or when the package is not a very large one.– JeanExtreme002
The behavior does not change because the module loaded once cached https://docs.python.org/3/reference/import.html#the-module-cache
– Guilherme Nascimento
I’m new to stackoverflow, both in English and Portuguese, but the people here don’t answer the questions? how do I mark the right answer?
– Iniciante
Please read the following question in order to understand: https://pt.meta.stackoverflow.com/q/2333/157404
– JeanExtreme002
You can click on the arrow beside the comment as a form of "like", if you like the reply in comment form.
– JeanExtreme002