avatarSteven Gong

Summary

The web content provides a comprehensive guide on setting up PySpark in the PyCharm IDE for both macOS and Windows users, detailing the installation process and integration with the IDE.

Abstract

The article "How to use PySpark in PyCharm IDE" outlines the necessary steps to install and configure PySpark, the Python API for Apache Spark, on macOS and Windows systems. It emphasizes the importance of having Java 8 installed, as Spark is only compatible with this version. For macOS, the guide instructs users to install Apache Spark via Homebrew and to update their .bash_profile with the appropriate environment variables. For Windows, the process involves downloading Spark from the official website, moving it to a designated folder, and unzipping the package. Both platforms require adding the Spark Python executable to the system's PATH and configuring PyCharm's project structure to include the Spark installation. The article also references additional resources for installing Python and using the vim text editor.

Opinions

  • The author suggests that Python 3.7 is the preferred version for use with PySpark.
  • The author recommends a specific tutorial from Datacamp for those interested in choosing the right language for Spark development.
  • The author highlights the importance of using Java 8 and provides a link to a tutorial for installing Java on macOS.
  • The author provides a personal touch by showing an image of their own .bash_profile configuration.
  • The author acknowledges potential issues with running PySpark, such as the need to add environment variables or the possibility of a PY4J error due to incorrect Java versions, and offers troubleshooting steps.

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-spark

After 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/bin

To save and exit vim, use the command “:x” and press enter.

This is what my bash_profile looks like

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 pyspark

should 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

  1. 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.tgz

Now, 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.tgz

3. Use both commands in terminal to unzip the file

gzip -d spark-2.4.4-bin-hadoop2.7.tgz
tar xvf spark-2.4.4-bin-hadoop2.7.tar

4. Adding the PATHS to be able to call PySpark directly from CMD

setx SPARK_HOME C:\opt\spark\spark-2.4.4-bin-hadoop2.7
setx PYSPARK_DRIVER_PYTHON python

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 “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 pyspark

should 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).

Spark
Pyspark
Pycharm
Technical
Recommended from ReadMedium