Registry / data / pyspark

pyspark

JSON →
library4.2.0pypypi✓ verified 27d ago

PySpark is the Python API for Apache Spark, a unified analytics engine for large-scale data processing. It allows users to leverage Spark's powerful distributed computing capabilities, including Spark SQL, DataFrames, Structured Streaming, and MLlib, using familiar Python syntax. The library is actively maintained, with the current version being 4.1.1, and follows the release cadence of the broader Apache Spark project.

pip install pyspark
INSTALL
IMPORT
SIG · PYSPARK
P
pyspark
datapythonv4.2.0
Install
29.5s avg
Import
525ms
Disk
850MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.549s · 870.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 35.4s · import 0.501s · 839MB
850MB installed
● package 850MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SparkSession
from pyspark.sql import SparkSession
The primary entry point for PySpark functionality.
functions
from pyspark.sql import functions as F
Commonly imported for SQL-like functions such as col, sum, when, lit, etc.
Row
from pyspark.sql import Row
Used for creating DataFrames from lists of Row objects.
types
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
Used for explicitly defining DataFrame schemas.

This quickstart demonstrates how to initialize a SparkSession, create a DataFrame from Python data, display its schema and content, perform a filtering transformation, and then a grouping and aggregation. It also highlights the importance of setting `JAVA_HOME`.

import os from pyspark.sql import SparkSession from pyspark.sql.functions import col # PySpark requires JAVA_HOME to be set. Ensure it points to your JDK installation. # For example: os.environ['JAVA_HOME'] = '/usr/lib/jvm/java-11-openjdk-amd64' # Create a SparkSession - the entry point to Spark functionality spark = SparkSession.builder \ .appName("PySparkQuickstart") \ .getOrCreate() # Create a simple DataFrame data = [("Alice", 1), ("Bob", 2), ("Charlie", 3), ("David", 1)] columns = ["name", "value"] df = spark.createDataFrame(data, columns) # Show the DataFrame schema and data df.printSchema() df.show() # Perform a simple transformation (filter) and action (show) filtered_df = df.filter(col("value") > 1) print("Filtered DataFrame:") filtered_df.show() # Group by 'value' and count occurrences grouped_df = df.groupBy("value").count() print("Grouped DataFrame:") grouped_df.show() # Stop the SparkSession spark.stop()
pyspark --version
Debug
Known issues
breakingPySpark 4.0 dropped support for Python 3.8. Ensure your Python environment is 3.10 or higher.
fix
Upgrade your Python environment to 3.10 or newer. PySpark 4.0+ supports Python 3.10, 3.11, and 3.12.
affects: 4.0.0+
breakingThe minimum required Pandas version for PySpark 4.0+ was raised. If using the Pandas API on Spark, ensure your Pandas installation meets the new requirement (1.0.5 or higher).
fix
Upgrade Pandas in your environment: `pip install 'pandas>=1.0.5'`
affects: 4.0.0+
breakingIn PySpark 4.1, the Pandas API on Spark operates under ANSI mode by default. This might change behavior for certain operations, especially concerning null handling and type conversions.
fix
Review your code for potential changes in behavior related to ANSI SQL mode if relying on Pandas API on Spark. Refer to Spark documentation for specifics on ANSI mode implications.
affects: 4.1.0+
gotchaPySpark operations are lazily evaluated. Transformations (e.g., `filter`, `select`) do not execute immediately; computation only triggers when an action (e.g., `show`, `count`, `collect`, `write`) is called.
fix
Understand the lazy evaluation model. Use `df.explain()` to see the execution plan without triggering computation, and be aware that actions force execution.
affects: All versions
gotchaCalling `.collect()` on a large DataFrame can pull all distributed data to the driver node, potentially causing OutOfMemory (OOM) errors and crashing the application.
fix
Avoid `.collect()` for large datasets. Use `df.show()`, `df.take(N)`, `df.limit(N).toPandas()`, or write to distributed storage for inspecting data or small samples.
affects: All versions
gotchaApache Spark (and thus PySpark) requires a Java Development Kit (JDK) to be installed and the `JAVA_HOME` environment variable to be correctly set. Additionally, Spark's internal startup scripts often rely on common shell utilities (like `bash`). Failure to meet these requirements can prevent PySpark applications from launching, often resulting in a `JAVA_GATEWAY_EXITED` error.
fix
Install a compatible JDK (e.g., OpenJDK 8 or 11) and set the `JAVA_HOME` environment variable to point to your JDK installation directory. For minimal environments (e.g., Alpine), ensure that required shell utilities like `bash` are also installed (`apk add bash` for Alpine), as Spark's startup scripts may depend on them.
affects: All versions
gotchaIgnoring partitioning strategies for DataFrames can lead to data skew and inefficient shuffles, significantly degrading performance for wide transformations like `groupBy` or `join`.
fix
Analyze your data's distribution and use `df.repartition(N, *columns)` or `df.coalesce(N)` before wide transformations to optimize partitioning, especially for high-cardinality columns.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyspark'
PySpark is either not installed in the active Python environment or the Python interpreter being used does not have access to the PySpark installation.
fix
Install PySpark using pip: `pip install pyspark` or ensure your environment variables (like `PYTHONPATH`) correctly point to your PySpark installation if it's not a standard `pip` install.
KeyError: 'SPARK_HOME'
The `SPARK_HOME` environment variable, which points to the Apache Spark installation directory, is not set or is not correctly accessible by the PySpark script or interactive session.
fix
Set the `SPARK_HOME` environment variable to your Spark installation path. For example, on Linux/macOS: `export SPARK_HOME=/path/to/spark` or in a Python script using `import os; os.environ['SPARK_HOME'] = '/path/to/spark'`. If using `findspark`, call `findspark.init('/path/to/spark')`.
AttributeError: 'DataFrame' object has no attribute 'map'
You are attempting to use the `map()` transformation, which is an RDD (Resilient Distributed Dataset) method, directly on a PySpark DataFrame. DataFrames have different, more optimized, higher-level APIs for transformations.
fix
Convert the DataFrame to an RDD using `.rdd` before applying RDD transformations (e.g., `df.rdd.map(...)`) or use DataFrame-specific methods like `select()`, `withColumn()`, `filter()`, `udf()` for column-wise operations, which are generally more efficient.
java.lang.ClassNotFoundException: org.postgresql.Driver
This error typically occurs when Spark tries to load a Java class (like a JDBC driver or a custom data source) that is not available in its classpath. This happens frequently when connecting to databases or using external libraries like Delta Lake without providing the necessary JAR files.
fix
Include the required JAR file(s) in Spark's classpath. When running with `spark-submit`, use the `--jars` option (e.g., `spark-submit --jars postgresql-42.7.0.jar your_script.py`). When creating a `SparkSession`, configure it with `spark.jars` or `spark.driver.extraClassPath` properties.
AttributeError: 'SparkSession' object has no attribute 'parallelize'
`parallelize()` is a method of `SparkContext` used to create an RDD from a Python collection. You are trying to call this method directly on a `SparkSession` object, which does not expose this functionality directly.
fix
Access the `SparkContext` object from your `SparkSession` instance to use `parallelize()`. For example, if your `SparkSession` object is named `spark`, use `spark.sparkContext.parallelize(...)`.
Upgrade
Version history
4.2.0latest on PyPI · released Jul 14, 2026
Audit
Dependencies
Java Development Kit (JDK)requiredApache Spark, which PySpark interfaces with, requires a compatible JDK (version 8 or 11 recommended) to be installed and JAVA_HOME environment variable set.
PythonrequiredPySpark 4.0+ requires Python 3.10 or higher.
Agent activity
34 hits · last 30 days
node
28
OpenAI (training)
1
Resources
pyspark — pip install pyspark · libregistry