簡體   English   中英

如果從main調用,Python模塊不起作用?

[英]Python module not working if called from main?

我有一個項目要進行。 在項目文件夾中,我有另一個文件夾(lib),在該文件夾中,我有2個文件。 每個的內容如下。 問題是req.py本身可以完美運行,但是當我運行main.py時,出現以下錯誤。

Traceback (most recent call last):
  File "C:\Users\redacted\main.py", line 5, in <module>
    req.req()
  File "C:\Users\redacted\lib\req.py", line 13, in req
    return r.content
  File "C:\Python27\lib\site-packages\requests\models.py", line 595, in content
    content = decode_gzip(self._content)
  File "C:\Python27\lib\site-packages\requests\utils.py", line 354, in decode_gzip
    return zlib.decompress(content, 16 + zlib.MAX_WBITS)
TypeError: must be string or read-only buffer, not None

設置如下:
__init__.py:import req

main.py

import lib as n
if __name__ == "__main__":
    req = n.req.req()
    req.req()

要求

import requests

class req():
    def __init__(self):
        ua     = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2'
        self.w = requests.session( headers = { 'User-Agent': ua } )

    def req(self):
        r = self.w.get('http://www.google.com')
        return r.content

if __name__ == "__main__":
    r = req()
    print r.req()

我完全沒有主意。 有什么原因的解釋嗎?

在項目文件夾中,我有另一個文件夾(lib),在該文件夾中,我有2個文件。

應該是3。

設置如下:__init__.py:import req

這是第3個文件還是?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM