Mar-03-2023, 04:26 PM
As you see all files as you describe.
I like to modify so can just to simple import like this and files are added.
In
This will bring constants and function into the namespace of package.
Some more examples you can look in link in this post.
C:\Python310\mymodule λ ls __init__.py constants.py function.py test.py
my_func()
will never work have to change to this.#function.py from mymodule.constants import my_const def my_func(): print(my_const)Now can run
test.py
,and it will work.C:\Python310\mymodule λ python test.py 42A little more about how a package works.
C:\Python310\mymodule λ ptpython >>> import mymodule >>> >>> dir(mymodule) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__'As see here so is nothing added to namespace if just import like this.
I like to modify so can just to simple import like this and files are added.
In
__init__.py
import mymodule.constants import mymodule.functionTest again.
C:\Python310\mymodule λ ptpython >>> import mymodule >>> >>> dir(mymodule) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'constants', 'function', 'mymodule'] >>> mymodule.constants.my_const 42 >>> mymodule.function.my_func() 42Or can import like this without modify
__init__.py
.This will bring constants and function into the namespace of package.
C:\Python310\mymodule λ ptpython >>> from mymodule import constants, function >>> constants.my_const 42 >>> function.my_func() 42
Some more examples you can look in link in this post.