【问题标题】:python __init__ error on Windows only仅在 Windows 上出现 python __init__ 错误
【发布时间】:2012-11-13 21:38:03
【问题描述】:

我对python很陌生...

 import sys, os, time, py4chan, urllib from urllib2 import urlopen, URLError, HTTPError

 def refreshthread(boardin,no):
     global thread
     global topic
     board = py4chan.Board(boardin)
     thread = board.getThread(int(no))
     topic = thread.topic
     time.sleep(2.5)

 def dlfile(url, folder):

     try:
         f = urlopen(url)

         with open(folder + "/" + os.path.basename(url), "wb") as local_file:
             local_file.write(f.read())
             print "Downloaded to " + str(folder + "/" + os.path.basename(url)) 

     except HTTPError, e:
         print "HTTP Error:", e.code, url
     except URLError, e:
         print "URL Error:", e.reason, url

 def getsize(uri):
     file = urllib.urlopen(uri)
     size = file.headers.get("content-length")
     file.close()
     return str(int(size) / 1024)

 def main():
     boardtag = str(raw_input("Board: "))
     threadno = int(raw_input("Thread id: "))
     folder = str(raw_input("Save to folder: "))
     print "Getting thread information..."
     refreshthread(boardtag,threadno)
     print "Subject: " + topic.Subject
     while(True):
         if not os.path.exists(folder): os.makedirs(folder)
         refreshthread(boardtag,threadno)
         for imgurl in thread.Files():
             if imgurl is not None and not os.path.exists(folder + "/" + os.path.basename(imgurl)):
                 print "A wild image appears! " + "(" + getsize(imgurl) + "kb)" 
                 dlfile(imgurl,folder)
             else:
                 pass

 if __name__ == '__main__':
     main()

我在 linux 上编写了这个代码,它运行得很好,但是如果我在 windows 上运行它,我会得到这个错误:

TypeError: __init__() takes exactly 4 arguments (2 given)

这很奇怪,因为我没有定义 init。 这可能是来自另一个模块的 init 吗?

如果我编写其他脚本,py4chan 模块似乎工作正常。 两台机器也有相同的python版本。

编辑(完全错误):

Getting thread information...
Traceback (most recent call last):
  File "4chan.py", line 59, in <module>
    main()
  File "4chan.py", line 46, in main
    refreshthread(boardtag,threadno)
  File "4chan.py", line 15, in refreshthread
    board = py4chan.Board(boardin)
TypeError: __init__() takes exactly 4 arguments (2 given)

*编辑:*好吧,我有两个不同的同名模块。现在都在工作。 我不应该被允许在这个网站上。

【问题讨论】:

  • 粘贴整个错误消息,其中包括文件和行号。
  • 你可能有不同版本的 py4chan 模块
  • 基于 @JBernardo 的评论,看起来 .Board()` 调用的一个版本可能没有使用默认参数 - 我会查看 Board__init__ 方法.
  • 绝对正确的 JBernado。我什至没有想过要从同一个站点获取模块。傻我。

标签: python windows init


【解决方案1】:

py4chan.Board 类需要用 3 个参数构造,而不是 1 个。refreshthread 函数中的这一行:

board = py4chan.Board(boardin)

应该是这样的:

board = py4chan.Board(base_url, post_url, filesize)

【讨论】:

  • 好的,这是有道理的。我遇到了这个问题,因为 py4chan.Board 类与“官方”py4chan 模块不同。我实际上在一个完全不同的模块上构建了脚本。
【解决方案2】:

乍一看,您似乎在两个地方硬编码了/ 作为目录分隔符。在 Windows 中,目录分隔符为 \

【讨论】:

  • 没错,虽然大部分的Windows也能识别/,而且不会导致这个特定的错误。
【解决方案3】:

我刚刚从http://py4chan.sourceforge.net/ 抓取的py4chan 模块对Board.__init__ 有这个定义:

class Board:
    def __init__(self, base_url, post_url, filesize):

这需要 4 个参数(包括隐含的 self),因此您缺少 post_urlfilesize 参数(不管它们是什么)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2012-06-05
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多