question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stop relying on `__file__`

See original GitHub issue

Description

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

  1. Use an interpreter that doesn’t define __file__. PyOxy is one of them: https://gregoryszorc.com/docs/pyoxy/main/
  2. 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

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
pfmoorecommented, Dec 9, 2022

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 like importlib.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 use importlib.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 use importlib.resources instead - or if it’s not possible, identify the missing functionality and get it added to importlib.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).

1reaction
pradyunsgcommented, Dec 9, 2022

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:

  1. It’s contingent on all our vendored dependencies being on-board. I am not comfortable with the idea of pushing this as a constraint on pip’s dependencies as “because pip wants to do it”, so whomever talks to them would need to advocate for removing reliance __file__ on its own merits.
  2. We’re not going to add additional patches to our vendored dependencies, to adapt for this.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found