Skip to content

.pth files cannot contain folders with utf-8 names #77102

Closed

Description

@einaren
mannequin
BPO 32921
Nosy @pfmoore, @vstinner, @tjguk, @ezio-melotti, @zware, @zooba

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2018-02-23.13:03:18.090>
labels = ['type-bug', 'expert-unicode', 'OS-windows']
title = '.pth files cannot contain folders with utf-8 names'
updated_at = <Date 2018-03-05.17:53:23.182>
user = 'https://bugs.python.org/einaren'

bugs.python.org fields:

activity = <Date 2018-03-05.17:53:23.182>
actor = 'steve.dower'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Unicode', 'Windows']
creation = <Date 2018-02-23.13:03:18.090>
creator = 'einaren'
dependencies = []
files = []
hgrepos = []
issue_num = 32921
keywords = []
message_count = 2.0
messages = ['312635', '313273']
nosy_count = 7.0
nosy_names = ['paul.moore', 'vstinner', 'tim.golden', 'ezio.melotti', 'zach.ware', 'steve.dower', 'einaren']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue32921'
versions = ['Python 3.6']

Linked PRs

Activity

  1. pfmoore commented on Apr 12, 2024

    @pfmoore
    Member

    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).

  2. methane commented on Apr 13, 2024

    @methane
    Member

    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.

  3. methane commented on Apr 13, 2024

    @methane
    Member

    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.

  4. hauntsaninja commented on Apr 14, 2024

    @hauntsaninja
    Contributor

    (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 ascii

  5. pfmoore commented on Apr 14, 2024

    @pfmoore
    Member

    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:

    1. .pth files created manually by the user, which can contain anything the user chooses.
    2. .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

    1. 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. ↩

  6. methane commented on Apr 15, 2024

    @methane
    Member

    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:

    Poetry uses locale.getpreferredencoding() that uses UTF-8 in UTF-8 mode.

    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.
  7. abravalheri commented on Apr 15, 2024

    @abravalheri

    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.

    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).

  8. methane commented on Apr 15, 2024

    @methane
    Member

    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.

  9. added 3 commits that reference this issue on Apr 16, 2024
  10. methane commented on Apr 16, 2024

    @methane
    Member

    3.11 is security fix only mode. I backported this only to 3.12.

  11. vstinner commented on Apr 16, 2024

    @vstinner
    Member

    Thanks @methane, using UTF-8 or falling back on locale encoding is a nice tradeoff.

  12. added a commit that references this issue on Apr 17, 2024
  13. ncoghlan commented on May 24, 2024

    @ncoghlan
    Contributor

    GH-119503 made a small tweak to ignore UTF-8 BOMs when reading .pth files (this aligns with the way source files are decoded)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

OS-windowstopic-unicodetype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions