'Running docker from python's venv

I have created a python module. Works on ubuntu/xenial with python3.5. I built it and uploaded to python repository. Then I created virtual environment, installed this module and tried to run. As a result I got:

(hdfnc_venv) svc@PL1LXD-529389:~/$ python3 -m my_package.my_module -h
Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/my_package/my_module.py", line 4, in <module>
    import docker
  File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/__init__.py", line 2, in <module>
    from .api import APIClient
  File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/api/__init__.py", line 2, in <module>
    from .client import APIClient
  File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/api/client.py", line 222
    f'Error while fetching server API version: {e}'
                                                  ^
SyntaxError: invalid syntax

So apparently there are multiple issues here. One is this invalid syntax which I suppose is because python3.5 does not recognize f-string syntax introduced in python3.6. Wasn't there an f-string, an exception would be raised anyway.

The bigger problem is why it there is error with just importing docker. Docker is properly installed on this machine, so is the module docker (5.0.3) in my venv. The module works when I run it directly in the system from the source code, not from my venv installation. Also when I run it this way, python3.5 works just fine, so invalid_syntax is not a problem anymore - most likely python didn't reach this part of the code, which means it managed to import docker module (also 5.0.3) without any problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source