从不同目录导入函数,这些文件中的每个文件仅包含一个函数

问题描述 投票:0回答:1

我目前有一个类似的目录结构:

project/
  indicators/
    __init.py__ (contains __all__ = ['ATR', 'MACD'])
    ATR.py
    MACD.py
  strategies/
    strategy.py

在ATR.py和MACD.py中,我具有类似的功能:

def ATR(dataframe, period):
  # do math

从strategy.py,我可以做:

from indicators.ATR import ATR

ATR(dataframe, period)

from indicators import *

ATR.ATR(dataframe, period)

但是我想做类似的事情

from indicators import ATR, MACD

ATR(dataframe, period)
MACD(dataframe, period)

建立此结构的正确方法是什么?我想我需要一种在每个指标中指定某种“默认”导出的方法。

python directory structure
1个回答
1
投票

__init__.py列表中,在__all__列表之前:

from ATR import ATR
from MACD import MACD

这会影响您不喜欢的其他导入,但是我认为您可以看到,可以根据自己的喜好在__init__.py中管理要导出的名称。

© www.soinside.com 2019 - 2024. All rights reserved.