1

I have the following error when starting Mercurial.

hg
Traceback (most recent call last):
  File "/usr/lib64/python3/site-packages/mercurial/policy.py", line 65, in _importfrom
    fakelocals[modname] = mod = getattr(pkg, modname)
  File "/usr/lib64/python3/site-packages/mercurial/pycompat.py", line 308, in w
    return f(object, sysstr(name), *args)
AttributeError: module 'mercurial.cext' has no attribute 'parsers'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/hg", line 59, in <module>
    dispatch.run()
  File "/usr/lib64/python3.9/importlib/util.py", line 245, in __getattribute__
    self.__spec__.loader.exec_module(self)
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/lib64/python3/site-packages/mercurial/dispatch.py", line 20, in <module>
    from .i18n import _
  File "/usr/lib64/python3.9/importlib/util.py", line 245, in __getattribute__
    self.__spec__.loader.exec_module(self)
  File "/usr/lib64/python3/site-packages/mercurial/i18n.py", line 122, in <module>
    if _plain():
  File "/usr/lib64/python3/site-packages/mercurial/i18n.py", line 114, in _plain
    b'HGPLAIN' not in encoding.environ
  File "/usr/lib64/python3.9/importlib/util.py", line 245, in __getattribute__
    self.__spec__.loader.exec_module(self)
  File "/usr/lib64/python3/site-packages/mercurial/encoding.py", line 40, in <module>
    charencode = policy.importmod('charencode')
  File "/usr/lib64/python3/site-packages/mercurial/policy.py", line 112, in importmod
    mod = _importfrom(pn, mn)
  File "/usr/lib64/python3/site-packages/mercurial/policy.py", line 67, in _importfrom
    raise ImportError('cannot import name %s' % modname)
ImportError: cannot import name parsers

Important part:

ImportError: cannot import name parsers

Full code of policy.py

# policy.py - module policy logic for Mercurial.
#
# Copyright 2015 Gregory Szorc <[email protected]>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import os
import sys

from .pycompat import getattr

# Rules for how modules can be loaded. Values are:
#
#    c - require C extensions
#    rust+c - require Rust and C extensions
#    rust+c-allow - allow Rust and C extensions with fallback to pure Python
#                   for each
#    allow - allow pure Python implementation when C loading fails
#    cffi - required cffi versions (implemented within pure module)
#    cffi-allow - allow pure Python implementation if cffi version is missing
#    py - only load pure Python modules
#
# By default, fall back to the pure modules so the in-place build can
# run without recompiling the C extensions. This will be overridden by
# __modulepolicy__ generated by setup.py.
policy = b'allow'
_packageprefs = {
    # policy: (versioned package, pure package)
    b'c': ('cext', None),
    b'allow': ('cext', 'pure'),
    b'cffi': ('cffi', None),
    b'cffi-allow': ('cffi', 'pure'),
    b'py': (None, 'pure'),
    # For now, rust policies impact importrust only
    b'rust+c': ('cext', None),
    b'rust+c-allow': ('cext', 'pure'),
}

try:
    from . import __modulepolicy__

    policy = __modulepolicy__.modulepolicy
except ImportError:
    pass

# PyPy doesn't load C extensions.
#
# The canonical way to do this is to test platform.python_implementation().
# But we don't import platform and don't bloat for it here.
if '__pypy__' in sys.builtin_module_names:
    policy = b'cffi'

# Environment variable can always force settings.
if 'HGMODULEPOLICY' in os.environ:
    policy = os.environ['HGMODULEPOLICY'].encode('utf-8')


def _importfrom(pkgname, modname):
    # from .<pkgname> import <modname> (where . is looked through this module)
    fakelocals = {}
    pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1)
    try:
        fakelocals[modname] = mod = getattr(pkg, modname)
    except AttributeError:
        raise ImportError('cannot import name %s' % modname)
    # force import; fakelocals[modname] may be replaced with the real module
    getattr(mod, '__doc__', None)
    return fakelocals[modname]


# keep in sync with "version" in C modules
_cextversions = {
    ('cext', 'base85'): 1,
    ('cext', 'bdiff'): 3,
    ('cext', 'mpatch'): 1,
    ('cext', 'osutil'): 4,
    ('cext', 'parsers'): 20,
}

# map import request to other package or module
_modredirects = {
    ('cext', 'charencode'): ('cext', 'parsers'),
    ('cffi', 'base85'): ('pure', 'base85'),
    ('cffi', 'charencode'): ('pure', 'charencode'),
    ('cffi', 'parsers'): ('pure', 'parsers'),
}


def _checkmod(pkgname, modname, mod):
    expected = _cextversions.get((pkgname, modname))
    actual = getattr(mod, 'version', None)
    if actual != expected:
        raise ImportError(
            'cannot import module %s.%s '
            '(expected version: %d, actual: %r)'
            % (pkgname, modname, expected, actual)
        )


def importmod(modname):
    """Import module according to policy and check API version"""
    try:
        verpkg, purepkg = _packageprefs[policy]
    except KeyError:
        raise ImportError('invalid HGMODULEPOLICY %r' % policy)
    assert verpkg or purepkg
    if verpkg:
        pn, mn = _modredirects.get((verpkg, modname), (verpkg, modname))
        try:
            mod = _importfrom(pn, mn)
            if pn == verpkg:
                _checkmod(pn, mn, mod)
            return mod
        except ImportError:
            if not purepkg:
                raise
    pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname))
    return _importfrom(pn, mn)


def _isrustpermissive():
    """Assuming the policy is a Rust one, tell if it's permissive."""
    return policy.endswith(b'-allow')


def importrust(modname, member=None, default=None):
    """Import Rust module according to policy and availability.

    If policy isn't a Rust one, this returns `default`.

    If either the module or its member is not available, this returns `default`
    if policy is permissive and raises `ImportError` if not.
    """
    if not policy.startswith(b'rust'):
        return default

    try:
        mod = _importfrom('rustext', modname)
    except ImportError:
        if _isrustpermissive():
            return default
        raise
    if member is None:
        return mod

    try:
        return getattr(mod, member)
    except AttributeError:
        if _isrustpermissive():
            return default
        raise ImportError("Cannot import name %s" % member)

List of folder and files with size of ./usr/lib64/python3.9/site-packages/mercurial

./usr/lib64/python3.9/site-packages/mercurial:
total 5,4M
-rw-r--r--  1 root root  13K ago 30  2021 ancestor.py
-rw-r--r--  1 root root  12K ago 30  2021 archival.py
-rw-r--r--  1 root root  33K ago 30  2021 bookmarks.py
-rw-r--r--  1 root root  30K ago 30  2021 branchmap.py
-rw-r--r--  1 root root  87K ago 30  2021 bundle2.py
-rw-r--r--  1 root root  14K ago 30  2021 bundlecaches.py
-rw-r--r--  1 root root  24K ago 30  2021 bundlerepo.py
-rw-r--r--  1 root root  827 ago 30  2021 cacheutil.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 cext
drwxr-xr-x  3 root root 4,0K ago 26 04:48 cffi
-rw-r--r--  1 root root  66K ago 30  2021 changegroup.py
-rw-r--r--  1 root root  20K ago 30  2021 changelog.py
-rw-r--r--  1 root root  26K ago 30  2021 chgserver.py
-rw-r--r--  1 root root 130K ago 30  2021 cmdutil.py
-rw-r--r--  1 root root  19K ago 30  2021 color.py
-rw-r--r--  1 root root  25K ago 30  2021 commandserver.py
-rw-r--r--  1 root root 259K ago 30  2021 commands.py
-rw-r--r--  1 root root  17K ago 30  2021 commit.py
-rw-r--r--  1 root root  47K ago 30  2021 configitems.py
-rw-r--r--  1 root root 8,6K ago 30  2021 config.py
-rw-r--r--  1 root root 100K ago 30  2021 context.py
-rw-r--r--  1 root root  46K ago 30  2021 copies.py
-rw-r--r--  1 root root  71K ago 30  2021 crecord.py
-rw-r--r--  1 root root  40K ago 30  2021 dagop.py
-rw-r--r--  1 root root  16K ago 30  2021 dagparser.py
-rw-r--r--  1 root root 157K ago 30  2021 debugcommands.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 defaultrc
-rw-r--r--  1 root root  17K ago 30  2021 destutil.py
-rw-r--r--  1 root root 2,4K ago 30  2021 diffhelper.py
-rw-r--r--  1 root root 4,6K ago 30  2021 diffutil.py
-rw-r--r--  1 root root 3,1K ago 30  2021 dirstateguard.py
-rw-r--r--  1 root root  33K ago 30  2021 dirstatemap.py
-rw-r--r--  1 root root  62K ago 30  2021 dirstate.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 dirstateutils
-rw-r--r--  1 root root  23K ago 30  2021 discovery.py
-rw-r--r--  1 root root  47K ago 30  2021 dispatch.py
-rw-r--r--  1 root root 2,2K ago 30  2021 dummycert.pem
-rw-r--r--  1 root root  23K ago 30  2021 encoding.py
-rw-r--r--  1 root root  18K ago 30  2021 error.py
-rw-r--r--  1 root root  95K ago 30  2021 exchange.py
-rw-r--r--  1 root root  25K ago 30  2021 exchangev2.py
-rw-r--r--  1 root root  30K ago 30  2021 extensions.py
-rw-r--r--  1 root root  12K ago 30  2021 exthelper.py
-rw-r--r--  1 root root  12K ago 30  2021 fancyopts.py
-rw-r--r--  1 root root 8,7K ago 30  2021 filelog.py
-rw-r--r--  1 root root  43K ago 30  2021 filemerge.py
-rw-r--r--  1 root root  11K ago 30  2021 filesetlang.py
-rw-r--r--  1 root root  19K ago 30  2021 fileset.py
-rw-r--r--  1 root root  27K ago 30  2021 formatter.py
-rw-r--r--  1 root root  17K ago 30  2021 graphmod.py
-rw-r--r--  1 root root 7,3K ago 30  2021 grep.py
-rw-r--r--  1 root root  11K ago 30  2021 hbisect.py
-rw-r--r--  1 root root  37K ago 30  2021 help.py
drwxr-xr-x  4 root root 4,0K ago 26 04:48 helptext
-rw-r--r--  1 root root  52K ago 30  2021 hg.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 hgweb
-rw-r--r--  1 root root  12K ago 30  2021 hook.py
-rw-r--r--  1 root root 4,3K ago 30  2021 httpconnection.py
-rw-r--r--  1 root root  35K ago 30  2021 httppeer.py
-rw-r--r--  1 root root 4,1K sep 24  2021 i18n.py
-rw-r--r--  1 root root  416 ago 30  2021 __init__.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 interfaces
-rw-r--r--  1 root root  28K ago 30  2021 keepalive.py
-rw-r--r--  1 root root  15K ago 30  2021 linelog.py
drwxr-xr-x  2 root root 4,0K may  7 18:22 locale
-rw-r--r--  1 root root 138K ago 30  2021 localrepo.py
-rw-r--r--  1 root root  12K ago 30  2021 lock.py
-rw-r--r--  1 root root  42K ago 30  2021 logcmdutil.py
-rw-r--r--  1 root root 5,0K ago 30  2021 logexchange.py
-rw-r--r--  1 root root 4,0K ago 30  2021 loggingutil.py
-rw-r--r--  1 root root 2,8K ago 30  2021 lsprofcalltree.py
-rw-r--r--  1 root root 4,3K ago 30  2021 lsprof.py
-rw-r--r--  1 root root  18K ago 30  2021 mail.py
-rw-r--r--  1 root root  76K ago 30  2021 manifest.py
-rw-r--r--  1 root root  53K ago 30  2021 match.py
-rw-r--r--  1 root root  18K ago 30  2021 mdiff.py
-rw-r--r--  1 root root  86K ago 30  2021 merge.py
-rw-r--r--  1 root root  30K ago 30  2021 mergestate.py
-rw-r--r--  1 root root  502 ago 30  2021 mergeutil.py
-rw-r--r--  1 root root  33K ago 30  2021 metadata.py
-rw-r--r--  1 root root 3,6K ago 30  2021 minifileset.py
-rw-r--r--  1 root root  30K ago 30  2021 minirst.py
-rw-r--r--  1 root root   61 sep 24  2021 __modulepolicy__.py
-rw-r--r--  1 root root 8,2K ago 30  2021 namespaces.py
-rw-r--r--  1 root root  12K ago 30  2021 narrowspec.py
-rw-r--r--  1 root root 2,1K ago 30  2021 node.py
-rw-r--r--  1 root root  37K ago 30  2021 obsolete.py
-rw-r--r--  1 root root  36K ago 30  2021 obsutil.py
-rw-r--r--  1 root root  26K ago 30  2021 parser.py
-rw-r--r--  1 root root 102K ago 30  2021 patch.py
-rw-r--r--  1 root root  13K ago 30  2021 pathutil.py
-rw-r--r--  1 root root  33K ago 30  2021 phases.py
-rw-r--r--  1 root root 4,8K ago 30  2021 policy.py
-rw-r--r--  1 root root  24K ago 30  2021 posix.py
-rw-r--r--  1 root root 8,7K ago 30  2021 profiling.py
-rw-r--r--  1 root root  11K ago 30  2021 progress.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 pure
-rw-r--r--  1 root root 1,7K ago 30  2021 pushkey.py
-rw-r--r--  1 root root 6,1K ago 30  2021 pvec.py
drwxr-xr-x  2 root root  12K ago 26 04:48 __pycache__
-rw-r--r--  1 root root  17K ago 30  2021 pycompat.py
-rw-r--r--  1 root root 3,6K ago 30  2021 rcutil.py
-rw-r--r--  1 root root  18K ago 30  2021 registrar.py
-rw-r--r--  1 root root  18K ago 30  2021 repair.py
-rw-r--r--  1 root root 4,5K ago 30  2021 repocache.py
-rw-r--r--  1 root root  17K ago 30  2021 repoview.py
-rw-r--r--  1 root root 3,4K ago 30  2021 requirements.py
-rw-r--r--  1 root root 118K ago 30  2021 revlog.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 revlogutils
-rw-r--r--  1 root root  29K ago 30  2021 revsetlang.py
-rw-r--r--  1 root root  88K ago 30  2021 revset.py
-rw-r--r--  1 root root 8,3K ago 30  2021 rewriteutil.py
-rw-r--r--  1 root root 2,6K ago 30  2021 scmposix.py
-rw-r--r--  1 root root  76K ago 30  2021 scmutil.py
-rw-r--r--  1 root root 3,4K ago 30  2021 scmwindows.py
-rw-r--r--  1 root root 7,1K ago 30  2021 server.py
-rw-r--r--  1 root root  19K ago 30  2021 setdiscovery.py
-rw-r--r--  1 root root  38K ago 30  2021 shelve.py
-rw-r--r--  1 root root 4,0K ago 30  2021 similar.py
-rw-r--r--  1 root root  19K ago 30  2021 simplemerge.py
-rw-r--r--  1 root root  34K ago 30  2021 smartset.py
-rw-r--r--  1 root root  26K ago 30  2021 sparse.py
-rw-r--r--  1 root root  25K ago 30  2021 sshpeer.py
-rw-r--r--  1 root root  32K ago 30  2021 sslutil.py
-rw-r--r--  1 root root  762 ago 30  2021 stack.py
-rw-r--r--  1 root root  12K ago 30  2021 state.py
-rw-r--r--  1 root root 7,7K ago 30  2021 statichttprepo.py
-rw-r--r--  1 root root  33K ago 30  2021 statprof.py
-rw-r--r--  1 root root  25K ago 30  2021 store.py
-rw-r--r--  1 root root  31K ago 30  2021 streamclone.py
-rw-r--r--  1 root root 8,7K ago 30  2021 strip.py
-rw-r--r--  1 root root  71K ago 30  2021 subrepo.py
-rw-r--r--  1 root root  18K ago 30  2021 subrepoutil.py
-rw-r--r--  1 root root  12K ago 30  2021 tagmerge.py
-rw-r--r--  1 root root  30K ago 30  2021 tags.py
-rw-r--r--  1 root root  16K ago 30  2021 templatefilters.py
-rw-r--r--  1 root root  31K ago 30  2021 templatefuncs.py
-rw-r--r--  1 root root  34K ago 30  2021 templatekw.py
-rw-r--r--  1 root root  38K ago 30  2021 templater.py
drwxr-xr-x 13 root root 4,0K ago 26 04:48 templates
-rw-r--r--  1 root root  36K ago 30  2021 templateutil.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 testing
drwxr-xr-x  5 root root 4,0K ago 26 04:48 thirdparty
-rw-r--r--  1 root root  26K ago 30  2021 transaction.py
-rw-r--r--  1 root root 6,0K ago 30  2021 treediscovery.py
-rw-r--r--  1 root root 1,1K ago 30  2021 txnutil.py
-rw-r--r--  1 root root  81K ago 30  2021 ui.py
-rw-r--r--  1 root root 9,7K ago 30  2021 unionrepo.py
-rw-r--r--  1 root root  13K ago 30  2021 upgrade.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 upgrade_utils
-rw-r--r--  1 root root 6,0K ago 30  2021 urllibcompat.py
-rw-r--r--  1 root root  25K ago 30  2021 url.py
-rw-r--r--  1 root root  97K ago 30  2021 util.py
drwxr-xr-x  3 root root 4,0K ago 26 04:48 utils
-rw-r--r--  1 root root  22K ago 30  2021 verify.py
-rw-r--r--  1 root root   60 ago 30  2021 __version__.py
-rw-r--r--  1 root root  25K ago 30  2021 vfs.py
-rw-r--r--  1 root root  23K ago 30  2021 win32.py
-rw-r--r--  1 root root  22K ago 30  2021 windows.py
-rw-r--r--  1 root root  65K ago 30  2021 wireprotoframing.py
-rw-r--r--  1 root root  28K ago 30  2021 wireprotoserver.py
-rw-r--r--  1 root root  14K ago 30  2021 wireprototypes.py
-rw-r--r--  1 root root  23K ago 30  2021 wireprotov1peer.py
-rw-r--r--  1 root root  27K ago 30  2021 wireprotov1server.py
-rw-r--r--  1 root root  19K ago 30  2021 wireprotov2peer.py
-rw-r--r--  1 root root  49K ago 30  2021 wireprotov2server.py
-rw-r--r--  1 root root  16K ago 30  2021 worker.py
-rwxr-xr-x  1 root root 669K may  7 18:22 zstd.cpython-36m-x86_64-linux-gnu.so

./usr/lib64/python3.9/site-packages/mercurial/cext:
total 176K
-rwxr-xr-x 1 root root  11K may  7 18:22 base85.cpython-36m-x86_64-linux-gnu.so
-rwxr-xr-x 1 root root  31K may  7 18:22 bdiff.cpython-36m-x86_64-linux-gnu.so
-rw-r--r-- 1 root root    0 ago 30  2021 __init__.py
-rwxr-xr-x 1 root root  15K may  7 18:22 mpatch.cpython-36m-x86_64-linux-gnu.so
-rwxr-xr-x 1 root root  20K may  7 18:22 osutil.cpython-36m-x86_64-linux-gnu.so
-rwxr-xr-x 1 root root  89K may  7 18:22 parsers.cpython-36m-x86_64-linux-gnu.so
drwxr-xr-x 2 root root 4,0K ago 26 04:48 __pycache__

./usr/lib64/python3.9/site-packages/mercurial/cext/__pycache__:
total 4,0K
-rw-r--r-- 1 root root 143 may  7 18:22 __init__.cpython-36.pyc

How do I solve it?

1 Answer 1

0

Import full path of .so module:

import importlib.util
name = "parsers"
url = "/usr/lib64/python3.9/site-packages/mercurial/cext/parsers.cpython-36m-x86_64-linux-gnu.so"
parsers = importlib.util.spec_from_file_location(name, url)

Alternative or for complement, change encoding.py:41

charencode = policy.importmod('charencode')

Replace by:

charencode = charencodepure

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .