Skip to content Skip to sidebar Skip to footer

Import Same Python Module More Than Once

I am trying to find a way of importing the same module more than once due to a certain key press.... if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: import

Solution 1:

You can't.

I'm going to have to guess the structure of your code, since you havent quite provided a Short, Self Contained, Correct (Compilable), Example.

You probably have several modules that look like:

# foo_level.pyprint"foo"

along with a main module:

# main.pywhileTrue:
    key = raw_input()
    if key == "foo":
        import foo_level
    # and so on.

the import statement is designed for bringing code into scope, not for actually executing any code.

Put all of the code you want to run multiple times in a function:

# foo_level.pydefdo_stuff():
    print"foo"

and instead, import all modules once, at the beginning and call the new functions inside the loop:

# main.pyimport foo_level
whileTrue:
    key = raw_input()
    if key == "foo":
        foo_level.do_stuff()
    # and so on.

Post a Comment for "Import Same Python Module More Than Once"