没有模块名称pyspark错误

问题描述 投票:0回答:9

这是我正在遵循的教程中的确切代码。我的同学使用相同的代码没有收到此错误:

ImportError                                Traceback (most recent call last)

<ipython-input-1-c6e1bed850ab> in <module>()
----> 1 from pyspark import SparkContext
      2 sc = SparkContext('local', 'Exam_3')
      3 
      4 from pyspark.sql import SQLContext
      5 sqlContext = SQLContext(sc)

ImportError: No module named pyspark

这是代码:

from pyspark import SparkContext
sc = SparkContext('local', 'Exam_3')
from pyspark.sql import SQLContext    
sqlContext = SQLContext(sc)
data = sc.textFile("exam3")
parsedData = data.map(lambda line: [float(x) for x in line.split(',')])
retail = sqlContext.createDataFrame(parsedData, 
     ['category_name','product_id', 'product_name', 'product_price'])
retail.registerTempTable("exam3")
print parsedData.take(3)
python pyspark
9个回答
34
投票

您没有将

pyspark
安装在您正在使用的 python 安装可用的位置。要确认这一点,请在命令行终端上激活
virtualenv
后,输入 REPL (
python
) 并输入
import pyspark
:

$ python
Python 3.5.0 (default, Dec  3 2015, 09:58:14) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyspark
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'pyspark'

如果您看到

No module name 'pyspark'
ImportError,则需要安装该库。退出 REPL 并输入:

pip install pyspark

然后重新输入repl以确认其有效:

$ python
Python 3.5.0 (default, Dec  3 2015, 09:58:14) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyspark
>>>

请注意,激活虚拟环境至关重要。当位于虚拟环境的目录中时:

$ source bin/activate

这些说明适用于基于 UNIX 的计算机,对于 Windows 会有所不同。


20
投票

只需使用:

import findspark
findspark.init()

import pyspark # only run after findspark.init()

如果您没有findspark模块,请安装它:

python -m pip install findspark

13
投票

您可以使用

findspark
使 Spark 在运行时可访问。 通常
findspark
会找到你安装spark的目录,但如果它安装在非标准位置,你可以将其指向正确的目录。安装完
findspark
后,如果spark安装在
/path/to/spark_home
,只需放置

import findspark
findspark.init('/path/to/spark_home')

在脚本/笔记本的最顶部,您现在应该能够访问 pyspark 模块。


2
投票

这是适用于我的最新解决方案,仅适用于 MAC 用户。我已经通过

pip install pyspark
安装了pyspark。但是,当我在终端中甚至在 python import pyspark 中执行
pyspark
时,它不起作用。我检查了 pyspark 已经安装在我的笔记本电脑中。

最后,我找到了解决方案。您只需添加到 bash 配置文件中即可。

按照步骤:

1) 在终端窗口中键入以下内容以转到您的主文件夹。

cd ~

2)然后按照以下步骤创建.bash_profile。 (如果已经存在,可以跳过。)

touch .bash_profile

3)

open -e .bash_profile

然后添加以下变量。

export SPARK_VERSION=`ls /usr/local/Cellar/apache-spark/ | sort | tail -1`
export SPARK_HOME="/usr/local/Cellar/apache-spark/$SPARK_VERSION/libexec"
export PYTHONPATH=$SPARK_HOME/python/:$PYTHONPATH
export PYTHONPATH=$SPARK_HOME/python/lib/py4j-0.10.7-src.zip:$PYTHONPATH

您需要更改最后一行中的 py4j-x.x.x-src.zip 版本号

4)分配所有这些变量后,保存并关闭.bash_profile。然后输入以下命令来重新加载文件。

. .bash_profile


1
投票

我在

之后遇到了这个错误
conda install pyspark

然后我

pip install pyspark

这会自动安装py4j,然后就正常了


0
投票

确保首先使用 conda 安装 pyspark :

conda install pyspark

0
投票

导入findspark findspark.init()

回溯(最近一次调用最后一次): 文件“”,第 1 行,位于 导入错误:没有名为“findspark”的模块

$ pip install findspark

会起作用的


0
投票

我解决这个问题的方法是

  • 通过以下方式在本地计算机上打开一个全新的 .ipynb:
$jupyter-lab
  • 接下来在第一个运行此的单元格中:

$pip 安装 pyspark

我的输出是:

Collecting pyspark   
  Using cached pyspark-3.2.0.tar.gz (281.3 MB)
  Preparing metadata (setup.py) ... done 
Collecting py4j==0.10.9.2
  Using cached py4j-0.10.9.2-py2.py3-none-any.whl (198 kB) 
Building wheels for collected packages: pyspark   
  Building wheel for pyspark (setup.py) ... done   
  Created wheel for pyspark: filename=pyspark-3.2.0-py2.py3-none-any.whl size=281805913 sha256=26e539058858454dbbb48158111968d67e663c7b53e64c4fd91e38d92ac1cd80 
  Stored in directory: /Users/user/Library/Caches/pip/wheels/2f/f8/95/2ad14a4614b4a9f645ee928fbbd057b1b254c67adb494c9a58 
Successfully built pyspark Installing collected packages: py4j, pyspark 
Successfully installed py4j-0.10.9.2 pyspark-3.2.0 
Note: you may need to restart the kernel to use updated packages.
  • 然后导入pyspark:

$导入pyspark

您可能还想尝试直接在实验室环境中运行 pip 命令。


0
投票

以防万一,如果您在使用 delta-spark 模块时来到这里,请检查 sparkdelta-spark 软件包的版本兼容性。 确保您使用的 PySpark 和 Delta Lake 库的版本兼容。

  • PySpark 3.2.x:Delta Lake 1.1.x
  • PySpark 3.3.x:Delta Lake 2.x

例如:

pip install pyspark==3.3.0
pip install delta-spark==2.3.0
© www.soinside.com 2019 - 2024. All rights reserved.