首页 > 解决方案 > Pastebin pbwrap 在 python 3.9.5 上有 AttributeError: __enter__ with io.open()

问题描述

来自 pypi 的 pastebin 包 pbwrap 以前对我来说总是正常工作,但它不适用于 python 3.9.5。它无法读取输入文件并给出以下错误。

from pbwrap import Pastebin   # https://pypi.org/project/pbwrap/

api_dev_key = r'your key'
username = r'your login'
password = r'your password'
pb = Pastebin(api_dev_key)
user_id = pb.authenticate(username, password)

### Pasting text to pastebin works as expected.
paste_url = pb.create_paste('this is a test', api_paste_private=1, api_paste_name='test')
print(paste_url)

### Make a simple text file to upload to pastebin
with open('test.txt', 'w') as f:
    f.write('This is line #1\n')
    f.write('This is line #2\n')

### When uploading a file to pastebin, an I/O error is returned from io.open() called from pbwrap
paste_url = pb.create_paste_from_file('test.txt', api_paste_private=1, api_paste_name='test')
print(paste_url)

### pbwrap.py", line 180, in create_paste_from_file
###     with io.open(
### AttributeError: __enter__

标签: pythonpython-3.xpypi

解决方案


这原来是 2020 年模块升级为使用 utf-8 时引入的错误。我已经分叉了 pbwrap 源代码并提交了一个修复程序。需要将读取的文件移出 with io.open 的上下文。


推荐阅读