简体   繁体   中英

Python module not working if called from main?

I have a project going. In the project folder, I have another folder (lib) and inside that folder I have 2 files. The contents of each is below. The problem is that req.py runs perfectly by itself, but when I run main.py, I get the following error.

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

Setup below:
__init__.py:import req

main.py

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

req.py

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()

I am entirely out of ideas. Is there any explanation for what's causing this?

In the project folder, I have another folder (lib) and inside that folder I have 2 files.

Should be 3.

Setup below: __init__.py:import req

Is this 3rd file or?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM