Solving Spark in Pycharm error – Cannot run program “python”

Spark in Pycharm error – Cannot run program “python” is an error which occurs because the environment variables for python and pySpark are not set properly.

In the blog post I attempt to explain why this error takes place and how you can solve it, I will also add other solutions that could solve the error if possible.

Exploring the Error : Spark in Pycharm error – Cannot run program “python”

This is an error which occurs because the environment variables for python and pySpark are not set properly.

Please double check so you can avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0)
java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
                                                                       #

In the sections bellow we will explain the root of the error more and propose some possible fixes.

Solution : Set the environment variables for PySpark and Python

The solution is simple, the environment variables for python and PySpark should be set properly.

First, in windows go to the bottom left and in the search bar type Environment Variables.

Click on the icon, and then click Environment Variables at the bottom.

Now, look for path, then click edit and a list should appear

Add the first PySpark environment variable

                                                                       #
C:/Spark/spark-3.3-bin-hadoop2.6/python/pyspark
                                                                       #

And then add the python environment variable

                                                                       #
C:\Python39
                                                                       #

The directories above should point to where PySpark and python are installed.

If the above does not work, please try adding three environment variables: PYTHONPATH , SPARK_HOME and SPARK_PYTHONPATH.

All three of them should point to the location of python.exe is installed. Like this fore example.

                                                                       #
SPARK_HOME=C:\Python39\python.exe
                                                                       #
                                                                       #
SPARK_PYTHONPATH=C:\Python39\python.exe
                                                                       #
                                                                       #
PYTHONPATH=C:\Python39\python.exe
                                                                       #

The solution above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/