30

I am importing a lot of different scripts, so at the top of my file it gets cluttered with import statements, i.e.:

from somewhere.fileA import ...
from somewhere.fileB import ...
from somewhere.fileC import ...
...

Is there a way to move all of these somewhere else and then all I have to do is import that file instead so it's just one clean import?

rypel
  • 4,686
  • 2
  • 25
  • 36
MxLDevs
  • 19,048
  • 36
  • 123
  • 194

2 Answers2

38

I strongly advise against what you want to do. You are doing the global include file mistake again. Although only one module is importing all your modules (as opposed to all modules importing the global one), the remaining point is that if there's a valid reason for all those modules to be collected under a common name, fine. If there's no reason, then they should be kept as separate includes. The reason is documentation. If I open your file, and see only one import, I don't get any information about what is imported and where it comes from. If on the other hand, I have the list of imports, I know at a glance what is needed and what not.

Also, there's another important error I assume you are doing. When you say

from somewhere.fileA import ...
from somewhere.fileB import ...
from somewhere.fileC import ...

I assume you are importing, for example, a class, like this

from somewhere.fileA import MyClass

this is wrong. This alternative solution is much better

 from somewhere import fileA 

 <later>

 a=fileA.MyClass()

Why? two reasons: first, namespacing. If you have two modules having a class named MyClass, you would have a clash. Second, documentation. Suppose you use the first option, and I find in your code the following line

 a=MyClass()

now I have no idea where this MyClass comes from, and I will have to grep around all your files in order to find it. Having it qualified with the module name allows me to immediately understand where it comes from, and immediately find, via a /search, where stuff coming from the fileA module is used in your program.

Final note: when you say "fileA" you are doing a mistake. There are modules (or packages), not files. Modules map to files, and packages map to directories, but they may also map to egg files, and you may even create a module having no file at all. This is naming of concepts, and it's a lateral issue.

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
24

Of course there is; just create a file called myimports.py in the same directory where your main file is and put your imports there. Then you can simply use from myimports import * in your main script.

Tamás
  • 47,239
  • 12
  • 105
  • 124
  • 3
    Note though that this shouldn't be done except in special circumstances and if you can 100% guarantee that not a single name is shadowed. See http://docs.python.org/howto/doanddont.html#from-module-import –  Jun 01 '11 at 18:54
  • 3
    Yes of course; this is not a coding practice that I would advocate in any circumstances, but who am I to question the intention of the poster? :) – Tamás Jun 01 '11 at 18:55
  • 7
    It's common (and good for everyone involved) to answer "How to I do " with "What is X exactly? ... Ah, yes. You can do that better like this: ..." –  Jun 01 '11 at 18:56
  • So I should just stick with potentially a dozen import statements? lol cause each additional file I import is like a plug-in for my main script. – MxLDevs Jun 01 '11 at 18:58
  • 2
    A dozen imports really isn't bad, when trying to understand someone else's code. – Eric Wilson Jun 01 '11 at 18:59
  • 1
    Does `import myimports` not work? Are imports not recursive? – Aaron Franke Mar 07 '19 at 06:20
  • Question here. Say all the different scripts will be relying on one another, so you must import them all in your import file. Would this create a "double up" of each script within that script? Eg: I might have "import somefile" in imports.py and also in somefile.py I'll have "import imports". Would this create two namespaces and two imports eg: somefile.somefileFunction1 as well as just somefileFunction1 when I call somefile directly? Would they conflict? – user4779 Mar 19 '19 at 04:33
  • This is a recipe for circular import bugs. – user2357112 Apr 20 '20 at 06:49