从python27中的文件夹导入模块

2024-09-29 01:23:04 发布

您现在位置:Python中文网/ 问答频道 /正文

这听起来很荒谬,因为这个网站上对这个问题的回答似乎是无限的-但是如果不临时更改每次重新加载的系统路径(或init,这对我的设置不起作用),我找不到一个简单的解决方案。我在找一个安全的,非黑客的方法来完成这个。在

简单地说-我有如下目录结构:

**root**
>main.py
>**modules**
  >>rivescript.py
  >>js.py
>**plugins**
  >>weather.py
  >>synd.py

为了简单起见,我想将每个可用的模块以本机方式导入到所呈现的子目录(模块、插件)中主.py在

伪码:

^{pr2}$

并能够调用函数,例如:

plugins_weather.get("3088")
modules_rivescript.RiveScript.reply("localuser", language_input)

有什么建议吗?速度和资源消耗对这个项目来说是一件大事。在


Tags: 模块方法py路径目录modulesinit网站
1条回答
网友
1楼 · 发布于 2024-09-29 01:23:04

首先,您应该将u init_uu.py文件(可能是空的)放在modules/和plugins/目录中,将它们标记为包。在

现在,您可以在中导入模块了主.py公司名称:

import modules.js as js
import modules.rivescript as rivescript

import plugins.weather as weather
import plugins.synd as synd

weather.get("3088")  # Usage example

相关问题 更多 >