2017-02-14 189 views
5

在Python 3.5 Jupyter环境中运行以下代码时出现以下错误。任何想法是什么导致它?findspark.init()IndexError:列表索引超出范围错误

import findspark 
findspark.init() 

错误:

IndexError        Traceback (most recent call 
last) <ipython-input-20-2ad2c7679ebc> in <module>() 
     1 import findspark 
----> 2 findspark.init() 
     3 
     4 import pyspark 

/.../anaconda/envs/pyspark/lib/python3.5/site-packages/findspark.py in init(spark_home, python_path, edit_rc, edit_profile) 
    132  # add pyspark to sys.path 
    133  spark_python = os.path.join(spark_home, 'python') 
--> 134  py4j = glob(os.path.join(spark_python, 'lib', 'py4j-*.zip'))[0] 
    135  sys.path[:0] = [spark_python, py4j] 
    136 

IndexError: list index out of range 

回答

2

这很可能是由于SPARK_HOME环境变量未正确设置你的系统上。或者,你可以指定它,当你初始化findspark,就像这样:

import findspark 
findspark.init('/path/to/spark/home') 

之后,它应该所有的工作!

2

我得到了同样的错误,并能使其通过输入精确的安装目录工作:

import findspark 
# Use this 
findspark.init("C:\Users\PolestarEmployee\spark-1.6.3-bin-hadoop2.6") 
# Test 
from pyspark import SparkContext, SparkConf 

Basically, it is the directory where spark was extracted. In future where ever you see spark_home enter the same installation directory. I also tried using toree to create a kernal instead, but it is failing somehow. A kernal would be a cleaner solution.

0

您需要更新里面的.bash_profile的SPARK_HOME变量。 对于我来说,下面的命令(终端)工作:

export SPARK_HOME="/usr/local/Cellar/apache-spark/2.2.0/libexec/"

在此之后,你可以使用请按照下列命令:

import findspark 
findspark.init('/usr/local/Cellar/apache-spark/2.2.0/libexec') 
相关问题