错误信息:TypeError: cannot pickle 'module' object

问题描述

访问Swagger提示错误,错误信息如下

代码报错信息

127.0.0.1 - - [16/Nov/2021 11:19:27] "GET /openapi.json HTTP/1.1" 500 -
Traceback (most recent call last):
  File "E:\Env\TestFlask\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "E:\Env\TestFlask\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "E:\Env\TestFlask\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "E:\Env\TestFlask\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "E:\Env\TestFlask\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "E:\Env\TestFlask\Lib\site-packages\apiflask\app.py", line 386, in dispatch_request
    return view_function(*req.view_args.values())  # type: ignore
  File "E:\Env\TestFlask\Lib\site-packages\apiflask\app.py", line 520, in spec
    response = jsonify(self._get_spec('json'))
  File "E:\Env\TestFlask\Lib\site-packages\apiflask\app.py", line 591, in _get_spec
    self._spec = self._generate_spec().to_dict()
  File "E:\Env\TestFlask\Lib\site-packages\apiflask\app.py", line 1087, in _generate_spec
    spec.path(path=path, operations=sorted_operations)
  File "E:\Env\TestFlask\Lib\site-packages\apispec\core.py", line 433, in path
    operations = deepcopy(operations) or OrderedDict()
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "D:\Language\Python\Python3.8\Lib\copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle 'module' object
from TestFlask.module.dto import IDReq
from TestFlask.module.dto.BugData import BugReq, BugRes
from TestFlask.service.BugService import BugService
from apiflask import APIBlueprint, input, output
from flask import request, jsonify

bug = APIBlueprint("Bug", __name__)
bugService = BugService()


@bug.post('/bugInfo')
@input(BugReq)
@output(BugRes)
def userInfo(data):
    return jsonify(bugService.bugQueryService(request.json))


@bug.post('/bugAdd')
@input(BugReq)
@output(BugRes)
def bugAdd(data):
    return jsonify(bugService.bugAddService(request.json))

#这个方法引起的报错,把这个注释掉,就正常访问
@bug.delete('/bugDel')
@input(IDReq)
@output(BugRes)
def bugDel(data):
    return jsonify(bugService.bugDelService(request.json))# 确定这个方法没问题

代码没动,现在好了。。。

IDReq 和 BugRes schema 方便提供吗?

我找到问题了,是引入包的问题

from TestFlask.module.dto import IDReq #这里的IDReq其实是个Python文件

实际上应该是

from TestFlask.module.dto.IDReq  import IDReq 

报错原因应该是swagger拿schema文件的时候,没有拿到导致的

回复在下面,是引入包的问题