我是 Python 新手。尝试使用此模块https://pypi.org/project/sparql-client/
模块.py
from sparql import Service
class MyModule:
    def my_method(self):
        s = Service('https://my-endpoint:8182/sparql', "utf-8", "GET")
        statement = """
            MOVE uri:temp_graph TO uri:user_graph
            ADD uri:temp_graph TO uri:user_graph    
        """.format(user_graph="http://aws.amazon.com/account-uid",
                   temp_graph="http://aws.amazon.com/account-uid-temp")
        s.query(statement)
我正在尝试测试它
测试模块.py
import unittest
from unittest.mock import patch, Mock
class TestModule(unittest.TestCase):
    @patch('sparql.Service', autospec=True)
    def test_mymethod(self, sparql_mock):
        sparql_instance = sparql_mock.return_value
        sparql_instance.query = Mock()
跑步时我得到
  File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1564, in <lambda>
    getter = lambda: _importer(target)
  File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/mock.py", line 1236, in _importer
    thing = __import__(import_path)
  File "/usr/local/lib/python3.9/site-packages/sparql.py", line 50, in <module>
    from base64 import encodestring
ImportError: cannot import name 'encodestring' from 'base64' (/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/base64.py)
所以它不能导入这一行
https://github.com/eea/sparql-client/blob/master/sparql.py#L50
知道如何解决这个问题吗?
小智 6
该问题是由您运行的 base64 模块版本引起的,而您安装的 sparql 版本依赖于较低版本的 base64 模块。sparql 依赖于为 python3.1 构建的 base64 版本。encodestring() 和 decodestring() 已被弃用。如果您必须继续使用此版本的 sparql,最好的选择是您必须将您的 python 版本从 3.9 降级到 3.1,这是您当前的版本。选项 2 将采用您已安装的当前版本的 base64 的新规范。这将意味着更新 sparql 以及任何您调用 base64 的已弃用方法的地方。
如果您选择使用 option2,则打开 sparql 模块并编辑导入语句。改变
from base64 import encodestring至from base64 import encodebytes干,用在你的代码,你必须依赖于任何的base64模块encodebytes encodestring的任何事件。那应该可以解决您的问题
| 归档时间: | 
 | 
| 查看次数: | 2623 次 | 
| 最近记录: |