Comments (9)
In GitLab by @jaraco on Sep 17, 2018, 22:25
changed the description
from importlib_metadata.
In GitLab by @warsaw on Sep 17, 2018, 22:44
Is there a reason why
from importlib_metadata import version
__version__ = version(__name__)
doesn't work in the general case? E.g. with this:
1 file changed, 2 insertions(+)
importlib_metadata/__init__.py | 2 ++
modified importlib_metadata/__init__.py
@@ -73,3 +73,5 @@ def _install(): # pragma: nocover
_install()
+
+__version__ = version(__name__)
I get this:
$ .tox/py37-nocov/bin/python -c "import importlib_metadata; print(importlib_metadata.__version__)"
0.5
Sorry, I forget what the point was about __dist_name__
. Where did that come from again?
from importlib_metadata.
In GitLab by @warsaw on Sep 17, 2018, 22:45
I mean, I see it's reference in name_for_module()
but I don't remember why we added that.
from importlib_metadata.
In GitLab by @jaraco on Sep 18, 2018, 12:29
It all comes down to - do we wish to resolve a Python module/package to a (e.g. pip installed) Distribution. The reason it works for importlib_metadata.__init__
is because the module's __name__
happens to match exactly the Distribution's name.
But this technique wouldn't work in these scenarios:
When the distribution name is different, such as with path.py
(whose module __name__
is path
), or with pkg_resources
(whose distribution is setuptools
), or with importlib_metadata._common
.
There are other cases that may be problemmatic too, like with namespace packages, where jaraco/functools.py
does happen to map to the jaraco.functools
distribution, but jaraco
maps to many distributions... and when many of these namespace packages are installed together, there are modules and packages inside jaraco
that all map to different distributions.
It would be nice for all of these cases to be able to reliably resolve their distribution package without having to re-supply it as path.py does.
We did expose the distribution
function, which if supplied a module will return its distribution, but from inside a module, it's not always straightforward to get a handle on the module object (there's no __self__
on a module).
The name_for_module()
method attempts to take a handle to a module and resolve its distribution, first using the common heuristic that a module's __name__
will match it's distribution package's name (as it does with importlib_metadata
and jaraco.functools
(a module) and jaraco.classes
(a package).
And the name_for_module
also allows the value for any given module to be overridden, so a module like pkg_resources
could supply a .__dist_name__
of "setuptools", thus allowing for:
import pkg_resources
__version__ = importlib_metadata.version(pkg_resources)
But where this still falls short is in the pkg_resources module itself. If it wishes to expose its own version, it can't readily resolve on itself:
# pkg_resources.py
import importlib_metadata
__dist_name__ = 'setuptools'
__version__ = importlib_metadata.version(__name__)
In this case, it will not work because the caller isn't passing the pkg_resources module nor the distribution name (the only two inputs currently honored for the distribution
function). That's what Distribution.from_named_module
was meant to solve and why there's no suitable replacement for this recommendation.
The way I see it, we have two options. We either eliminate the concept of resolving a module to its distribution altogether and don't encourage the use of __name__
when it just happens to match. Or we try to provide a mechanism for robustly resolving a module (both from within and externally) to its distribution package.
My instinct is to adopt the first tact, removing the functionality, and then revisit options for re-introducing it.
from importlib_metadata.
In GitLab by @jaraco on Sep 18, 2018, 12:37
mentioned in commit df455f5
from importlib_metadata.
In GitLab by @jaraco on Sep 19, 2018, 01:37
closed via commit df455f5
from importlib_metadata.
In GitLab by @jaraco on Sep 19, 2018, 01:37
closed via merge request !20
from importlib_metadata.
In GitLab by @warsaw on Sep 19, 2018, 06:05
Thanks for the detailed explanation. I agree with your conclusion. Let's revisit the functionality later.
from importlib_metadata.
In GitLab by @jaraco on Sep 20, 2018, 03:09
mentioned in merge request !23
from importlib_metadata.
Related Issues (20)
- Cannot infer top-level imports from packages installed by PDM w/`install.cache=true` (aka. symlinked packages) HOT 10
- Uncaught Exception when reading Egg information HOT 6
- `__pycache__` returned as an import name from `packages_distributions()` HOT 4
- Raise if multiple `dist-info` folders for same dist exist HOT 3
- Create a Github Security Policy file HOT 3
- PathDistribution normalized name ignores name components after first "-" HOT 6
- Provide a 'diagnose' routine to help users inspect broken environments HOT 1
- ContextualVersionConflict error on jupyter notebook HOT 1
- pypy test has started failing HOT 12
- Rely on zipfile.Path (where viable) HOT 1
- Backport docs changes HOT 1
- Invalid annotation in `SimplePath` protocol HOT 2
- Change build backend to flit-core? HOT 1
- 6.8.0: pytest is failing in tests/test_api.py::APITests::test_prefix_not_matched` unit HOT 3
- PackageNotFoundError No package metadata was found for <myproject> HOT 1
- Question: parsing package names & versions from METADATA file instead of dist-info directory HOT 1
- Provide a helper for "is distribution present" HOT 2
- Mypy tests failing on Windows only HOT 1
- 6.9.0: `DiscoveryTests.test_package_discovery` fails HOT 6
- distributions() finds two packages for editable installs HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from importlib_metadata.