Code in a file is referred to as a module. You can encapsolate code in modules and then import them into other files.
Say you have a file foo.py with:
def bar():
print('baz')In another script in the same directory, you can use the bar function like this:
import foo
foo.bar()
#=> bazYou can also import the function like this:
from foo import bar
bar()
#=> bazIf the foo module is located in a directory, like biz
├── biz
│ └── foo.py
└── main.py... you can import it like this
from biz.foo import bar
bar()... or, like this
import biz.foo
biz.foo.bar()This is referred to as a "package". In earlier versions of Python you needed to include an __init__.py file in a directory to make it a package. However this is implicit now and not required.
The __init__.py file can be a good place to put initialization code, if you're package needs any. The file is only loaded and executed the first time a package is imported.
Inside of a package you need to import things slighly differently. Lets say you have a file biz/baz.py and you need to import biz/foo.py into it to get the bar method:
# baz.py
from .foo import bar
bar()The dot prefix tells the Python importer to look in the current package.
When a module name is used, Python will first look locally and then it will look up a series of directories, which you can access with the sys module:
import sys
print(sys.path)You can use the dir method to figure out what a module defines:
import biz.foo
dir(biz.foo)
#=> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'bar']Sometimes you only want to code to execute if it's being run directly, not when it's being imported.
From the documentation:
'main' is the name of the scope in which top-level code executes. A module’s name is set equal to 'main' when read from standard input, a script, or from an interactive prompt.
def main():
pass
if __name__ == "__main__":
main()