indygreg / indygreg/PyOxidizer

Stop requiring `__file__` in Python packages

Open
#69 18 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

compatibility
Dominant language
Rust
Stars
6.2k
Forks
256
PR merge metrics
No merged PRs in 30d

Description

# The Problem

Many Python modules and scripts use `__file__` to derive the filesystem path to the current file.

As documented at https://docs.python.org/3/reference/datamodel.html (search for `__file__`), `__file__` is optional (_the `__file__` attribute may be missing for certain types of modules_).

However, because Python has traditionally relied on filesystem-based imports and hasn't had a stable story around non-module _resource_ handling, , `__file__` is almost always defined and has been used to locate and load files next to Python source files for seemingly forever. This is arguably tolerable. But reliance on `__file__` undermines tools - like PyOxidizer - which don't import Python modules from the filesystem. This in turn constrains the flexibility and utility of the larger Python ecosystem.

# The Solution

Python code should be rewritten to not assume the existence of `__file__`. By doing so, Python code will be more compatible with more Python execution environments (such as PyOxidizer), and this benefits the overall Python ecosystem.

Instructions for writing portable Python code that doesn't rely on `__file__` can be found at https://pyoxidizer.readthedocs.io/en/latest/packaging_pitfalls.html#reliance-on-file.

# This Issue

This issue can serve as a focal point for tracking and coordinating Python packages and tools which currently rely on `__file__` but shouldn't. If you file a GitHub issue against a project that relies on `__file__`, you can reference this issue by typing `indygreg/PyOxidizer#69` and provide Python project maintainers with enough context to make informed decisions about the use of `__file__` in their projects.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue text and the linked PyOxidizer documentation on reliance on `__file__`. Identify Python packages or tools that assume `__file__` exists, then determine how each can handle resources without filesystem-based module paths. Done means the relevant projects no longer require `__file__` in execution environments where it is absent.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.