Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

v1.0.0: ImportError: cannot import name 'parse_xml' from 'docx.oxml' #1256

Closed
matthuisman opened this issue Oct 11, 2023 · 10 comments · Fixed by BlackBoiler/python-docx#23
Closed
Labels
misfeature Doesn't behave how one might expect shortlist

Comments

@matthuisman
Copy link

matthuisman commented Oct 11, 2023

between v0.8.11 and v1.0.0.0 this seems to have been moved to: from docx.oxml.parser import parse_xml

I cant recall seeing any warning about this and cant see any migration notes for the 1.0.0 release.
Maybe this was a mistake?

Would it be possible to add the above to docx.oxml.init to allow backwards compatibility with existing imports?

@scanny
Copy link
Contributor

scanny commented Oct 11, 2023

Hmm, interesting, thanks for taking a look at this and reporting :)

Let me have a look here ...

Ah, right. So this actually moved to docx.oxml.parser.parse_xml(). There were some import cycles that led me to extract the parser into its own module. OxmlElement() also moved to that module.

Those are not part of the API per se, so we reserve the right to move those around as needed for the implementation.

What is the problem that it causes?

btw, docx.opc.oxml.parse_xml() is strictly for parsing packaging items like relationships (.rels) package parts, so wouldn't work for things like document elements, paragraphs, runs, etc.

@matthuisman
Copy link
Author

i guess the problem is we have a lot of legacy code using the old import
And if we change to new import - it breaks any clients using an older version.

And yes, your right. The new equivalent is
from docx.oxml.parser import parse_xml

@hf-kklein
Copy link

same goes for OxmlElement

ImportError: cannot import name 'OxmlElement' from 'docx.oxml'

It's now available as:

from docx.oxml.parser import OxmlElement

@scanny
Copy link
Contributor

scanny commented Oct 11, 2023

k, I think I can "re-export" those from docx.oxml(.__init__) without running into any cycles, let me have a look ...

@patou01
Copy link

patou01 commented Oct 11, 2023

Hi, this broke a number of packages indeed as far as I can tell. Broke my CI this morning, as a dependency of https://github.com/elapouya/python-docx-template/blob/master/requirements.txt and https://github.com/4teamwork/docxcompose/blob/master/setup.py, which installed 1.0.0 instead of previously 0.8.11 and broke after that.

src\base_items\test_reports\control\test_report.py:8: in <module>
    from docxtpl import DocxTemplate
C:\hostedtoolcache\windows\Python\3.9.13\x86\lib\site-packages\docxtpl\__init__.py:13: in <module>
    from .subdoc import Subdoc
C:\hostedtoolcache\windows\Python\3.9.13\x86\lib\site-packages\docxtpl\subdoc.py:11: in <module>
    from docxcompose.properties import CustomProperties
C:\hostedtoolcache\windows\Python\3.9.13\x86\lib\site-packages\docxcompose\properties.py:9: in <module>
    from docx.oxml import parse_xml
E   ImportError: cannot import name 'parse_xml' from 'docx.oxml' (C:\hostedtoolcache\windows\Python\3.9.13\x86\lib\site-packages\docx\oxml\__init__.py)

Because of the way their requirements are defined, I guess both of these packages are broken by the release. It's unclear to me how or if these are maintained though, and I don't think it's your job to check on each of your users.

Just putting this here as info in case it could help.

From my side, I'm probably just going to pin to the older release because of my environment (32 bits), which will eventually not be supported anymore and will just bring me back here to look for somebody who had the same issue :) .

(Thanks for the work!)

@scanny
Copy link
Contributor

scanny commented Oct 11, 2023

Okay, I can live with these two being "de facto published API". It doesn't appear to cause me any real problems to republish them in their original location.

I'll include these changes in v1.0.1. That won't be long, within two weeks I expect, just want to see what else might need to go in there.

In the meantime, pinning to python-docx<1.0.0 should do the trick. Btw the same will be necessary for anyone on a "legacy" version of Python, meaning 3.6 or before.

@scanny scanny added shortlist misfeature Doesn't behave how one might expect labels Oct 11, 2023
jacobtylerwalls added a commit to archesproject/arches-templating that referenced this issue Oct 11, 2023
wes-otf added a commit to HyphaApp/hypha that referenced this issue Oct 12, 2023
With the latest updates to python-docx, older APIs that are relied upon
by htmldocx have broken. This pins the version so htmldocx can utilize
the old APIs. For more info on this breakage see
[here](python-openxml/python-docx#1256 (comment)).
@dsanmarting
Copy link

is the version, install:

pip install python-docx==0.8.11

that worked for me

scanny added a commit that referenced this issue Oct 12, 2023
Multiple downstream "extension" packages expect to find `OxmlElement`
and `parse_xml()` at `docx.oxml`. Refactoring in v1.0.0 moved those to
`docx.oxml.parser`, but republishing them at `docx.oxml` does not cause
any immediate breakage.

Republish those two callables at `docx.oxml` for v1.0.1.
@scanny
Copy link
Contributor

scanny commented Oct 12, 2023

Okay, v1.0.1 is live on PyPI and should remedy this. Please confirm that fixes this for you, meantime I'll close this issue :)

@scanny scanny closed this as completed Oct 12, 2023
@matthuisman
Copy link
Author

matthuisman commented Oct 12, 2023

i cant see any code changes on this repo relating to 1.0.1?

Also, should these be considered deprecated? Is it advised to change imports?

@scanny
Copy link
Contributor

scanny commented Oct 13, 2023

The last four commits on master are these, or you can look at tag "v1.0.1".

I looked into adding a deprecation warning, but that looked like more trouble that it's worth. In my own code I would import them from where they're defined rather than somewhere they're republished, but I don't see an immediate reason to remove them from docx/oxml/__init__.py. I'll leave it up to you.

wes-otf added a commit to HyphaApp/hypha that referenced this issue May 7, 2024
With the latest updates to python-docx, older APIs that are relied upon
by htmldocx have broken. This pins the version so htmldocx can utilize
the old APIs. For more info on this breakage see
[here](python-openxml/python-docx#1256 (comment)).
wes-otf added a commit to HyphaApp/hypha that referenced this issue May 8, 2024
With the latest updates to python-docx, older APIs that are relied upon
by htmldocx have broken. This pins the version so htmldocx can utilize
the old APIs. For more info on this breakage see
[here](python-openxml/python-docx#1256 (comment)).
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
misfeature Doesn't behave how one might expect shortlist
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants