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
Use of __package__ in pydantic/__init__.py fails if imported from zip #9221
Comments
I'm a bit lost, please can you format your issue using markdown so I can read the code. |
Hi,
I am unsure of what you want me todo. The issue was really only about following recommendation in the python docs and not use the identifier __package__ in your code.
We run Python embedded and prepackage all modules needed in a zip-file. We can not currently put pydantic in such a zip-file.
Regards,
Johan
…________________________________
Från: Samuel Colvin ***@***.***>
Skickat: Friday, April 12, 2024 2:57:20 PM
Till: pydantic/pydantic ***@***.***>
Kopia: Johan Andersson ***@***.***>; Author ***@***.***>
Ämne: Re: [pydantic/pydantic] Use of __package__ in pydantic/__init__.py fails if imported from zip (Issue #9221)
I'm a bit lost, please can you format your issue using markdown so I can read the code.
—
Reply to this email directly, view it on GitHub<#9221 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BDUU755IPP5S4DR3HFDF53DY47K3BAVCNFSM6AAAAABGDW6DK2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJRG4YTEMRXGE>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Thanks @johanandersson-tietoevrycom for reporting this issue.
Agree, it is not recommended to use Could you please help us to reproduce the problem? We need some steps to reproduce the problem then we can confirm the fix works. |
Hi,
I can reproduce the error under Windows with Python3.9. Essentially by putting the pydantic code in a zip-file and use that instead of the ‘pydantic’ folder under site-packages. The real context for me is when we use Python embedded into a C++ executable, and all support libraries we use go into a zip-file.
The following steps exemplifies how to get the error outside the ‘embedded’ context:
0. Install Python39 under Windows in path ‘c:\Python39’
1.
C:\tmp\testpydantic>c:\Python39\Scripts\pip.exe install pydantic
Collecting pydantic
Downloading pydantic-2.7.1-py3-none-any.whl (409 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 409.3/409.3 KB 8.5 MB/s eta 0:00:00
Collecting pydantic-core==2.18.2
Downloading pydantic_core-2.18.2-cp39-none-win_amd64.whl (1.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.9/1.9 MB 15.3 MB/s eta 0:00:00
Collecting annotated-types>=0.4.0
Using cached annotated_types-0.6.0-py3-none-any.whl (12 kB)
Collecting typing-extensions>=4.6.1
Using cached typing_extensions-4.11.0-py3-none-any.whl (34 kB)
Installing collected packages: typing-extensions, annotated-types, pydantic-core, pydantic
Successfully installed annotated-types-0.6.0 pydantic-2.7.1 pydantic-core-2.18.2 typing-extensions-4.11.0
WARNING: You are using pip version 22.0.4; however, version 24.0 is available.
You should consider upgrading via the 'C:\Python39\python.exe -m pip install --upgrade pip' command.
2. Under: C:\Python39\Lib\site-packages, create a ‘pydantic.zip’ from ‘C:\Python39\Lib\site-packages\pydantic’
3. Rename folder ‘C:\Python39\Lib\site-packages\pydantic’ to ‘C:\Python39\Lib\site-packages\xxpydantic’
4. Move ‘pydantic.zip’ to ‘C:\tmp\testpydantic’
5. set PYTHONPATH=c:\tmp\testpydantic\pydantic.zip
6.
C:\tmp\testpydantic>c:\Python39\python.exe
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>> from pydantic import Field
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1055, in _handle_fromlist
File "c:\tmp\testpydantic\pydantic.zip\pydantic\__init__.py", line 389, in __getattr__
File "c:\Python39\lib\importlib\__init__.py", line 122, in import_module
raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.fields'
>> from pydantic.fields import Field
>>
From: Hasan Ramezani ***@***.***>
Sent: Thursday, 18 April 2024 18:04
To: pydantic/pydantic ***@***.***>
Cc: Johan Andersson ***@***.***>; Mention ***@***.***>
Subject: Re: [pydantic/pydantic] Use of __package__ in pydantic/__init__.py fails if imported from zip (Issue #9221)
Thanks @johanandersson-tietoevrycom<https://github.com/johanandersson-tietoevrycom> for reporting this issue.
As package is discouraged in docs: https://docs.python.org/3/reference/import.html,
Agree, it is not recommended to use __package__. Thanks for your suggestion.
Could you please help us to reproduce the problem? We need some steps to reproduce the problem then we can confirm the fix works.
—
Reply to this email directly, view it on GitHub<#9221 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BDUU75YHJG23FILVWWER4QTY57VGRAVCNFSM6AAAAABGDW6DK2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRUGM3DSMZXG4>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
Thanks @johanandersson-tietoevrycom for helping us to reproduce the problem. I created #9331 to fix the problem. It would be great if you can confirm the fix. |
Initial Checks
Description
If pydantic folder is placed in zip, then when doing for example 'from pydantic import Field' and __init__.py is run to resolve to 'pydantic.fields' this fails because __package__ is not set. As __package__ is discouraged in docs:
https://docs.python.org/3/reference/import.html, it would be nice if __init__.py used '__spec__.parent' instead as this then works in zip files too. At least do:
if not __package__:
__package__ = __spec__.parent
at some point before using __package__
Example Code
No response
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: