Where should the program modules be located?

Asked

Viewed 32 times

-2

I developed a personal agenda on Python using the Sqlalchemy and the Pyqt5. I realized that, for example:

I have the main.py aquivo

app = QtWidgets.QApplication([])

login = TelaLogin()

if login.exec() == 1:
    sessao_user = login.retorna_sessao()
    janela_principal = JanelaPrincipal(sessao_user)
    janela_principal.show()

sys.exit(app.exec_())

and the add_user.py file:

 import sqlalchemy
 user = Usuario(usuario_nome = name, usuario_login = login, usuario_senha = password)
 connection.session.add(user)
 connection.session.commit()

Then I realized that if I change the import sqlalchemy to the archive main py. does not change the behavior of the system, that is, not the error. The Imports should stay in the main py. ? or this has nothing to do with?

This system has several files, several Imports and many lines, so I also want to know if this interferes with the performance.

  • The import sqlalchemy no problems if done in main py. if you import it first than the module add_user.py. If you import your module first and then import the package an error will be generated, because the part that needs the package sqlalchemy will be executed before.

  • 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.

  • What if I need sqlalchemy in many modules ? I need to import and each one of them ?

  • 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 module Tk, in another module X the classes Button and Entry, in another module Z the class Label, etc. The syntax for this is from <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.

  • The behavior does not change because the module loaded once cached https://docs.python.org/3/reference/import.html#the-module-cache

  • 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?

  • Please read the following question in order to understand: https://pt.meta.stackoverflow.com/q/2333/157404

  • You can click on the arrow beside the comment as a form of "like", if you like the reply in comment form.

Show 3 more comments

1 answer

1


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? :)
  • Leaving modules/packages independent of one another is called decoupling and is almost a prerequisite for unit testing

Browser other questions tagged

You are not signed in. Login or sign up in order to post.