Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Cannot import pythonjsonlogger.jsonlogger #29

Closed
gowdy opened this issue Dec 12, 2024 · 8 comments · Fixed by #33
Closed

Cannot import pythonjsonlogger.jsonlogger #29

gowdy opened this issue Dec 12, 2024 · 8 comments · Fixed by #33
Labels
bug Something isn't working

Comments

@gowdy
Copy link

gowdy commented Dec 12, 2024

Was 3.2.0 meant to remove this?

from pythonjsonlogger.jsonlogger import JsonFormatter

Looks to still be deprecated from the release docs, nothing about it being removed (it says moved at the same time as deprecated).

@iTaybb
Copy link

iTaybb commented Dec 12, 2024

It juts broke for me as well (using kopf).
I just rollbacked to 2.0 and it's working for me

@leroyshirto
Copy link

It juts broke for me as well (using kopf). I just rollbacked to 2.0 and it's working for me

This has broken kopf for us too

@soxofaan
Copy link
Contributor

pythonjsonlogger.jsonlogger seems to be removed (already in 3.1.0) with 49f69fb for #9

if I see correctly, an attempt was made to stay backward compatible:

def __getattr__(name: str):
if name == "jsonlogger":
warnings.warn(
"pythonjsonlogger.jsonlogger has been moved to pythonjsonlogger.json",
DeprecationWarning,
)
return pythonjsonlogger.json
raise AttributeError(f"module {__name__} has no attribute {name}")

but that only seems to work with

from pythonjsonlogger import jsonlogger

not with

import pythonjsonlogger.jsonlogger

@nhairs nhairs added the bug Something isn't working label Dec 12, 2024
@nhairs nhairs changed the title Removal of deprecated jsonlogger Cannot import pythonjsonlogger.jsonlogger Dec 12, 2024
@nhairs
Copy link
Owner

nhairs commented Dec 12, 2024

Thanks for digging into that @soxofaan.

Short term workaround would be to pin your python-json-logger version.

Medium term would be to migrate the underlying code.

I will however see if I can fix this bug as I would like to keep backwards compatibility.

nhairs added a commit that referenced this issue Dec 13, 2024
Fixes: #29

Although efforts were made for backwards compatibility using
`__getattr__` in `pythonjsonlogger/__init__.py`, this only worked
if you were doing `from pythonjsonlogger import jsonlogger`. When
importing by the full name `import pythonjsonlogger.jsonlogger` it
it would fail to locate the module file and thus not be able to
produce a module spec which then of course causes the import to fail.

We get around this by actually having a module that imports the names
it needs from the new locations.
@nhairs
Copy link
Owner

nhairs commented Dec 13, 2024

I've uploaded a development version that should fix this.

Would someone be able to test using python-json-logger==3.2.1.dev1 and get back to me on #33 if it resolved this issue?

@Rhiyo
Copy link

Rhiyo commented Dec 13, 2024

This broke our code as well.

quick fix for us was just to add

python-json-logger<3.1.0 to our pinned dependencies.

nhairs added a commit that referenced this issue Dec 16, 2024
Fixes: #29

Although efforts were made for backwards compatibility using
`__getattr__` in `pythonjsonlogger/__init__.py`, this only worked if you
were doing `from pythonjsonlogger import jsonlogger`. When importing by
the full name `import pythonjsonlogger.jsonlogger` it it would fail to
locate the module file and thus not be able to produce a module spec
which then of course causes the import to fail.

We get around this by actually having a module that imports the names it
needs from the new locations.

### Test Plan

- Run unit tests
@nhairs
Copy link
Owner

nhairs commented Dec 16, 2024

Hi all, I've released a fix for this issue (python-json-logger==3.2.1).

I don't currently intend to backport the changes, but could be convinced if someone has a compelling need.

@gowdy
Copy link
Author

gowdy commented Dec 16, 2024

Thanks, that looks to have solved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
@soxofaan @gowdy @iTaybb @Rhiyo @leroyshirto @nhairs and others