Forum Discussion
spark NameError when using an imported function needing SparkContext
- 1 year ago
When you develop locally, your spark.py file explicitly creates a SparkSession using SparkSession.builder. This works because you control the full Python environment and are expected to instantiate Spark manually.
However, in Microsoft Fabric Notebooks, a SparkSession is already created and provided implicitly by the runtime. You can access it simply via the spark object, but you should not re-instantiate or create a new SparkSession.
Don't define your own spark.py moduleRename your module to something else like spark_utils.py or file_io.py. This avoids shadowing the built-in spark object.
Use the implicit spark from Fabric
Remove the following from your code entirely:
CopyEdit from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName("GlobalSpark") \ .master("local[*]") \ .getOrCreate()Instead, in your utility function, rely on the pre-provided spark:
CopyEdit def get_file(outputFilePath: str): df = spark.read.option("multiline", "true").json(outputFilePath) return df
Avoid import spark altogetherIf you must package utilities, structure it like this:
CopyEdit # file_io.py def get_file(outputFilePath: str): from pyspark.sql import SparkSession spark = SparkSession.getActiveSession() if spark is None: raise RuntimeError("No active SparkSession found. This function must be run within a Spark environment.") df = spark.read.option("multiline", "true").json(outputFilePath) return dfBut in Fabric, SparkSession.getActiveSession() should return the running session just fine.
Please mark this post as solution if it helps you. Appreciate Kudos.
Hello Andrew,
Thank you for your reply.
Indeed, I performed the following actions :
- I removed the spark.py utility from my package
- I added this to my function :
spark = SparkSession.getActiveSession()
Once these 2 actions performed, Fabric recognizes spark and doesn't throw a NameError.
However...
If i don't explicitly use the 'getActiveSession' function, then the problem still persists.
How come we have no direct access to the SparkSession(spark) without defining it first?
Thanks so much for your help so far !
Kind regards,
Anissa