我正在运行此导入语句
from pyspark.ml.feature import VectorAssembler
这是完整的回溯:
ModuleNotFoundError Traceback (most recent call last)
Cell In[5], line 1
----> 1 from pyspark.ml.feature import VectorAssembler
File /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/pyspark/ml/__init__.py:22
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one or more
3 # contributor license agreements. See the NOTICE file distributed with
(...)
15 # limitations under the License.
16 #
18 """
19 DataFrame-based machine learning APIs to let users quickly assemble and configure practical
20 machine learning pipelines.
21 """
您发布的回溯表明 pyspark 不在您的 python 环境中。
首先,如果您之前在 jupyter 实验室中成功使用了 pyspark,请确保您使用的是相同的内核。
如果不是这样,您可以通过以下方式排除 pyspark 安装丢失或损坏的情况:
pip uninstall pyspark
pip install pyspark
如果您确定已安装 pyspark,请考虑安装和使用 findspark (
pip install findspark
),它会在运行时自动查找 pyspark 并将其添加到您的 sys.path 中。
import findspark
findspark.init()
from pyspark.ml.feature import VectorAssembler
如果一切都失败了,你可以创建一个新的Python环境,其中包含pyspark。这个过程并不是 Jupyter Lab 所独有的,任何 Jupyter Notebook 教程都会为您指明正确的方向。根据回溯中的
/Library/Frameworks
行,我假设您使用的是 OS X:
python -m venv MY_ENVIRONMENT_NAME
source MY_ENVIRONMENT_NAME/bin/activate
pip install pyspark
pip install ipykernel
python -m ipykernel install --user --name MY_KERNEL_NAME
根据需要替换
MY_ENVIRONMENT_NAME
和 MY_KERNEL_NAME
。之后,新环境将显示在您的内核列表中。选择它,然后就可以开始了。