首页 > 解决方案 > 如何在python中重定向导入

问题描述

我在许多文件中有一个代码和一个导入from test import testfunc。我现在想将其更改为from lib import flow不接触文件而只接触启动脚本。我该怎么做呢?顺便说一句,这是python 3

我已经尝试过sys.module['test.testfunc'] = lib.flow,但这无济于事

from test import testfunc
test01 = testfunc()

如何让它像

from lib import flow
test01 = flow()

文件目录

root
 ├─main.py
 ├─lib.py
 ├─test.py
 └─test_plugins
    └─foo.py

主文件

from lib import flow
sys.module['test.func'] = lib.flow
import test_plugins.foo

库文件

def flow(input):
    print(input)

测试.py

def func(test):
    print(type(test))

test_plugins/foo.py

from test import func
func(007)

但现在我想要 foo.py 做

from lib import flow
flow(007)

标签: python-3.xsys

解决方案


推荐阅读