【问题标题】:Python importing package as if it is a modulePython导入包就好像它是一个模块
【发布时间】:2021-12-24 03:44:21
【问题描述】:

我在 Python 中导入一个名为“scanpy”的包时遇到了很多麻烦 - 我一直在尝试在此处遵循本教程 (https://scanpy-tutorials.readthedocs.io/en/latest/pbmc3k.html),但 Python 似乎将“scanpy”视为一个模块包而不是包本身:

import numpy as np
import pandas as pd
import scanpy as sc
sc.settings.verbosity = 3
sc.logging.print_header()
sc.settings.set_figure_params(dpi=80, facecolor='white')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-129af3139565> in <module>
----> 1 sc.settings.verbosity = 3
      2 sc.logging.print_header()
      3 sc.settings.set_figure_params(dpi=80, facecolor='white')

AttributeError: module 'scanpy' has no attribute 'settings'

其他人似乎也无法安装“scanpy”,所以我在这个 GitHub 线程 (https://github.com/theislab/scanpy/issues/990) 上按照 kleurless 的安装说明进行操作。这是那些安装说明的结果。 (另外,我还遵循了“scanpy”网站上的安装说明,但这些说明似乎不起作用。)如果这很重要,我还在 Jupyter Notebook 中运行上述代码行。我在加载 Jupyter Notebook 之前忘记加载虚拟环境,所以我在第一行之前添加了“!source activate scanpy_environment”,但这没有帮助。如果有人可以帮助我,我将非常感激。

【问题讨论】:

  • 包是模块,其中包含子模块。就是这样。
  • 无论如何,您使用的是哪个版本的scanpy?这可能是相关的:github.com/theislab/scanpy-tutorials/issues/1
  • 您的工作目录中是否有一个名为scanpy.py 的文件?即,如果您在 jupyter 笔记本中使用 !ls?
  • 感谢您的 cmets @juanpa.arrivillaga,我不太确定它是什么版本的“scanpy” - 我尝试了“sc.__version__”,但没有返回任何内容。我也试过“!conda list”,但“scanpy”没有出现在那里,所以我不太确定我的虚拟环境是否正确激活
  • 针对此类问题的标准调试:print(sc.__file__) 查看模块的确切来源,print(dir(sc)) 查看其中的实际内容。

标签: python scanpy


【解决方案1】:

我刚刚在我的备份测试 ubuntu 计算机上安装了它,它可以工作。

我做到了: pip install scanpy 在 bash 终端中 然后一旦安装,我就编写了这个测试脚本并运行它:

import scanpy as sc

sc.settings.verbosity = 3
sc.logging.print_header()
sc.settings.set_figure_params(dpi=80, facecolor='white')

print(sc.settings.verbosity)
print("test")

如果有效,则可能是 Jupyter notebook 的问题。

我还想说使用pip freeze 检查您的模块,看看是否安装了scanpy。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    相关资源
    最近更新 更多