在运行Python代码时尝试相对导入非包

0 投票
2 回答
529 浏览
提问于 2025-04-18 03:04

我刚开始学习Python,不太确定怎么运行这段代码。我遇到了以下错误:

mona$ python spectral.py 
Traceback (most recent call last):
  File "spectral.py", line 12, in <module>
    from ..base import BaseEstimator, ClusterMixin
ValueError: Attempted relative import in non-package


>>> from ..base import BaseEstimator, ClusterMixin, TransformerMixin
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package

我不太清楚还需要从这个库下载哪些文件,或者需要下载哪些包才能运行这段代码。

2 个回答

0

你需要把它当作一个整体来运行,而不仅仅是某个组件。

你有没有安装过sklearn这个包和它需要的其他东西?

如果还没有,你应该去看看这个页面,按照上面的步骤来安装和配置这个包。

1

你为什么想要运行那个脚本呢?

首先,你需要安装scikit-learn这个库。

sudo pip install scikit-learn

安装好之后,你就可以在你的Python脚本中导入光谱聚类功能了:

可以参考这个链接: http://scikit-learn.org/stable/auto_examples/cluster/plot_lena_segmentation.html#example-cluster-plot-lena-segmentation-py

还有这个链接:

http://scikit-learn.org/stable/auto_examples/cluster/plot_segmentation_toy.html#example-cluster-plot-segmentation-toy-py

撰写回答