{"id":4643,"library":"natto-py","title":"natto-py (MeCab binding)","description":"natto-py is a Python package that provides a Foreign Function Interface (FFI) binding to MeCab, the part-of-speech and morphological analyzer for the Japanese language. It allows Python applications to leverage MeCab's capabilities without requiring SWIG or a C compiler for installation. The current version is 1.0.1, and the library is actively maintained with an irregular release cadence.","status":"active","version":"1.0.1","language":"en","source_language":"en","source_url":"https://github.com/buruzaemon/natto-py","tags":["NLP","Japanese","MeCab","text-processing","morphological-analysis","ffi"],"install":[{"cmd":"pip install natto-py","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"natto-py is an FFI binding to the MeCab library and requires a pre-existing MeCab installation (v0.996 or greater) and a system dictionary (e.g., IPA, Unidic) on the host machine. It does not bundle MeCab itself.","package":"MeCab (system-wide)","optional":false},{"reason":"Used for the Foreign Function Interface to MeCab. Automatically installed as a Python dependency with natto-py.","package":"cffi","optional":false}],"imports":[{"symbol":"MeCab","correct":"from natto import MeCab"},{"note":"While MeCabNode instances are often yielded by MeCab.parse(as_nodes=True), direct import is also possible.","symbol":"MeCabNode","correct":"from natto.mecab import MeCabNode"}],"quickstart":{"code":"import os\nfrom natto import MeCab\n\n# Optional: Set MeCab path and charset if auto-detection fails\n# os.environ['MECAB_PATH'] = os.environ.get('MECAB_PATH', '/usr/local/lib/libmecab.so')\n# os.environ['MECAB_CHARSET'] = os.environ.get('MECAB_CHARSET', 'utf8')\n\n# Instantiate MeCab with recommended options for detailed parsing\n# -F: node-format for features, -U: unk-format for unknown words\nwith MeCab(r'-F%m,%f[0],%f[1],%f[2],%f[3],%f[4],%f[5],%f[6],%f[7],%f[8]\\n -U?,?,?,?,?,?,?,?,?,?\\n') as nm:\n    text = 'これは日本語のテキストです。'\n    print(f\"Parsed text (string output):\\n{nm.parse(text)}\\n\")\n\n    print(\"Parsed text (node output with features):\")\n    for n in nm.parse(text, as_nodes=True):\n        if not n.is_eos(): # Ignore end-of-sentence nodes\n            print(f'Surface: {n.surface}, Feature: {n.feature}, Cost: {n.cost}')","lang":"python","description":"This quickstart demonstrates how to initialize `natto-py` with MeCab and perform both string-based and node-based parsing. The `with` statement is used for proper resource management, and MeCab options are set to obtain detailed morphological features for each node."},"warnings":[{"fix":"Upgrade to Python 3.7+ or pin `natto-py==0.9.2` for Python 2 projects.","message":"Version 1.0.0 of natto-py formally dropped support for Python 2.x. Users who still require Python 2 compatibility must use `natto-py==0.9.2`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure MeCab and a system dictionary are installed and accessible on your system's PATH, or explicitly set `os.environ['MECAB_PATH']` and `os.environ['MECAB_CHARSET']` in your Python code before importing `natto`.","message":"natto-py requires a pre-installed MeCab library (v0.996+) and a system dictionary (e.g., IPA, Unidic) on the operating system. These are not bundled. If natto-py cannot automatically locate MeCab, you may need to set the `MECAB_PATH` and `MECAB_CHARSET` environment variables.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap `MeCab` instantiation in a `with` statement: `with MeCab(...) as nm:`.","message":"For robust applications, it is highly recommended to instantiate `MeCab` using a Python `with` statement (e.g., `with MeCab() as nm:`). This ensures that MeCab's internal resources are properly cleaned up when the object goes out of scope, preventing potential memory leaks or crashes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pass formatting options like `r'-F%m,%f[0],%f[1]\n -U?,?,?\n'` to the `MeCab` constructor and iterate over `MeCabNode` instances using `nm.parse(text, as_nodes=True)`.","message":"For detailed morphological analysis, prefer parsing with `as_nodes=True` and customizing MeCab's output format using the `-F` (node-format) and `-U` (unknown-format) options during `MeCab` instantiation. This provides structured `MeCabNode` objects with rich features, avoiding brittle manual string parsing.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}