[英]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.
在项目文件夹中,我有另一个文件夹(lib),在该文件夹中,我有2个文件。 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.
问题是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
Setup below: 设置如下:
__init__.py:import req
main.py 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.
在项目文件夹中,我有另一个文件夹(lib),在该文件夹中,我有2个文件。
Should be 3. 应该是3。
Setup below: __init__.py:import req
设置如下:__init__.py:import req
Is this 3rd file or? 这是第3个文件还是?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.