有没有替代Python的函数,如PHP include()函数?

编程入门 行业动态 更新时间:2024-10-24 09:22:34
本文介绍了有没有替代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()

请参阅文档(如 MYYN )

更多推荐

有没有替代Python的函数,如PHP include()函数?

本文发布于:2023-07-27 00:18:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Python   include   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!