pypa/pip · error · TypeError

`base` parameter in `_fn` is `None`. Either override this…

Error message

`base` parameter in `_fn` is `None`. Either override this method or check the parameter first.

What it means

Raised by NullProvider._fn() when the `base` argument is None. _fn() joins base with the resource_name to form a filesystem path; if base (typically the module's directory, derived from module.__file__) is None, there is no path root to join against, so TypeError is raised. This happens when the module has no __file__ attribute.

Solutions

  1. Ensure the target package/module has a real __file__ on the filesystem before using pkg_resources resource APIs.
  2. Use importlib.resources which handles package resources more robustly for packages without __file__.
  3. Override _fn() in a custom provider if you control the loader and have an alternative path root.

Example fix

// before
# module is a namespace pkg without __file__
path = provider._fn(provider.module_path, 'data.txt')  # module_path derived None
// after
# use importlib.resources for packages without __file__
import importlib.resources
with importlib.resources.files('mypkg') as pkg_dir:
    path = pkg_dir / 'data.txt'
Defensive patterns

Strategy: type-guard

Validate before calling

import importlib.resources
# prefer importlib.resources for modules without __file__
if getattr(module, '__file__', None) is None:
    raise RuntimeError(f'{module} has no __file__; use importlib.resources')

Type guard

def module_has_file(module) -> bool:
    return getattr(module, '__file__', None) is not None

Try / catch

try:
    provider._fn(provider.module_path, name)
except TypeError:
    # module has no __file__; fall back to importlib.resources

Prevention

When it happens

Trigger: Calling a resource API on a module that has no __file__ (e.g. a built-in module, a namespace package, a module created dynamically, or a frozen module without a file path), so NullProvider.__init__ sets self.module_path = '' and base ends up None.

Common situations: Trying to access resources of a built-in module (like 'os' or 'json' in some contexts), a namespace package, or a module loaded in an embedded/frozen environment where __file__ is absent.

Related errors


AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08). Data as JSON: /api/errors/09189ed34f43658b. Report an issue: GitHub.

Appendix: source

Thrown at src/pip/_vendor/pkg_resources/__init__.py:1744

    def _has(self, path) -> bool:
        raise NotImplementedError(
            "Can't perform this operation for unregistered loader type"
        )

    def _isdir(self, path) -> bool:
        raise NotImplementedError(
            "Can't perform this operation for unregistered loader type"
        )

    def _listdir(self, path) -> list[str]:
        raise NotImplementedError(
            "Can't perform this operation for unregistered loader type"
        )

    def _fn(self, base: str | None, resource_name: str):
        if base is None:
            raise TypeError(
                "`base` parameter in `_fn` is `None`. Either override this method or check the parameter first."
            )
        self._validate_resource_path(resource_name)
        if resource_name:
            return os.path.join(base, *resource_name.split('/'))
        return base

    @staticmethod
    def _validate_resource_path(path):
        """
        Validate the resource paths according to the docs.
        https://setuptools.pypa.io/en/latest/pkg_resources.html#basic-resource-access

        >>> warned = getfixture('recwarn')
        >>> warnings.simplefilter('always')
        >>> vrp = NullProvider._validate_resource_path
        >>> vrp('foo/bar.txt')
        >>> bool(warned)

View on GitHub (pinned to f399c37189)