Python with sparql-client - ImportError: cannot import name 'encodestring' from 'base64'
Asked Answered
E

1

7

I am Python newbie. Trying to use this module https://pypi.org/project/sparql-client/

module.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)

I am trying to test it

test_module.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()

While running I get

  File "/usr/local/Cellar/[email protected]/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/[email protected]/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/[email protected]/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/base64.py)

So it can not import this line

https://github.com/eea/sparql-client/blob/master/sparql.py#L50

Any idea how to fix this?

Experimentation answered 12/11, 2020 at 7:12 Comment(4)
Do you have base64 module installed? Try installing the module. you seem to be running on linux so use pip command to install modulesAccuracy
@nelsonsule How to check whether I have it or not?Experimentation
launch your python IDLE and run import base64. python is an interpreted language so if it throws an import error exception, then it means it is not installed. you can also check by attempting to install the module. if it is installed, it will throw an exceptionAccuracy
@nelsonsule ok seems I have that module ` % python3 Python 3.9.0 (default, Oct 27 2020, 14:15:17) [Clang 12.0.0 (clang-1200.0.32.21)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import base64 >>> ` Any other ideas?Experimentation
A
13

The problem is caused by the version of base64 module you are running while the version of sparql you have installed is dependent on a lower version of the base64 module. The sparql is dependent on base64 version built for python3.1. encodestring() and decodestring() have since been deprecated.

  • Option 1 - Your best bet (if you must continue using this version of sparql) is to downgrade your version of python to 3.1 from 3.9 which is your current version.
  • Option 2 - will be to adopt the new specifications for the current version of base64 you have installed. this will mean updating sparql and anywhere you are calling deprecated methods of base64.

If you choose to go with option 2, then open the sparql module and edit the import statement. Change from base64 import encodestring to from base64 import encodebytes and replace any occurrences of encodestring with encodebytes in your code and any module you have dependent on base64. That should solve your problem.

Accuracy answered 12/11, 2020 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.