You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.pth files cannot contain folders with utf-8 names #77102
It's worth noting that tools which use the editables library will also write .pth files for support of editable installations (editables returns the file name and content as strings, the calling tool needs to choose the encoding when writing the file). And people can manually write their own. I think the important thing here is to define a standard that tools can work to. Normally I'd suggest it should be a packaging standard, but .pth files are a core feature, so that might not work. I don't know where a rule like this, for a core Python feature that needs to be documented across versions, should be published - expecting users to check the 3.11, 3.12 and 3.13+ documentation for .pth files and work out the differences themselves seems rather hostile 🙁
From the perspective of a tool (or user) writing a .pth file, I think the rule should be "use UTF-8 for Python 3.13 or later, and the default locale for Python 3.12 and earlier". It's easy to know what the target Python is, as you're typically writing the file into the site-packages for that installation. It's too late to change the default for 3.12, so this is really the only possible option.
From the perspective of the core reading .pth files, I'd try UTF-8 first, and in the case of an encoding error (as @vstinner suggested) fall back to the default encoding. I'd be inclined to leave the fallback in place even in 3.13+ to support users still using older tools - but I could accept 3.13 applying the stricter "only UTF-8" rule.
I don't think we should bring UTF-8 mode into it, as that's a runtime choice (and .pth files are static, not affected by runtime settings).
Reacted by Anderson Bravalheri, Mehdi Drissi and Steve Dower
Locale encoding is also a runtime configuration. When you switch languages on Windows or change environment variables on Linux, it changes. UTF-8 Mode helps ensure that the virtual environment is not destroyed by changes in language or environment variables.
When I proposed PEP 597, I did not have plans to make UTF-8 Mode the default. site.py supported UTF-8 Mode up to Python 3.9, but when we implemented PEP 597, I stopped supporting it for eliminating EncodingWarning. I choose locale encoding because setuptools used it.
However, I realized that the feasibility of a migration process using EncodingWarnings is very low. I proposed PEP 686, which is a migration process using the UTF-8 Mode, and it was approved. In UTF-8 Mode, we should use UTF-8 in all places unless there is a special reason not to.
So I think supporting UTF-8 mode again in 3.11/3.12 is good idea. It is compatible to 3.9. It provides reason to opt-in UTF-8 mode before Python 3.15.
Another way to support UTF-8 pth file is #117802 . It tries UTF-8 and fallbacks to locale encoding. I'm OK for it too.
Anyway, Python is a glue language, and interoperability with other languages is important. I don't want to force the uv developers to use a huge number of codecs that works 100% same to Python codecs to handle locale encoding. I want to provide a way to use UTF-8 in not only Python 3.13 but also Python 3.11 and 3.12 as well.
Maybe, #117802 would be better idea for now. setuptools uses locale encoding and uv uses utf-8.
It would be better to support both of utf-8 and locale encoding in one mode.
uv venv writes a .pth file, as you noticed, but it's ASCII-only. That's the only time uv writes a .pth file itself (as far as I know) - any other .pth files will come from installing projects, and those projects will have their own build backend (specified in the project's pyproject.toml file).
The only difficulty from .pth files created in the install process is likely to come from editable mode installs, as has been discussed here. Those files include the absolute path of the project source directory, and that can be non-ASCII (for example, if it's in the user's home directory and the user has a username that includes non-ASCII characters).
There are other problem cases:
.pth files created manually by the user, which can contain anything the user chooses.
.pth files included in a project, which put subdirectories installed by the project onto sys.path. For example, pywin32 does this. But actually, the only example of this that I know of is pywin32, and that has ASCII names, so this might well be a non-issue.
For (1), we need to document the rules properly, and then it's up to the user to follow them - if the user wants to add a site-packages directory (via $PYTHONPATH, for instance) that is shared across Python versions, making that work is on them, I guess1.
For (2), things are similar, as the same package could be installed in different Python versions. I doubt a project is going to want to publish different wheels for different Python versions, just to handle .pth file encodings. But as I said, it's quite possible that there are no packages that have this problem in any case.
Footnotes
Supporting locale encoding as a fallback if there are UTF-8 errors until all Python versions that don't allow UTF-8 are out of support would help with this, though. ↩
uv venv writes a .pth file, as you noticed, but it's ASCII-only. That's the only time uv writes a .pth file itself (as far as I know) - any other .pth files will come from installing projects, and those projects will have their own build backend (specified in the project's pyproject.toml file).
The only difficulty from .pth files created in the install process is likely to come from editable mode installs, as has been discussed here. Those files include the absolute path of the project source directory, and that can be non-ASCII (for example, if it's in the user's home directory and the user has a username that includes non-ASCII characters).
You are right. I thought pth files are created by uv pip install -e but it is created by the Hatch. Hatchling uses UTF-8 when building wheel for editable install:
For (1), we need to document the rules properly, and then it's up to the user to follow them - if the user wants to add a site-packages directory (via $PYTHONPATH, for instance) that is shared across Python versions, making that work is on them, I guess1.
How about this plan?
Python 3.11~3.13: Use UTF-8 and fallback to locale encoding.
Python 3.14~3.15: Same to above but show EncodingWarning when fallback is used.
Python 3.16~: UTF-8 only.
Reacted by Shantanu, Anderson Bravalheri and Paul Moore
Python 3.11~3.13: Use UTF-8 and fallback to locale encoding.
Python 3.14~3.15: Same to above but show EncodingWarning when fallback is used.
Python 3.16~: UTF-8 only.
LGTM.
Would the check sys.version_info >= (3, 13) be good enough for libraries producing .pth to know when they can emit UTF-8? (Ideally there should be a simple and easy to maintain condition that producers can check to see when using UTF-8 is guaranteed to work for the given Python installation).
Would the check sys.version_info >= (3, 13) be good enough for libraries producing .pth to know when they can emit UTF-8? (Ideally there should be a simple and easy to maintain condition that producers can check to see when using UTF-8 is guaranteed to work for the given Python installation).
Yes. UTF-8 support will be backported to 3.11 and 3.12 only for tools already produce UTF-8 .pth files.
Activity
pfmoore commentedon Apr 12, 2024
It's worth noting that tools which use the
editableslibrary will also write.pthfiles for support of editable installations (editablesreturns the file name and content as strings, the calling tool needs to choose the encoding when writing the file). And people can manually write their own. I think the important thing here is to define a standard that tools can work to. Normally I'd suggest it should be a packaging standard, but.pthfiles are a core feature, so that might not work. I don't know where a rule like this, for a core Python feature that needs to be documented across versions, should be published - expecting users to check the 3.11, 3.12 and 3.13+ documentation for.pthfiles and work out the differences themselves seems rather hostile 🙁From the perspective of a tool (or user) writing a
.pthfile, I think the rule should be "use UTF-8 for Python 3.13 or later, and the default locale for Python 3.12 and earlier". It's easy to know what the target Python is, as you're typically writing the file into the site-packages for that installation. It's too late to change the default for 3.12, so this is really the only possible option.From the perspective of the core reading
.pthfiles, I'd try UTF-8 first, and in the case of an encoding error (as @vstinner suggested) fall back to the default encoding. I'd be inclined to leave the fallback in place even in 3.13+ to support users still using older tools - but I could accept 3.13 applying the stricter "only UTF-8" rule.I don't think we should bring UTF-8 mode into it, as that's a runtime choice (and
.pthfiles are static, not affected by runtime settings).methane commentedon Apr 13, 2024
Locale encoding is also a runtime configuration. When you switch languages on Windows or change environment variables on Linux, it changes. UTF-8 Mode helps ensure that the virtual environment is not destroyed by changes in language or environment variables.
When I proposed PEP 597, I did not have plans to make UTF-8 Mode the default. site.py supported UTF-8 Mode up to Python 3.9, but when we implemented PEP 597, I stopped supporting it for eliminating EncodingWarning. I choose locale encoding because setuptools used it.
However, I realized that the feasibility of a migration process using EncodingWarnings is very low. I proposed PEP 686, which is a migration process using the UTF-8 Mode, and it was approved. In UTF-8 Mode, we should use UTF-8 in all places unless there is a special reason not to.
So I think supporting UTF-8 mode again in 3.11/3.12 is good idea. It is compatible to 3.9. It provides reason to opt-in UTF-8 mode before Python 3.15.
Another way to support UTF-8 pth file is #117802 . It tries UTF-8 and fallbacks to locale encoding. I'm OK for it too.
Anyway, Python is a glue language, and interoperability with other languages is important. I don't want to force the uv developers to use a huge number of codecs that works 100% same to Python codecs to handle locale encoding. I want to provide a way to use UTF-8 in not only Python 3.13 but also Python 3.11 and 3.12 as well.
methane commentedon Apr 13, 2024
Maybe, #117802 would be better idea for now. setuptools uses locale encoding and uv uses utf-8.
It would be better to support both of utf-8 and locale encoding in one mode.
hauntsaninja commentedon Apr 14, 2024
(possibly dumb question) uv doesn't provide a build backend, so when is it writing pth files?
edit: I guess uv venv writes to
_virtualenv.pth, but that should be asciipfmoore commentedon Apr 14, 2024
uv venvwrites a.pthfile, as you noticed, but it's ASCII-only. That's the only timeuvwrites a.pthfile itself (as far as I know) - any other.pthfiles will come from installing projects, and those projects will have their own build backend (specified in the project'spyproject.tomlfile).The only difficulty from
.pthfiles created in the install process is likely to come from editable mode installs, as has been discussed here. Those files include the absolute path of the project source directory, and that can be non-ASCII (for example, if it's in the user's home directory and the user has a username that includes non-ASCII characters).There are other problem cases:
.pthfiles created manually by the user, which can contain anything the user chooses..pthfiles included in a project, which put subdirectories installed by the project ontosys.path. For example,pywin32does this. But actually, the only example of this that I know of ispywin32, and that has ASCII names, so this might well be a non-issue.For (1), we need to document the rules properly, and then it's up to the user to follow them - if the user wants to add a site-packages directory (via
$PYTHONPATH, for instance) that is shared across Python versions, making that work is on them, I guess1.For (2), things are similar, as the same package could be installed in different Python versions. I doubt a project is going to want to publish different wheels for different Python versions, just to handle
.pthfile encodings. But as I said, it's quite possible that there are no packages that have this problem in any case.Footnotes
Supporting locale encoding as a fallback if there are UTF-8 errors until all Python versions that don't allow UTF-8 are out of support would help with this, though. ↩
methane commentedon Apr 15, 2024
You are right. I thought pth files are created by
uv pip install -ebut it is created by the Hatch. Hatchling uses UTF-8 when building wheel for editable install:Poetry uses
locale.getpreferredencoding()that uses UTF-8 in UTF-8 mode.How about this plan?
abravalheri commentedon Apr 15, 2024
LGTM.
Would the check
sys.version_info >= (3, 13)be good enough for libraries producing.pthto know when they can emit UTF-8? (Ideally there should be a simple and easy to maintain condition that producers can check to see when using UTF-8 is guaranteed to work for the given Python installation).methane commentedon Apr 15, 2024
Yes. UTF-8 support will be backported to 3.11 and 3.12 only for tools already produce UTF-8
.pthfiles.gh-77102: site: try utf-8 and fallback to locale encoding when readin…
[3.12] pythongh-77102: site: try utf-8 and fallback to locale encodin…
gh-77102: site: try utf-8 and locale encoding when reading .pth file (G…
methane commentedon Apr 16, 2024
3.11 is security fix only mode. I backported this only to 3.12.
vstinner commentedon Apr 16, 2024
Thanks @methane, using UTF-8 or falling back on locale encoding is a nice tradeoff.
pythongh-77102: site: try utf-8 and fallback to locale encoding when …
ncoghlan commentedon May 24, 2024
GH-119503 made a small tweak to ignore UTF-8 BOMs when reading .pth files (this aligns with the way source files are decoded)