在不同的目录中导入 python 个具有相同函数名称的文件

import python files, with same function names, in different directories

我在不同的 目录 .

中有不同的 .py 文件

然而,每个 class 都有大部分相同的函数名称,只是代码方法不同。

我想用 pytest.

一起测试他们每个人的表现

test_CompositeKey.py:

import os
import pandas as pd

import pytest
from dagster import execute_solid
from CompositeKey.modules.Gordian.Gordian import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from CompositeKey.modules.Assisted_Gordian.Assisted_Gordian import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning.Assisted_Gordian_with_Cardinality_Pruning import (add_to_non_key_set, create_prefix_tree, dfs, find_uniqueness_of_keys, get_col_cardinalities, get_final_col_list, get_keyset, non_key_finder, prefix_tree_merging, set_index_to_col_names)
from pwmf.pipeline.utils import local_mode

df = pd.read_csv('../data/OPIC-scraped-portfolio-public.csv')
df_sample = df.sample(10)

@pytest.mark.unit
def test_add_to_non_key_set():
    curNonKey = {0, 1, 4, 8, 21}
    global NonKeySet
    NonKeySet = {(0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
# Changes
    observed_value_G = CompositeKey.modules.Gordian.Gordian.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AG = CompositeKey.modules.Assisted_Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AGC = CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning.Assisted_Gordian_with_Cardinality_Pruning.add_to_non_key_set(curNonKey, NonKeySet)

    expected_output = {(0, 1, 4, 21, 8), (0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
    assert (expected_output == observed_value_G) and (expected_output == observed_value_AG) and (expected_output == observed_value_AGC)

test_add_to_non_key_set()

错误:

Traceback (most recent call last):
  File "test_CompositeKey.py", line 135, in <module>
    test_add_to_non_key_set()
  File "test_CompositeKey.py", line 21, in test_add_to_non_key_set
    observed_value_AG = CompositeKey.modules.Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)
NameError: name 'CompositeKey' is not defined

我检查了文件夹路径,它们是正确的。事实上,我还有其他各种 classes,每个都导入其中一行,没有问题。

在每个导入文件的目录中创建空 __init__.py 文件没有帮助。


@blhsing 的解决方案:

import os
import pandas as pd

import pytest
from dagster import execute_solid
from CompositeKey.modules.Gordian import Gordian as G
from CompositeKey.modules.Assisted_Gordian import Assisted_Gordian as AG
from CompositeKey.modules.Assisted_Gordian_with_Cardinality_Pruning import Assisted_Gordian_with_Cardinality_Pruning as AGC
from pwmf.pipeline.utils import local_mode

df = pd.read_csv('../data/OPIC-scraped-portfolio-public.csv')
df_sample = df.sample(10)

@pytest.mark.unit
def test_add_to_non_key_set():
    curNonKey = {0, 1, 4, 8, 21}
    global NonKeySet
    NonKeySet = {(0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
    observed_value_G = G.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AG = AG.add_to_non_key_set(curNonKey, NonKeySet)
    observed_value_AGC = AGC.add_to_non_key_set(curNonKey, NonKeySet)

    expected_output = {(0, 1, 4, 21, 8), (0, 1, 4, 21, 6, 8), (0, 1, 4, 21, 6)}
    
    assert (expected_output == observed_value_G) and (expected_output == observed_value_AG) and (expected_output == observed_value_AGC)

当您使用时:

from CompositeKey.modules.Gordian.Gordian import (add_to_non_key_set ...)

CompositeKey.modules.Gordian.Gordian模块导入add_to_non_key_set,只有名称add_to_non_key_set本身被导入到当前模块的命名空间中,而不是模块CompositeKey.modules.Gordian.Gordian或者包裹 CompositeKey.

因此,您应该像使用 add_to_non_key_set(curNonKey, NonKeySet) 一样直接使用此名称引用 add_to_non_key_set

如果您更愿意通过包 CompositeKey 引用它,就像您尝试过的那样:

CompositeKey.modules.Gordian.Assisted_Gordian.add_to_non_key_set(curNonKey, NonKeySet)

您应该将包直接导入到当前模块的命名空间中:

import CompositeKey