[Python][External library issue] Deepl library issue

Hi all,

I’m trying to create an app using the recent deepl python library. I seem unable to use the library on Qt Creator altough it workds flawlessly on my computer’s terminal.

The python code is called by a QML file that worked with other python codes before. It’s using the io.thp.pyotherside 1.5 library.

Has any of you had the chance to try this library ?

I imported all necessary library files (deepl, requests, idna, urllib3, certifi and charset) into the project in Qt Creator.

When using the following code in my python file :

translator = deepl.Translator(my_key) result = translator.translate_text("hello world!", target_lang="DE") print(result)

I get the following errors :

Traceback (most recent call last): File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner self.run() File "/usr/lib/python3.8/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/usr/share/pythonsample/qml/pages/datadownloader.py", line 52, in slow_function result = translator.translate_text("hello world!", target_lang="FR") File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 655, in translate_text request_data = self._check_language_and_formality( File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 572, in _check_language_and_formality self._check_valid_languages(source_lang, target_lang) File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 537, in _check_valid_languages target_lang == lang.code for lang in self.get_target_languages() File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 935, in get_target_languages self._target_languages_cached = self._request_languages( File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 499, in _request_languages status, content, json = self._api_call("v2/languages", data=data) File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 430, in _api_call status_code, content = self._client.request_with_backoff( File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 93, in request_with_backoff raise exception File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 79, in request_with_backoff response = self.request( File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 150, in request raise ConnectionException(message, should_retry=should_retry) deepl.exceptions.ConnectionException: Unexpected connection error: __enter__

Feel free to ask for information I missed sharing !

Have a nice day !

What version of the requests library do you use? deepl requires at least 2.18. The versions before did not allow to use a context manager for Response.

According to the version.py file I’m using version 2.26.0

version = ‘2.26.0’
build = 0x022600

I’m wondering if it’s worth to create an issue on their github or if the issue could be on either my side or pytheotherside side’s

It’s a bit difficult to debug this without code. If it was available, I’d try interactive debugging.

Version 1.1.2 of deepl-python has been released with improved exception stacktraces. Maybe that helps in finding the cause of the problem.

Thanks for the tip, I’ve just tried it and it’s the same error.

I tried this very minimalist setup that gives the same AttributeError: __enter__ error as before the 1.1.2 version of deepl. With sailpy I generated the dependencies folder.

The main.qml file :

import QtQuick 2.0
import Sailfish.Silica 1.0
import io.thp.pyotherside 1.5
Page {
id: main
Label{
id: mainLabel
anchors.centerIn: parent
text: ‘’
}
Python {
id: python
Component.onCompleted: {
addImportPath(Qt.resolvedUrl(’…/pages’));
importModule(‘main’,function(){
call(‘main.hello_world’, [], function(message){
mainLabel.text = message;
}) }); }}}

The main.py file :

import pyotherside
import deepl
def hello_world():
translator = deepl.Translator(my_api_key)
result = translator.translate_text(“hello world!”, target_lang=“DE”)
pyotherside.send(result)

So in the end, even with the updated version of deepl, I’ve got the exact same error.

The string “Unexpected connection error” has been replaced in deepl-python 1.1.2, so the error cannot be exactly the same. Do you mind posting your stack trace again?

Sorry my mistake, the end line is the same.

[D] unknown:0 - "PyOtherSide error: Traceback (most recent call last):\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/http_client.py\", line 134, in request\n    with self._session.request(\n\nAttributeError: __enter__\n\n\nThe above exception was the direct cause of the following exception:\n\n\nTraceback (most recent call last):\n\n  File \"/usr/share/pythonsample/qml/pages/main.py\", line 11, in hello_world\n    result = translator.translate_text(\"hello world!\", target_lang=\"FR\")\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/translator.py\", line 655, in translate_text\n    request_data = self._check_language_and_formality(\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/translator.py\", line 572, in _check_language_and_formality\n    self._check_valid_languages(source_lang, target_lang)\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/translator.py\", line 537, in _check_valid_languages\n    target_lang == lang.code for lang in self.get_target_languages()\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/translator.py\", line 935, in get_target_languages\n    self._target_languages_cached = self._request_languages(\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/translator.py\", line 499, in _request_languages\n    status, content, json = self._api_call(\"v2/languages\", data=data)\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/translator.py\", line 430, in _api_call\n    status_code, content = self._client.request_with_backoff(\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/http_client.py\", line 93, in request_with_backoff\n    raise exception\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/http_client.py\", line 79, in request_with_backoff\n    response = self.request(\n\n  File \"/usr/share/pythonsample/qml/pages/deepl/http_client.py\", line 151, in request\n    raise ConnectionException(message, should_retry=False) from e\n\ndeepl.exceptions.ConnectionException: Unexpected request failure: __enter__\n"
[W] unknown:0 - Unhandled PyOtherSide error: Return value of PyObject call is NULL: Traceback (most recent call last):

  File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 134, in request
    with self._session.request(

AttributeError: __enter__


The above exception was the direct cause of the following exception:


Traceback (most recent call last):

  File "/usr/share/pythonsample/qml/pages/main.py", line 11, in hello_world
    result = translator.translate_text("hello world!", target_lang="FR")

  File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 655, in translate_text
    request_data = self._check_language_and_formality(

  File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 572, in _check_language_and_formality
    self._check_valid_languages(source_lang, target_lang)

  File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 537, in _check_valid_languages
    target_lang == lang.code for lang in self.get_target_languages()

  File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 935, in get_target_languages
    self._target_languages_cached = self._request_languages(

  File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 499, in _request_languages
    status, content, json = self._api_call("v2/languages", data=data)

  File "/usr/share/pythonsample/qml/pages/deepl/translator.py", line 430, in _api_call
    status_code, content = self._client.request_with_backoff(

  File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 93, in request_with_backoff
    raise exception

  File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 79, in request_with_backoff
    response = self.request(

  File "/usr/share/pythonsample/qml/pages/deepl/http_client.py", line 151, in request
    raise ConnectionException(message, should_retry=False) from e

deepl.exceptions.ConnectionException: Unexpected request failure: __enter__

Still looks to me as there is some problem with the context manager usage. Could you try to embed the following Python script into your main.py file?

import requests

print(requests.__version__)

with requests.session() as session, session.request(method='GET', url='https://www.deepl.com') as request:
    request.raise_for_status()

If everything works as expected, it should print out the requests library’s version (2.25.1 in my case) and perform an HTTP GET request resulting in requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.deepl.com/.

Thank you for your help !

So this is what my main.py file looked like :

import pyotherside
import deepl
import requests
print(requests.version)
with requests.session() as session, session.request(method=‘GET’, url=‘https://www.deepl.com’) as request:
request.raise_for_status()
def hello_world():
translator = deepl.Translator(“my_api_key”)
result = translator.translate_text(“hello world!”, target_lang=“DE”)
pyotherside.send(result)

And this is the error output :

[D] unknown:0 - Using Wayland-EGL
[D] unknown:0 - Got library name:  "/usr/lib/qt5/qml/io/thp/pyotherside/libpyothersideplugin.so"
[D] unknown:0 - "PyOtherSide error: Traceback (most recent call last):\n\n  File \"/usr/share/pythonsample/qml/pages/main.py\", line 11, in <module>\n    with requests.session() as session, session.request(method='GET', url='https://www.deepl.com') as request:\n\nAttributeError: __enter__\n"
[W] unknown:0 - Unhandled PyOtherSide error: Cannot import module: main (Traceback (most recent call last):

  File "/usr/share/pythonsample/qml/pages/main.py", line 11, in <module>
    with requests.session() as session, session.request(method='GET', url='https://www.deepl.com') as request:

AttributeError: __enter__
)
[D] unknown:0 - "PyOtherSide error: Traceback (most recent call last):\n\n  File \"<string>\", line 1, in <module>\n\nNameError: name 'main' is not defined\n"
[W] unknown:0 - Unhandled PyOtherSide error: Function not found: 'main.hello_world' (Traceback (most recent call last):

  File "<string>", line 1, in <module>

NameError: name 'main' is not defined
)

I also tried by inserting your code in the def :

def hello_world():
print(requests.version)

with requests.session() as session, session.request(method='GET', url='https://www.deepl.com') as request:
    request.raise_for_status()
translator = deepl.Translator("my_api_key")
result = translator.translate_text("hello world!", target_lang="DE")
pyotherside.send(result)

Which resulted in : the same error

[D] unknown:0 - Using Wayland-EGL
[D] unknown:0 - Got library name: “/usr/lib/qt5/qml/io/thp/pyotherside/libpyothersideplugin.so”
[D] unknown:0 - “PyOtherSide error: Traceback (most recent call last):\n\n File “/usr/share/pythonsample/qml/pages/main.py”, line 13, in hello_world\n with requests.session() as session, session.request(method=‘GET’, url=‘https://www.deepl.com’) as request:\n\nAttributeError: enter\n”
[W] unknown:0 - Unhandled PyOtherSide error: Return value of PyObject call is NULL: Traceback (most recent call last):
File “/usr/share/pythonsample/qml/pages/main.py”, line 13, in hello_world
with requests.session() as session, session.request(method=‘GET’, url=‘https://www.deepl.com’) as request:
AttributeError: enter

So if my understanding is correct, the problem (for me) comes from the requests library ?

Yes, it looks like the with session.request(… construct (context manager usage) is not supported by your requests version. That’s why I assumed in my first comment that you might be using a requests version from a time before that support was added. Unfortunately, I cannot see the result of print(requests.__version__) in the log. Could you try to make that visible somewhere? I have never used pyotherside, but looking at your code, maybe something like this could work:

import pyotherside
import requests

def hello_world():
    print(requests.__version__)
    pyotherside.send(requests.__version__)

It prints :

[D] onReceived:120 - got message from python: 2.8.0

Okay, that’s quite old (requests 2.8.0 released 2015-10-05). Is your script executed inside the Sailfish OS emulator? Is it possible to update the requests package somehow, e.g., by installing one from OpenRepos.net?

Ok this was my mistake, it was using an old repository from a forked github example of python app for SFOS i was using, it is now using version “2.26.0”

Ah, okay. 2.26.0 sounds great. Do you still see the problems with the example I provided or with deepl-python with that newer version?

Yes it’s solved ! But the issue was somewhere else !

You see, in this repo the author packed all it’s libraries into one folder (so all requests required libraries inside the requests folder).

And I decided to do the same… So i had the deepl library, inside it, the requests one, and inside that one all of it’s dependencies.

I realized thanks to your last test that it wasn’t working. When I instead had all my libraries side by side (so in page folder there is the deepl, the requests, urllib3, idna, etc…) it worked just fine !

Okay, great that you have found that and that’s working now. Maybe you should mark your post as solution for your problem then.

1 Like