In Part 3, you will install and test PySpark locally. This part is optional if you prefer to keep using Google Colab.
You will:
- check that Python is installed
- check that Java is installed
- create a local virtual environment
- install PySpark from
requirements.txt - run a small local PySpark script
- troubleshoot common setup problems
Before starting:
- Open the
session8folder in Visual Studio Code. - Open a terminal from inside
session8. - Confirm your path:
pwdIt should end with:
/bda/session8- PySpark runs Python code that communicates with Spark on the Java Virtual Machine.
- Java must be available for local Spark.
- A virtual environment keeps project dependencies separate.
- Colab handles most setup for you; local setup gives you more control.
Run:
python3 --version
java -versionExpected idea:
Python prints a 3.x version.
Java prints a version and does not say command not found.Note
Current PySpark releases require a modern Python version and Java 17 or later. If Java is missing, install a current LTS Java release before continuing.
macOS or Linux:
python3 -m venv .venv
source .venv/bin/activateWindows PowerShell:
python -m venv .venv
.venv\Scripts\Activate.ps1Run:
pip install -r requirements.txtThis installs:
pyspark==4.1.2
quizmdCreate this file:
session8/solutions/exercise-08-03.pyAdd:
from pyspark.sql import SparkSession
from pyspark.sql.functions import avg
spark = (
SparkSession.builder
.appName("Session08LocalCheck")
.master("local[*]")
.getOrCreate()
)
data = [
("Athens", "Electronics", 1200.00),
("Athens", "Office", 35.00),
("Patras", "Furniture", 340.00),
("Heraklion", "Electronics", 620.00),
]
df = spark.createDataFrame(data, ["city", "category", "revenue"])
df.show()
df.groupBy("category").agg(avg("revenue").alias("average_revenue")).show()
spark.stop()Run:
python3 solutions/exercise-08-03.pyMinimum completion checklist:
- The script starts Spark.
- A table prints with city, category, and revenue.
- A grouped average prints by category.
- The script exits without leaving Spark running.
If java is not found:
Install Java 17 or later, then open a new terminal.If PySpark cannot start:
Check that Java works with java -version.
Check that your virtual environment is activated.
Run pip install -r requirements.txt again.If Python cannot find your file:
Run the script from inside the session8 folder.quizmd quizzes/python-session-08-part-03-quiz.md