?print-pdf
' Created for
├── my_app
│ ├── app.py # the main file
│ ├── my_module.py # a module file
def greet(name):
return f"Hello, {name}!"
import my_module
# Using the function from the module
print(my_module.greet("World")) # Output: Hello, World!
import X
def get_user_name():
return input('Enter your name:')
def greet(user_name):
print(f'Hello, {user_name}!')
# import the helper_module:
import helper_module
# use helper_module
user_name = helper_module.get_user_name()
helper_module.greet(user_name)
import X as Y
import helper_module as hm
user_name = hm.get_user_name()
hm.greet(user_name)
from X import Y
from X import Y
notation. After that, you can use the name (Y), without having to prefixing it.
from helper_module import get_user_name, greet
user_name = get_user_name()
greet(user_name)
from X import *
from helper_module import *
user_name = get_user_name()
greet(user_name)
import
looks for a module?
import sys
print(sys.path)
import my_module # Executes the module code
import my_module # Does nothing, uses cached module
__file__
and __name__
variables__file__
and __name__
variables.__file__
variable contains the pathname of the file from which the module was loaded__name__
is set to the module’s name, without the .py extension__name__
variable is set to '__main__' value
print( "__file__:", __file__)
print( "__name__:", __name__)
python helper_module.py
# __file__: /some/path/helper_module.py
# __name__: __main__
import helper_module
# __file__: /some/path/helper_module.py
# __name__: helper_module
__name__
.py
file, it executes the code in it!.py
file is executed as a module:__name__
is set to module's own filename.py
file is executed as stand-alone program:__name__
is set to "__main__"
__name__ - examples
Create in same directory next Python files:
import helper_module
if __name__ == "__main__":
print("helper_module is executed as stand-alone py file")
else:
print("helper_module is imported as module")
main.py
and look at the outputhelper_module.py
and look at the output__file__
__file__ - examples
Create in same directory next Python files:
import helper_module
print( "__file__:", __file__)
main.py
and look at the outputhelper_module.py
and look at the output__init__.py
__init__.py
file in the directory, which you want it to be treated as package from Python.__init__.py
can be an empty file
$ tree my_app
my_app/
├── app.py
└── packA
├── greet.py
├── __init__.py
└── packB
├── get_data.py
└── __init__.py
from python 3.2 __init__.py must be present only in Regular Packages, but can be omitted in Namespace Packages. More info in docs
$ tree my_app
my_app/
├── app.py
└── packA
├── greet.py
├── __init__.py
└── packB
├── get_data.py
└── __init__.py
get_data
module in app
with:
import packA.packB.get_data
.pyc
file containing the compiled code will be created.python -m module_name from terminal in order to execute a module
python -m helper_module
# note the difference between:
python3 helper_module.py
These slides are based on
customised version of
framework