While setting up PySpark to run with Spyder, Jupyter, or PyCharm on Windows, macOS, Linux, or any OS, we often get the error “py4j.protocol.Py4JError: org.apache.spark.api.python.PythonUtils.getEncryptionEnabled does not exist in the JVM“
Below are the steps to solve this problem.
Solution 1. Check your environment variables
You are getting “py4j.protocol.Py4JError: org.apache.spark.api.python.PythonUtils.getEncryptionEnabled does not exist in the JVM
” due to Spark environemnt variables are not set right.
Check if you have your environment variables set right on .<strong>bashrc</strong>
file. For Unix and Mac, the variable should be something like below. You can find the .bashrc file on your home path.
Note: Do not copy and paste the below line as your Spark version might be different from the one mentioned below.
export SPARK_HOME=/opt/spark-3.0.0-bin-hadoop2.7
export PYTHONPATH=$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.10.9-src.zip:$PYTHONPATH
export PATH=$SPARK_HOME/bin:$SPARK_HOME/python:$PATH
For Linux or Mac users, vi ~/.bashrc
, add the above lines and reload the bashrc file using source ~/.bashrc
If you are running on windows, open the environment variables window, and add/update below environments.
SPARK_HOME => /opt/spark-3.0.0-bin-hadoop2.7
PYTHONPATH => %SPARK_HOME%/python;%SPARK_HOME%/python/lib/py4j-0.10.9-src.zip;%PYTHONPATH%
PATH => %SPARK_HOME%/bin;%SPARK_HOME%/python;%PATH%
After setting the environment variables, restart your tool or command prompt.
Solution 2. Using findspark
Install findspark
package by running $pip install findspark
and add the following lines to your pyspark program.
import findspark
findspark.init()
# you can also pass spark home path to init() method like below
# findspark.init("/path/to/spark")
Solution 3. Copying the pyspark and py4j modules to Anaconda lib
Sometimes after changing/upgrading the Spark version, you may get this error due to the version incompatible between pyspark version and pyspark available at anaconda lib. In order to correct it do the following.
Note: copy the specified folder from inside the zip files and make sure you have environment variables set right as mentioned in the beginning.
Copy the py4j folder from C:\apps\opt\spark-3.0.0-bin-hadoop2.7\python\lib\py4j-0.10.9-src.zip\ to C:\Programdata\anaconda3\Lib\site-packages\.
And, copy pyspark folder from C:\apps\opt\spark-3.0.0-bin-hadoop2.7\python\lib\pyspark.zip\ to C:\Programdata\anaconda3\Lib\site-packages\
You may need to restart your console some times even your system in order to affect the environment variables.
When I upgraded my Spark version, I was getting this error, and copying the folders specified here resolved my issue.
Hope this resolves your issue as well.
Happy Learning !!
Related Articles
- How to Spark Submit Python | PySpark File (.py)?
- Spark Submit Command Explained with Examples
- Spark – Initial job has not accepted any resources; check your cluster UI
- Spark Exception: Python in worker has different version 3.4 than that in driver 2.7, PySpark cannot run with different minor versions