Moran Reznik
Moran Reznik

Reputation: 1371

Question about the correct way to orgenize a small project in python in term of imports

I am making a small project in python. As part of this project, I created 7 functions. Each function is saved in a separate file. I also have a main file - the code inside of it runs the functions. each of the functions in the files needs at least two external libraries. Also, each external library is used at at least 2 functions.

  1. Where should I place the imports?
  2. If I call a function in a file that includes an import several times in the main code, will python redundantly import the library several times?

Upvotes: 1

Views: 31

Answers (1)

Vlad Bezden
Vlad Bezden

Reputation: 89527

where should I place the imports?

You place imports in each file that references that library. So if you have module 'A' and it has function my_func() that references 'math' you import it in the 'A' module.

if I call a function in a file that includes an import several times in the main code, will python redundantly import the library several times?

No, Python is smart enough to do it only once. It's kind of singleton. Before it load a module, Python will check if it's already loaded and if it's loaded, it will reuse it.

Upvotes: 1

Related Questions