user4396006
user4396006

Reputation:

How to import appropriately

I have been wondering for a long time how to appropriately import in Python, in relation to the way I'm going to explain now:

Imagine I've got two modules built by myself, modulea and moduleb, and have a last one which uses both the modulea and b, so modulec.

Furthermore, modulea uses moduleb.

The modules are all one-file, which form part of a big module which has got its own __init__.py file.

+ My project
-
-> modulea.py
-> moduleb.py
-> modulec.py
-> __init__.py # Here I import modulea, b, c.
-> main_module.py # This is were I do all the hardcore.

I've created a file for importing all the modules I need, instead of importing them one by one in each script, and to know if I have already imported it, I would have to check file for file if I have already imported that or not.

I could simply declare, as you have been saying below in the comment box, the required modules in each module, but I think that's not practical and it's easier to import them all only in one file.

Tell me if this is a good practice or not.

If I import

import modulea
import moduleb
import modulec

It won't work (ImportError) because modulea does not implicitly import moduleb inside the code, and it is imported before than the module it requires (b)

If I did

import moduleb
import modulea
import modulec

I guess they will work because since the runnning script already imports moduleb before a, it should also be accessible to a and not only to the running script; and the same I guess it happens with modulec.

Supposing this is true, which I think indeed, will it work if the import were done in only one line? Are the modules imported and included into the script one by one or are they rather imported altogether and them enabled for use, after the statement finishes (the one-line import call)?

import moduleb, modulea, modulec

And if it does, does it shorten too much the processing time of the code, or is it basically the same, despite the aesthetic matter?

Upvotes: 0

Views: 112

Answers (3)

tdelaney
tdelaney

Reputation: 77367

You can put all of your imports into one place if you'd like.

myproject/internal.py

from moduleb import *
from modulea import *
from modulec import *

Now, your other modules can just import it

myproject/modulec.py

from internal import *

Although its usually preferable to import everything in the module that uses it, there are conditions where that is not desirable. I have a project with many small scripts that test product functionality. It would be a bother to have lots of imports in each of them.

Upvotes: 0

bgusach
bgusach

Reputation: 15205

You are overthinking way too much.

Each module must be "self contained" in the sense that itself satisfies its dependencies. If modulea needs moduleb, you do

# modulea.py
import moduleb

Then moduleb has to import its dependencies, if any:

# moduleb.py
import json  # for instance

And finally modulec needs modulea and moduleb, you just import them:

# modulec.py
import modulea
import moduleb  # <-- actually this import does not reload the moduleb, since it was already loaded in modulea, and it was cached.

There is nothing more about this.

And if you just need a function from a module, you can just import it into the namespace like this.

from moduleb import my_cool_function

Just as style convention, you should do each import in a new line:

import json
from moduleb import my_cool_function
from moduleb import another_not_so_cool_function

Upvotes: 8

Malonge
Malonge

Reputation: 2040

According to the python style guide you should use an import statement for each individual module. It will also tell you everything else you need to know about the conventions surrounding importing modules.

import moduleb
import modulea
import modulec

Also, as some of the commenters have indicated, you don't seem to be importing these modules properly within each script, thus creating errors here in this script.

Upvotes: 2

Related Questions