Stop relying on `__file__`
See original GitHub issueDescription
As https://github.com/indygreg/PyOxidizer/issues/69 explains, __file__
is optional, and in environments running pip
where the interpreter doesn’t define it: 💥
An easy way to see this in action is pyoxy run-python -- -m pip --version
(releases are here: https://gregoryszorc.com/docs/pyoxy/main/)
Expected behavior
No response
pip version
(main
)
Python version
PyOxy 0.2.0
OS
Ubuntu 20
How to Reproduce
- Use an interpreter that doesn’t define
__file__
. PyOxy is one of them: https://gregoryszorc.com/docs/pyoxy/main/ - Run
pip
with that interpreter
Output
Traceback (most recent call last):
File "runpy", line 194, in _run_module_as_main
File "runpy", line 87, in _run_code
File "pip.__main__", line 29, in <module>
File "pip._internal.cli.main", line 9, in <module>
File "pip._internal.cli.autocompletion", line 10, in <module>
File "pip._internal.cli.main_parser", line 8, in <module>
File "pip._internal.cli.cmdoptions", line 22, in <module>
File "pip._vendor", line 23, in <module>
NameError: name '__file__' is not defined
Code of Conduct
- I agree to follow the PSF Code of Conduct.
Issue Analytics
- State:
- Created 9 months ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Stop Relying on Cloud File Stores as a Backup Strategy
There is a growing assumption that Cloud file-storage services represent an ideal way of backing up files. It seems a compelling idea ...
Read more >How do I stop rake from treating a file dependency as ...
I have a problem: a file dependency I need for one rake task matches the name of another rake task, so instead of...
Read more >Stop relying on file extensions
The main reason why such files have exotic extensions is to try to bypass dump filters based on regex like:
Read more >Ransomware 101
Ransomware is an ever-evolving form of malware designed to encrypt files on a device, rendering any files and the systems that rely on...
Read more >Compose file version 3 reference
Compose file reference. ... It's recommended that you use reverse-DNS notation to prevent your labels from ... Use none to disable networking during...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To be more explicit, I’m only in favour if someone can solve all the issues in doing this. And I’m not at all convinced that those issues are actually solveable…
In particular, I agree regarding dependencies. And I’d go further still - we need a solution that means that we don’t have to go through this whole process again before adding any new dependencies. So sorting out our current dependencies is only part of the issue. Also, I agree that asking dependencies to stop using
__file__
“for pip’s sake” is unacceptable.TBH, I’m not convinced by the arguments in the linked PyOxidizer issue. The docs say that
__file__
may not be present “for certain types of module”. Pure Python files are a “type of module” that does have__file__
, and I think it’s legitimate to use for that reason. Of course, if the Python code isn’t hosted on a filesystem, expecting to navigate the filesystem via__file__
won’t work, and the value of__file__
may only be of use for informational purposes. But we have libraries likeimportlib.resources
to handle those needs in an import mechanism independent way. I’d expect PyOxidizer to support such mechanisms if they expect people to stop using__file__
. And I would support educating people to useimportlib.resources
rather than accessing the filesystem directly, just as a general “good practice”. So if PyOxidizer works with mechanisms like that, the problem will solve itself in due course.(Someone still needs to go through existing uses of
__file__
, in pip and elsewhere, and identify how to useimportlib.resources
instead - or if it’s not possible, identify the missing functionality and get it added toimportlib.resources
. This isn’t a “magic wand” that will fix everyone’s issues, it’s just a way to work on them within the context of existing approaches to the problem).I’m ambivalent on this and, at least, Paul is in favour of this. I’ve labelled this to say that we’re open to accepting PRs toward this. I do have a couple of concerns / caveats around this:
__file__
on its own merits.