A way to import Python inside imports?

Sorry if this is a very newbie, I was just curious about something.

When in python and your code is split across multiple files, how would you avoid tons of imports on the same?

Let's say I have 2 files. Main and Content.

Main:

import pygame
from pygame.locals import *
pygame.display.init()
blah

      

Content:

import pygame
from pygame.locals import *
pygame.display.init()
load content and stuff

      

pygame

is imported twice and display.init

called twice. This is problematic elsewhere. Do I need to bypass this at all, or just import and import and import?

One situation I can think of is this: A script that gets written to a file every time it is imported. So if it is imported 3 times, it runs 3 times, so writing to the file 3 times.

Thanks in advance!

+3


source to share


3 answers


You should avoid that nothing happens on import (also see below). the python file is the first module, so it can and should be used by other python modules. If something "happens" during the import phase of a python file, it may not happen in an undesirable way when the file is imported by another module.

Each module should just define the things to use: classes, functions, constants, and just wait until something else uses them.


Obviously, if no script ever does on import, then it is impossible to actually use anything and make things "happen". There is a special idiom for the unusual case of a module being called directly. Every python file has a variable, __name__

automatically created with the module name that was imported as. When you run the script from the command line (or, nevertheless, you ran it), it was not imported and there is no name for it, so the variable __name__

will have a special meaning "__main__"

to indicate that the script is being executed. You can check this condition and act accordingly:



# main.py
import pygame
from pygame.locals import *

import content

def init():
    pygame.display.init()

def stuff():
    content.morestuff()

if __name__ == '__main__':
    init()
    stuff()

      

# content.py
import pygame
from pygame.locals import *

def init():
    pygame.display.init()

def morestuff():
    "do some more stuff"

if __name__ == '__main__':
    init()
    morestuff()

      

Thus; init()

and thus pygame.display.init()

only called once, by a script that was started by the user. the code that works, assuming it init()

has already been called, is split into another function and called as needed by the main script (whatever happens)

+3


source


You misunderstand what it is doing import

. This is not the same as include

. Loaded modules are single-point and their corresponding files are not evaluated twice.

However, a well-built module will have no side effects when imported. This is the purpose of the idiom if __name__=='__main__'

.



Don't try to "clean up" your imports. Import whatever you need to use from the file. You can use less import *

, but this is purely for code readability and maintainability.

+6


source


import

The assertions should be declarations that you are using something from another module. They shouldn't be used to call something (like writing to a file). As Francis Avila pointed out, Python will try not to execute module code more than once.

This leads to the fact that if the given statement import

leads to anything, it is a global application property at runtime; you cannot tell just from the import statement and the source code of the module, because it depends on whether any other code in the project has already imported that module.

Thus, having a "do something" module when it is executed is usually a very fragile way to implement your application. However, there is no hard and fast definition of "do something", because obviously a module has to create whatever other modules will import from it, and this could include reading configuration files, perhaps even writing log files, or any number of other things. ... But anything that appears to be "doing" your program, rather than just "setting" things to be imported from a module, should usually not be done in module scope.

0


source







All Articles