Title: Can't open orphan path on bare importlib.resources.files() under zipapp · Issue #121735 · python/cpython · GitHub
Open Graph Title: Can't open orphan path on bare importlib.resources.files() under zipapp · Issue #121735 · python/cpython
X Title: Can't open orphan path on bare importlib.resources.files() under zipapp · Issue #121735 · python/cpython
Description: Bug report Bug description: Context: In Python 3.12 the function importlib.resources.files() was updated: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files Changed in version 3.12: package parameter was...
Open Graph Description: Bug report Bug description: Context: In Python 3.12 the function importlib.resources.files() was updated: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files Change...
X Description: Bug report Bug description: Context: In Python 3.12 the function importlib.resources.files() was updated: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files Change...
Opengraph URL: https://github.com/python/cpython/issues/121735
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can't open orphan path on bare importlib.resources.files() under zipapp","articleBody":"# Bug report\n\n### Bug description:\n\nContext: In Python 3.12 the function importlib.resources.files() was updated: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files\n\n\u003e Changed in version 3.12: package parameter was renamed to anchor. anchor can now be a non-package module and if omitted will default to the caller’s module. package is still accepted for compatibility but will raise a [DeprecationWarning](https://docs.python.org/3/library/exceptions.html#DeprecationWarning). Consider passing the anchor positionally or using importlib_resources \u003e= 5.10 for a compatible interface on older Pythons.\n\nThe Issue: When using `importlib.resources.files()`, I am able to open and read a text file included as package data in my package. However, if I zip up my package with zipapp and run the .pyz file, it is unable to read the text file.\nIf I use `importlib.resources.files(\"mypkg\")` it will work in both cases.\n\nMinimal example:\n\nCreate a project directory named `mypkg` with the [src layout](https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#src-layout).\n\nCreate the following files.\n\nmypkg/src/mypkg/a.py\n```python\nimport importlib.resources\n\ndef main():\n x = importlib.resources.files() / \"data.txt\"\n print(x.read_text())\n```\nmypkg/src/mypkg/data.txt\n```text\ndatadata\n```\n\nIn pyproject.toml make sure to set up the entry script and include the data file.\n\nmypkg/pyproject.toml\n```\n[project.scripts]\nmyscript = \"mypkg.a:main\"\n\n[tool.setuptools.package-data]\nmypkg = [\"*.txt\"]\n```\n\nInstall the package into a fresh Python (virtual) environment with pip.\nRun `myscript` and verify it prints `datadata`.\n\nNow back in the mypkg project root folder, we will generate a .pyz file with zipapp and run the program from the .pyz file.\n\nRun `pip install . --target buildtemp --upgrade \u0026\u0026 python3 -m zipapp buildtemp --main mypkg.a:main -p '/usr/bin/env python3' -o mypkg.pyz` in the terminal.\n\nRun `./mypkg.pyz`. This results in an error:\n```\nTraceback (most recent call last):\n File \"\u003cfrozen runpy\u003e\", line 198, in _run_module_as_main\n File \"\u003cfrozen runpy\u003e\", line 88, in _run_code\n File \"/home/win/code/sandbox/python/mypkg/./mypkg.pyz/__main__.py\", line 3, in \u003cmodule\u003e\n File \"/home/win/code/sandbox/python/mypkg/./mypkg.pyz/mypkg/a.py\", line 6, in main\n File \"/home/win/.local/share/mise/installs/python/3.12.4/lib/python3.12/importlib/resources/abc.py\", line 89, in read_text\n with self.open(encoding=encoding) as strm:\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/win/.local/share/mise/installs/python/3.12.4/lib/python3.12/importlib/resources/_adapters.py\", line 139, in open\n raise FileNotFoundError(\"Can't open orphan path\")\nFileNotFoundError: Can't open orphan path\n```\n\nNote that if you change `x = importlib.resources.files() / \"data.txt\"` to `x = importlib.resources.files(\"mypkg\") / \"data.txt\"`, it will work when running the .pyz file.\n\n\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nLinux, macOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-123037\n* gh-123986\n* gh-124011\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/winstontsai","@type":"Person","name":"winstontsai"},"datePublished":"2024-07-14T06:56:00.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":16},"url":"https://github.com/121735/cpython/issues/121735"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:47d6f5a5-2941-9274-3b53-da6564fa96c4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BCB4:31E362:178D4B5:1FA1365:6969BEB2 |
| html-safe-nonce | a09bd1e762a9e39301f94df76e0c558ed78db03f9fdb52b9ace3328eeae9dc98 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0I0OjMxRTM2MjoxNzhENEI1OjFGQTEzNjU6Njk2OUJFQjIiLCJ2aXNpdG9yX2lkIjoiMjYwNjMyMjU3MzczMjU5MzMzMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ca5414a6dd59e8afbab24e399a35ff692f5d29d7618eefe1495ecd320a49e6d3 |
| hovercard-subject-tag | issue:2407296772 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/121735/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b8cc8853465b5ca32d8236cbeb194e72ca5e3374f90bbcde5ed4a399723b2068/python/cpython/issues/121735 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b8cc8853465b5ca32d8236cbeb194e72ca5e3374f90bbcde5ed4a399723b2068/python/cpython/issues/121735 |
| og:image:alt | Bug report Bug description: Context: In Python 3.12 the function importlib.resources.files() was updated: https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files Change... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | winstontsai |
| hostname | github.com |
| expected-hostname | github.com |
| None | acedec8b5f975d9e3d494ddd8f949b0b8a0de59d393901e26f73df9dcba80056 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 83c08c21cdda978090dc44364b71aa5bc6dcea79 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width