有没有替代Python的函数,如PHP include()函数? [英] Is there an alternative python function does as PHP include() function?

查看:60
本文介绍了有没有替代Python的函数,如PHP include()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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()

请参阅文档(如 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆