hankcs/HanLP · error · EnvironmentError

Your python command needs to be Python2, not {version.strip(

Error message

Your python command needs to be Python2, not {version.strip()}. Try:

	ln -sf "$(which python2)" "$(which python)"

What it means

Building the gold CoNLL files for OntoNotes 5 uses the official LDC scripts, which are Python 2 only. ensure_python_points_to_python2 runs `python --version`; if it doesn't start with 'Python 2', it raises EnvironmentError with a suggested symlink fix because the pipeline depends on a python2 interpreter being reachable as `python`.

Source

Thrown at hanlp/datasets/srl/ontonotes5/_utils.py:410

        print(f'Converting CoNLL file {full_file} to json file {v5_json_file}')
        labels, stats = convert_to_jsonlines(full_file, v5_json_file, language)
        print('Labels:')
        pprint(labels)
        print('Statistics:')
        pprint(stats)
        conll12_json_file = f'{lang_dir}/{split}.{language}.conll12.jsonlines'
        print(f'Applying CoNLL 12 official splits on {v5_json_file} to {conll12_json_file}')
        id_file = get_resource(f'https://file.hankcs.com/research/emnlp2021/conll.cemantix.org.zip#2012/download/ids/'
                               f'{language}/coref/{split}.id')
        filter_data(v5_json_file, conll12_json_file, id_file)


def ensure_python_points_to_python2():
    exitcode, out, version = get_exitcode_stdout_stderr('python --version')
    if not version:
        version = out
    if not version.startswith('Python 2'):
        raise EnvironmentError(f'Your python command needs to be Python2, not {version.strip()}. Try:\n\n\t'
                               'ln -sf "$(which python2)" "$(which python)"')


def make_gold_conll(ontonotes_path, language):
    ensure_python_points_to_python2()
    ontonotes_path = os.path.abspath(get_resource(ontonotes_path))
    to_conll = get_resource(
        'https://gist.githubusercontent.com/hankcs/46b9137016c769e4b6137104daf43a92/raw/66369de6c24b5ec47696ae307591f0d72c6f3f02/ontonotes_to_conll.sh')
    to_conll = os.path.abspath(to_conll)
    # shutil.rmtree(os.path.join(ontonotes_path, 'conll-2012'), ignore_errors=True)
    with pushd(ontonotes_path):
        try:
            flash(f'Converting [blue]{language}[/blue] to CoNLL format, '
                  f'this might take half an hour [blink][yellow]...[/yellow][/blink]')
            run_cmd(f'bash {to_conll} {ontonotes_path} {language}')
            flash('')
        except RuntimeError as e:
            flash(f'[red]Failed[/red] to convert {language} of {ontonotes_path} to CoNLL. See exceptions for detail')

View on GitHub (pinned to ddb1299bdd)

Solutions

  1. Install python2 and make `python` resolve to it in that shell: sudo apt install python2 && ln -sf "$(which python2)" "$(which python)" (or use an alias/PATH shim only for the conversion step)
  2. Activate an environment (conda create -n py2 python=2.7) where python is 2.x before running make_gold_conll
  3. Alternatively download pre-converted OntoNotes _conll files instead of converting yourself

Example fix

# shell
sudo apt-get install -y python2
ln -sf "$(which python2)" "$(which python)"
python --version   # Python 2.7.x
python -m hanlp ... # or your conversion script
Defensive patterns

Strategy: validation

Validate before calling

import subprocess, shutil
if shutil.which('python2') is None:
    raise SystemExit('python2 required for OntoNotes conversion scripts; install it first')
out = subprocess.run(['python', '--version'], capture_output=True, text=True)
if not (out.stdout or out.stderr).startswith('Python 2'):
    raise SystemExit("point 'python' at python2 for this step (see ln -sf suggestion)")

Prevention

When it happens

Trigger: Running make_gold_conll (or hanlp datasets preparation for srl/ontonotes5) on modern systems where `python` is Python 3; environments without python2 installed at all.

Common situations: Ubuntu 20.04+/Debian where python2 is not installed; conda/venv environments where python points to python3; CI images with only Python 3.

Related errors


AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27). Data as JSON: /api/errors/cc7c026da47fb679. Report an issue: GitHub.