Python: import "import file"

I import a lot of different scripts, so at the top of my file it is cluttered with import statements, that is:

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

Is there a way to move it all somewhere else, and then all I have to do is import this file, and only one clean import?

+5
source share
2 answers

Of course have; just create a file with a name myimports.pyin the same directory where your main file is, and put your import there. Then you can just use from myimports import *in your main script.

+4
source

, . global include file. ( , ), , , , . , . . , , . , , , , , .

, , , .

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

, , , ,

from somewhere.fileA import MyClass

.

 from somewhere import fileA 

 <later>

 a=fileA.MyClass()

? : -, . MyClass, . -, . , ,

 a=MyClass()

, MyClass, , . , , , /search, , fileA.

: "fileA", . ( ), . , , , , . , .

+11

All Articles