有没有替代Python的函数,如PHP include()函数? [英] Is there an alternative python function does as PHP include() function?
问题描述
我的python项目路径/project
及其这些文件:
My python project path /project
and these are its files :
/project/test.py
/project/templates/
/project/includes/
/project/includes/config.py
# config.py
import os
template_path = os.path.join(os.path.dirname(__file__), "templates")
当我使用execute()函数从test.py包含config.py时,执行/project/includes/config.php
文件并将结果返回到/project/includes/test.py
,因此template_path变量已另存为/project/includes/templates/
,而在此找不到模板文件夹.
我想要一个函数包含/project/test.py
中的/project/includes/config.py
并在/project/includes/config中执行所有功能.py
到 test.py
.
when I used execute() function to include config.py from test.py , the /project/includes/config.php
file was executed and the result was returned to /project/includes/test.py
so template_path variable was saved as /project/includes/templates/
and templates folder would not be found there .
I want a function to include /project/includes/config.py
from /project/test.py
and execute all functions in /project/includes/config.py
through test.py
.
推荐答案
在 includes
中创建一个(空)文件 __ init __.py
.
Create a (empty) file __init__.py
in includes
.
在 test.py
中,通过执行 import
调用 config.py
.
from包含导入配置
因此,如果在 config
中有一个说 foo
的函数,则您将这样称呼它:
So if there is a function say foo
in config
, you will call it like:
config.foo()
请参阅文档(如 查看全文