AttributeError: 'NoneType' object has no attribute 'sc' 解决方法!

本文记录了一个Spark初学者在使用Python进行数据处理时遇到的问题及解决办法。博主在尝试读取文件并过滤含有“Python”的行时遇到了AttributeError错误,通过重新初始化SparkContext解决了这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自从解决了第一个问题,于是博主又开始了第二个代码了。
依旧说下:本人的操作系统为win7,python3,spark 1.6,anaconda3

代码如下:

lines = sc.textFile("D://Program Files//spark//spark-1.6.0-bin-hadoop2.6//README.md")
pythonLines = lines.filter(lambda line: "Python" in line)
print("过滤后文本的第一行:",pythonLines.first())
pythonLines.persist
print("过滤后文本的行数:",pythonLines.count())
sc.stop()

第一次run程序的结果如下:

过滤后文本的第一行: high-level APIs in Scala, Java, Python, and R, and an optimized engine that
过滤后文本的行数: 3

第二以及第N次run的结果如下:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-37c825a1b443> in <module>()
      1 
----> 2 lines = sc.textFile("D://Program Files//spark//spark-1.6.0-bin-hadoop2.6//README.md")
      3 pythonLines = lines.filter(lambda line: "Python" in line)
      4 print("过滤后文本的第一行:",pythonLines.first())
      5 pythonLines.persist

D:\spark\spark-1.6.0-bin-hadoop2.6\python\pyspark\context.py in textFile(self, name, minPartitions, use_unicode)
    471         [u'Hello world!']
    472         """
--> 473         minPartitions = minPartitions or min(self.defaultParallelism, 2)
    474         return RDD(self._jsc.textFile(name, minPartitions), self,
    475                    UTF8Deserializer(use_unicode))

D:\spark\spark-1.6.0-bin-hadoop2.6\python\pyspark\context.py in defaultParallelism(self)
    346         reduce tasks)
    347         """
--> 348         return self._jsc.sc().defaultParallelism()
    349 
    350     @property

AttributeError: 'NoneType' object has no attribute 'sc'

竟然报错了,而且报了个空对象没有sc的这个属性成员。这。。。完全不可能吧,于是又是一堆google、百度、stack overflow,依旧解决不了问题,最后灵机一动,它不是说空对象吗?那我直接给他初始化一个对象不就可以了吗?
于是代码如下:

sc = SparkContext("local", "Simple App") #初始化一个新的sc

lines = sc.textFile("D://Program Files//spark//spark-1.6.0-bin-hadoop2.6//README.md")
pythonLines = lines.filter(lambda line: "Python" in line)
print("过滤后文本的第一行:",pythonLines.first())
pythonLines.persist
print("过滤后文本的行数:",pythonLines.count())
sc.stop()

第一次以及第N次运行程序结果如下:
这里写图片描述

完美解决了这个错误!!
最后,希望我的错误记录能帮到未知的你!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值