简体   繁体   English

如果从main调用,Python模块不起作用?

[英]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.

相关问题 GAE / Python-从主类重定向工作,而不是从调用方法 - GAE/Python - redirect working from main class but not from called method 在主要的Python安装中导入模块时出错:从不存在的venv Python调用模块 - Error when importing module in main Python installation: module is called from an inexistent venv Python Python线程仅在从另一个模块调用时才有效 - Python threading only working when called from another module 如何访问从主模块调用的模块范围的变量? - How to access to the variables of the scope of a module called from the main module? 从python中的外部模块调用主要方法 - Call main methods from a outside module in python Obspy.read() 从 mod_python apache 模块调用时不起作用 - Obspy.read() not working when called from mod_python apache module Python从调用的模块获取返回的对象 - Python Get Returned Object From Called Module 从 VBA 调用的 Python 脚本从 Python 调用不工作 - Python script called from VBA called from Python is not working 从Python中的导入模块替换主模块中的函数 - Replace function in main module from imported module in Python 如何在Python中从被调用模块访问调用模块名称空间? - How to access to calling module namespace from called module in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM