Python: Is there a place where I can put the default import for all my modules?

Is there a place where I can put the default import for all my modules?

+3
source share
2 answers

Yes, just create a separate module and import it into yours.

Example:

# my_imports.py
'''Here go all of my imports'''
import sys
import functools
from contextlib import contextmanager  # This is a long name, no chance to confuse it.
....


# something1.py
'''One of my project files.'''
from my_imports import * 
....

# something2.py
'''Another project file.'''
from my_imports import * 
....

Please note that in accordance with standard recommendations should be avoided . If you are managing a small project with several files that need a common import, I think that everything will be alright with you , but it will still be a better idea to reorganize your code so that different files need different imports. from module import *from module import *

So do this:

# something1.py
'''One of my project files. Takes care of main cycle.'''
import sys
....

# something2.py
'''Another project file. Main program logic.'''
import functools
from contextlib import contextmanager  # This is a long name, no chance to confuse it.
....
+3
source

python, PYTHONSTARTUP, python, , . .

+4

Source: https://habr.com/ru/post/1716378/


All Articles