2

When attempting to import uno under a Python 3.10 install, Python throws an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\libreoffice7.3.4\program\uno.py", line 19, in <module>
    import pyuno
ImportError: Module use of python38.dll conflicts with this version of Python.

Per research, the culprit is indeed the "python38.dll", which is a file in the Python 3.8 install that LibreOffice needs to do its Python stuff. The catch is, I don't have that dll, I have "python310.dll", and I cannot see how to tell LibreOffice (v. 7.3.4) to look for that dll instead.

I have tried to update the PYTHONPATH environment variable to point at the 310.dll, and I have made sure that I only have one version of Python installed with $ py -01p.

Searching within the LibreOffice files/forums has not turned up anything, either. Apparently LibreOffice 7.2 shipped with Python 3.8, and the LibreOffice 7.3 release notes' "Python" field is empty.

Am I just SOL trying to use Python to interact with LibreOffice until they maybe fix the issue with 7.4?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 2
    You could try creating a Python 3.8 environment (separate from your main global 3.10 environment) and have that be the active environment when starting LibreOffice? It's a bit dodgy that LO would rely on some version of Python that just happens to be installed though, so it may just be the result of the way you've installed / configured LibreOffice and a reinstall might fix it. – Grismar Jul 18 '22 at 03:30
  • I agree that it's dodgy.. but OpenOffice relies on an even earlier version of Python. Both OO and LO ship with included versions of Python, and neither one apparently has an explicit method for updating the version... – M.Argyle.22 Jul 18 '22 at 03:53
  • Well-formatted question, but the "this is what I want to know" finale is unclear. "trying to use Python to interact with LibreOffice" — This sounds like you are asking how to get Python-UNO working the normal way, which would be the installation that comes bundled with LO. However, the other info sounds like you are trying to hack a separate Python installation to get it to work with LO. If you are specifically asking about Python 3.10, then the title should be about how to get LO to work with a newer version of Python than it is bundled with. – Jim K Jul 18 '22 at 18:45
  • Jim, the correct phrasing of my issue, which you have succinctly identified, was not apparent to me at the time of posting the question. I would have to know half the answer to my Problem to frame my Issue correctly! Basically, I had (1) installed Python 3.10, (2) installed LibreOffice7.3, and (3) failed to be able to `import uno`. The facts that there were two separate instances of Python, and that LO requires its own native instance, were unknown to me at the time. – M.Argyle.22 Jul 19 '22 at 02:02

1 Answers1

1

After reinstalling Python (multiple versions, including 3.8) and LibreOffice several times, digging through settings files, and reading half a dozen sites on the topic, I finally arrived upon a very simple solution.

There's a python.exe file in the Program folder of the LibreOffice install. This file is a python interpreter, and the Uno module works just fine when using this interpreter.

Point VSCode (or similar) to that file, and problem is totally solved, unless there is some compelling reason to use Python 3.9 or 3.10.

  • Checking back in on this to `alert( :-P )` - this fix does not allow terminal/powershell access (either through the windows command prompt or through the VSCode Terminal pane) to the uno-ready Python, which is more than a little annoying. I intend to figure out a fix by updating various Registry fields. Hopefully I get to this soon... – M.Argyle.22 Jul 18 '22 at 13:04
  • "reading half a dozen sites on the topic" — It's better to give links than to simply state that you've read something somewhere. But whatever you read, the normal way to run Python-UNO on Windows is to use the Python installation bundled with LibreOffice. There should be no problem using it in a terminal either — `chdir Libreoffice/program`, then `& ./python.exe`, then `import uno`. – Jim K Jul 18 '22 at 18:11
  • 1
    This is indeed how I run my Python macros in Windows: use the LibO version of Python. To do really fun stuff, you might want to install modules (e.g. NumPy, Pandas, etc) into the LibO version of Python. Check out [this Answer](https://stackoverflow.com/a/68568331/9705687) for some instructions how to do it. Also, you should look at the [MRI extension](https://github.com/hanya/MRI/releases) and [APSO extension](https://extensions.libreoffice.org/en/extensions/show/apso-alternative-script-organizer-for-python) – bfris Jul 18 '22 at 21:53
  • @bfris yes! I was just looking at get-pip.py, and I'd also like to note (for those who might be using this thread for guidance) that [this site](http://www.imladris.com/Scripts/PythonForWindows.html) gave easy to follow instructions for updating the Path to enable `$ python` to open the LibO instance (delete prior Paths in the list, or at least move the LibO location above the others). – M.Argyle.22 Jul 19 '22 at 01:57