Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Provide public is_editable API to check package install mode #4186

Closed
1 task done
janosh opened this issue Jan 15, 2024 · 4 comments
Closed
1 task done

[FR] Provide public is_editable API to check package install mode #4186

janosh opened this issue Jan 15, 2024 · 4 comments
Labels
enhancement Needs Triage Issues that need to be evaluated for severity and status.

Comments

@janosh
Copy link

janosh commented Jan 15, 2024

What's the problem this feature will solve?

The location of some data files in my package changes based on whether it's PyPI or editable install. I need a programmatic way to check in the package's own code if it's installed in --editable mode.

Describe the solution you'd like

A private API for this already exists in pip.

Alternative Solutions

There is an SO question with 4x as many upvotes as the highest-rated question (despite having 5 answers), indicating there's a need for this but no good existing method.

Additional context

No response

Code of Conduct

  • I agree to follow the PSF Code of Conduct
@abravalheri
Copy link
Contributor

If I am not mistaken, you can already use importlib.metadata to read the direct_url.json file that contains a recording of how the package was installed.

@janosh
Copy link
Author

janosh commented Jan 16, 2024

Thanks, I didn't know about this! This is working great!

import json
from importlib.metadata import Distribution

direct_url = Distribution.from_name("pkg-name").read_text("direct_url.json")
pkg_is_editable = json.loads(direct_url).get("dir_info", {}).get("editable", False)

@janosh janosh closed this as completed Jan 16, 2024
@abravalheri
Copy link
Contributor

abravalheri commented Jan 16, 2024

@janosh, I think it is not fully documented, but if I am not wrong the distribution.origin attribute will also give you the contents of direct_url.json as a dict (already parsed from the JSON file).

(Probably depends on the version of importlib.metadata you have, or if you are using the backfill importlib_metasdata, because the property was introduced a couple of months ago)

@janosh
Copy link
Author

janosh commented Jan 16, 2024

I briefly tried that but got AttributeError from distribution.origin. I have importlib-metadata 6.8.0.

Looks like origin was added in 6.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Needs Triage Issues that need to be evaluated for severity and status.
Projects
None yet
Development

No branches or pull requests

2 participants