This happens because when you type “pyspark” in the terminal, the system automatically initialized the SparkContext (maybe a Object?), so you should stop it before creating a new one.
You can use
sc.stop()
before you create your new SparkContext.
Also, you can use
sc = SparkContext.getOrCreate()
instead of
sc = SparkContext()
I am new in Spark and I don’t know much about the meaning of the parameters of the function SparkContext() but the code showed above both worked for me.