So I want to clarify a bit about what import
does and how to use it. Literally, any python file can (technically) be used as a module. For example:
*Note: I am assuming for these little demos that file 1 and 2 are always in the same directory
# file1
import file2
print ('in primary file')
# file2
print ('this is from the imported file')
outputs:
this is from the imported file
in primary file
But this obviously does not happen when you import a library because it doesn’t output a bunch of info when you do import gamelib
for example. This is because everything is in functions or classes. So here is another example:
# file1
import file2
print ('in primary file')
# file2
def foo():
print ('this is from the imported file')
outputs:
in primary file
because foo was never called. In contrast:
# file1
import file2
foo()
print ('in primary file')
# file2
def foo():
print ('this is from the imported file')
outputs:
NameError: name 'foo' is not defined
This is because foo is not inside the scope of file1
, it is inside the scope of file2
. If you are familiar with C++, etc this is akin to a namespace (just ignore if you’re not familiar with this). To make this work you have to tell python that foo()
is part of file2, not file1. So like this:
# file1
import file2
file2.foo()
print ('in primary file')
# file2
def foo():
print ('this is from the imported file')
outputs:
this is from the imported file
in primary file
But what if you have a massive python file and you really don’t need everything inside of it. You just want certain parts of the file. This is where the from ____ import ____
comes from. For example:
# file1
from file2 import foo
foo()
bar()
print ('in primary file')
# file2
def foo():
print ('this was imported')
def bar():
print ('this was not imported')
outputs:
this was imported
NameError: name 'bar' is not defined
The error is thrown because you did not import the entire file, just the function foo()
. Note that with this different syntax I do not specify file2.foo()
, just foo()
.
This covers basic importing another file. What if you want to import a whole bunch of files that are inside a folder, and control what you get from them. Python handles this by specifying that __init__.py
is where it looks when you import a folder.
Looking at the init file for the gamelib folder:
"""
The gamelib package contains modules that assist in algo creation
"""
from .algocore import AlgoCore
from .util import debug_write
from .game_state import GameState
from .unit import GameUnit
from .game_map import GameMap
from .advanced_game_state import AdvancedGameState
__all__ = ["advanced_game_state", "algocore", "game_state", "game_map", "navigation", "unit", "util"]
We can see that this uses almost exactly the same syntax that I have above, with the exception that there is a period in front of each file name. The __all__
is essentially a list of modules that are required when you import gamelib. This primarily happens/is necessary when you do from gamelib import *
(meaning import all), which is not recommended.
Thus, in your code it prevents you from having to import a ton of files, instead you just do import gamelib
and now you have access to all of those other files.
So to use the GameState class, eg make an object from that class, you would do gamelib.GameState
in the same way when we imported other modules from the examples above. And thus, this matches the syntax for creating a GameState object that we see in the code: game_state = gamelib.GameState(self.config, turn_state)