我正在try 在同一虚拟环境中使用包scrapygreat_expectations.这两个包之间的兼容性似乎有问题,这取决于我导入它们的顺序.

示例:

  • 我创建了一个虚拟环境,pip安装了每个软件包的最新版本.

有效:

import great_expectations
import scrapy

print("done")

Does not work:

import scrapy
import great_expectations

print("done")

Error:

Traceback (most recent call last):
  File 

"/Users/grant/vs_code_projects/grants_projects/test_environment.py", line 2, in <module>
    import great_expectations
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/__init__.py", line 32, in <module>
    register_core_expectations()
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/expectations/registry.py", line 187, in register_core_expectations
    from great_expectations.expectations import core  # noqa: F401
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/expectations/core/__init__.py", line 1, in <module>
    from .expect_column_distinct_values_to_be_in_set import (
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/expectations/core/expect_column_distinct_values_to_be_in_set.py", line 12, in <module>
    from great_expectations.expectations.expectation import (
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/expectations/expectation.py", line 2350, in <module>
    class BatchExpectation(Expectation, ABC):
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/expectations/expectation.py", line 287, in __new__
    newclass._register_renderer_functions()
  File "/Users/grant/Envs/test_env/lib/python3.8/site-packages/great_expectations/expectations/expectation.py", line 369, in _register_renderer_functions
    attr_obj: Callable = getattr(cls, candidate_renderer_fn_name)
AttributeError: __provides__

编辑:

  • 我试过Scrapy的所有主要版本(当前版本,2.0.0,1.0.0)与Great Expectations的所有主要版本,结果是一样的.

推荐答案

如果我将行"attr_obj: Callable = getattr(cls, candidate_renderer_fn_name)"改为"attr_obj: Callable = getattr(cls, candidate_renderer_fn_name, None)",它确实允许导入包.

对:如果在cls上不存在名为candidate_renderer_fn_name的属性,则将该行修改为包括默认值Nonegetattr将通过允许将attr_obj设置为None而有效地绕过AttributeError.

虽然这允许您的导入没有错误,但它也可能掩盖其他问题或导致后续的意外行为,特别是如果期望attr_obj总是成功检索属性.

您可以try 将Great Expectations的导入延迟到Scrapy初始化之后:使用一个函数来封装Great Expectations的导入和使用,确保仅在需要时导入.

import scrapy

def use_great_expectations():
    import great_expectations
    # Your Great Expectations code here
    print("Great Expectations used")

print("Scrapy and other initializations")
# Other Scrapy or general code here

# Now, call the function to use Great Expectations
use_great_expectations()

或者,从An Truong读到"100":

import scrapy
import lazy_import

great_expectations = lazy_import.lazy_module("great_expectations")

def use_great_expectations():
    # Assuming `great_expectations` functionalities are required here
    # Now, the Great Expectations module will be actually loaded
    print("Using Great Expectations")
    # Example Great Expectations code here

为什么?

When a module is imported, Python executes all top-level statements in the module.
If these statements modify global state, or if the module's initialization logic is complex (involving dynamic imports or modifications to the module's attributes), it can lead to bugs or conflicts when combined with other modules that have their own complex initialization procedures.

Scrapy依赖于zope.interface,它可以修改类和对象的行为,其方式可能与其他库期望Python对象模型的工作方式不完全兼容.

如果zope.interface或由Scrapy的导入改变的其他组件改变了属性访问的行为(通过__getattr____getattribute____dir__等机制),它可能会导致您观察到的AttributeError.

我确实看到了recent releases of zope.interface,但我没有看到任何recent Scrapy issue involving zope.interfaceAttributeError register,除了issue 4276.

Python相关问答推荐

Python plt.text中重叠,包adjust_text不起作用,如何修复?

Python在tuple上操作不会通过整个单词匹配

重新匹配{ }中包含的文本,其中文本可能包含{{var}

从dict的列中分钟

Python库:可选地支持numpy类型,而不依赖于numpy

C#使用程序从Python中执行Exec文件

我如何使法国在 map 中完全透明的代码?

Python,Fitting into a System of Equations

OR—Tools中CP—SAT求解器的IntVar设置值

对象的`__call__`方法的setattr在Python中不起作用'

mypy无法推断类型参数.List和Iterable的区别

Django—cte给出:QuerySet对象没有属性with_cte''''

合并帧,但不按合并键排序

需要帮助重新调整python fill_between与数据点

基于形状而非距离的两个numpy数组相似性

并行编程:同步进程

语法错误:文档. evaluate:表达式不是合法表达式

Python—在嵌套列表中添加相同索引的元素,然后计算平均值

Seaborn散点图使用多个不同的标记而不是点

如何在基于时间的数据帧中添加计算值