【问题标题】:VS Code python doesn't autocomplete scipy.statsVS Code python 不会自动完成 scipy.stats
【发布时间】:2021-10-30 05:58:42
【问题描述】:

scipyt.stats 我们有变量binom 不知何故,vscode 中的自动完成功能似乎找不到它。 在 pycharm 中,自动完成工作正常。任何指针,好像为什么会这样? 还有一些带有binom的示例代码如下 from scipy.stats import binom; binom.rvs(10,0.5,size=12).

【问题讨论】:

  • 确保您选择了正确的 Python 解释器(F1 > Python:选择解释器)。还要确保您安装了 Python 扩展。

标签: python visual-studio-code scipy intellisense scipy.stats


【解决方案1】:

看来binom是在子模块scipy/stats/_discrete_distns中定义的,是scipy/stats/__init__中的主模块导入的。我相信该变量不会自动完成,因为它是在scipy.stats 的内部模块中定义的(_discrete_distns 具有underscore prefix),即在这种情况下,VS Code 将其视为“私有”。

我尝试了一些扩展,例如 IntelliCode 和 Pylance,但它仍然不能从 scipy/stats 自动完成 binom。我设法让它自动完成的唯一方法是直接导入scipy/stats/_discrete_distns

【讨论】:

    【解决方案2】:

    这是scipy.stats包下的__init__.py文件:

    from .stats import *
    from .distributions import *
    
    from .morestats import *
    from ._binomtest import binomtest
    from ._binned_statistic import *
    from .kde import gaussian_kde
    from . import mstats
    from . import qmc
    from ._multivariate import *
    from . import contingency
    from .contingency import chi2_contingency
    from ._bootstrap import bootstrap
    from ._entropy import *
    from ._hypotests import *
    from ._rvs_sampling import rvs_ratio_uniforms, NumericalInverseHermite
    from ._page_trend_test import page_trend_test
    from ._mannwhitneyu import mannwhitneyu
    
    __all__ = [s for s in dir() if not s.startswith("_")]  # Remove dunders.
    
    from scipy._lib._testutils import PytestTester
    
    test = PytestTester(__name__)
    del PytestTester
    

    它不包含:from ._discrete_distns import binom。所以 Pylance 不会在建议列表中提示。

    您可以通过from scipy.stats._discrete_distns import binom 像上面建议的bwdm 一样导入它。

    【讨论】:

    • 这看起来像是 Pylance 中的一个缺陷。由于历史原因,将名称 binom 导入到 scipy.stats 命名空间要经过几个间接级别(__init__.pydistributions.py 导入,distributions.py_discrete_distns.py 导入)。然而,最终结果是 binomscipy.stats 命名空间中的公共名称。 _discrete_distns.py 中的前导下划线正是为了避免让用户直接从该模块导入;由于是私有的,它可能会在未来的 SciPy 版本中发生变化。
    • 更明确地说:一般来说,您应该scipy.stats._discrete_distns导入。该私有模块的内容可以更改,或者模块本身可以在将来的任何时候重命名或删除。
    猜你喜欢
    • 1970-01-01
    • 2022-09-24
    • 2019-07-28
    • 2018-07-07
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多