base库中b64encode报错

AttributeError: partially initialized module 'base64' has no attribute 'b64encode' (most likely due to a circular import)

该回答引用GPTᴼᴾᴱᴺᴬᴵ
这个错误通常表示在导入base64模块时出现了循环引用的问题,可能是由于代码中的导入方式不正确所致。

解决此问题的一种方法是将导入方式改为使用模块名来引用函数,而不是直接从模块中导入函数。例如,将代码中的from base64 import b64encode改为import base64,然后使用base64.b64encode()来调用函数。

您可以尝试使用以下代码来解决这个问题:

import base64

message = "Hello, world!"
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')

print(base64_message)


这将使用base64模块来对消息进行编码,并打印出编码后的结果。
·
另外,您还可以尝试检查您的代码中是否存在导入循环依赖的问题。如果您在不同的模块之间进行导入,可能会出现循环依赖的问题。为了避免这种情况,请确保您的代码结构清晰,并且避免在不同的模块之间进行过多的交叉导入。

(most likely due to a circular import)
这是循环引用了。
400