Jupyter ImportError: No module named py4j.protocol despite py4j is installed

Jupyter ImportError: No module named py4j.protocol despite py4j is installed

我阅读了一些关于我现在在导入 pyspark 时看到的错误的帖子,一些建议 install py4j,我已经这样做了,但我仍然看到错误。

I am using a conda environment, here is the steps:
1. create a yml file and include the needed packages (including the py4j)
2. create a env based on the yml
3. create a kernel pointing to the env
4. start the kernel in Jupyter
5. running `import pyspark` throws error: ImportError: No module named py4j.protocol

通过在 kernel.json 中添加环境部分并明确指定以下变量解决了该问题:

 "env": {
  "HADOOP_CONF_DIR": "/etc/spark2/conf/yarn-conf",
  "PYSPARK_PYTHON":"/opt/cloudera/parcels/Anaconda/bin/python",
  "SPARK_HOME": "/opt/cloudera/parcels/SPARK2",
  "PYTHONPATH": "/opt/cloudera/parcels/SPARK2/lib/spark2/python/lib/py4j-0.10.7-src.zip:/opt/cloudera/parcels/SPARK2/lib/spark2/python/",
  "PYTHONSTARTUP": "/opt/cloudera/parcels/SPARK2/lib/spark2/python/pyspark/shell.py",
  "PYSPARK_SUBMIT_ARGS": " --master yarn --deploy-mode client pyspark-shell"
 }