How to use PySpark in PyCharm IDE
I have recently been exploring the world of big data and started to use Spark, a platform for cluster computing (i.e. allows the spread of data and computations over clusters with multiple nodes (think of each node as a separate computer)).
However, Spark can be used in 3 main languages, Scala, Python and Java. If you are curious as to which language to use, check out this great article by Datacamp https://www.datacamp.com/community/tutorials/apache-spark-python
We will be download PySpark, the Python API for Spark.
Pre-requisites: -python3.7 installed on your computer (https://realpython.com/installing-python/ for tutorial) -VERY IMPORTANT: Java 8 installed since Spark only runs on Java 8, not Java 11 For Mac: Homebrew installed (https://realpython.com/installing-python/#step-1-install-homebrew-part-1)
Setting up in Mac
Part 1: Installing PySpark
Open up your terminal, and run the command:
brew install apache-sparkAfter the installation is completed, try writing in terminal
pyspark the following should appear:

If the command does not work, you will have a few additional steps:
vim ~/.bash_profile
Add these 2 lines to your bash_profile. If you have never used vim, check out this tutorial https://www.howtoforge.com/vim-basics.
SPARK_HOME=”/usr/local/Cellar/apache-spark/2.4.4/libexec”
export PATH=$PATH:$SPARK_HOME/binTo save and exit vim, use the command “:x” and press enter.

These 2 extra lines of code should allow you to directly call pyspark from terminal.
Part 2: Connecting PySpark to Pycharm IDE
Open up any project where you need to use PySpark
To be able to run PySpark in PyCharm, you need to go into “Preferences” and “Project Structure” to “add Content Root”, where you specify the location of the python executable of apache-spark.

Press “Apply” and “OK” after you are done.
Relaunch Pycharm and the command
import pysparkshould be able to run within the PyCharm console.
Setting up in Windows
It is different from Mac since Windows does not operate on Homebrew.
Part 1: Installing PySpark on your computer
- Install Apache Spark from http://spark.apache.org/downloads.html in your downloads folder

2. move the file to the appropriate location
mv C:\Users\yourusername\Downloads\spark-2.4.4-bin-hadoop2.7.tgz C:\opt\spark\spark-2.4.4-bin-hadoop2.7.tgzNow, Spark is no longer located in your downloads folder, but at C:\opt\spark\spark-2.4.4-bin-hadoop2.7.tgz
Hence, in terminal,
cd C:\opt\spark\spark-2.4.4-bin-hadoop2.7.tgz3. Use both commands in terminal to unzip the file
gzip -d spark-2.4.4-bin-hadoop2.7.tgztar xvf spark-2.4.4-bin-hadoop2.7.tar4. Adding the PATHS to be able to call PySpark directly from CMD
setx SPARK_HOME C:\opt\spark\spark-2.4.4-bin-hadoop2.7setx PYSPARK_DRIVER_PYTHON pythonPart 2: Connecting PySpark to Pycharm IDE
Open up any project where you need to use PySpark
To be able to run PySpark in PyCharm, you need to go into “Settings” and “Project Structure” to “add Content Root”, where you specify the location of the python file of apache-spark.

Press “Apply” and “OK” after you are done. Relaunch Pycharm and the command
import pysparkshould be able to run within the PyCharm console.
Note: If you obtain a PY4J missing error, it may be due to your computer running on the wrong version of Java (i.e. Spark only runs on Java 8 but you may have Java 11 installed).




