hanlp.utils.io_util.check_outdated(package='hanlp', version='2.1.0-beta.54', repository_url='')[source]

Given the name of a package on PyPI and a version (both strings), checks if the given version is the latest version of the package available. Returns a 2-tuple (installed_version, latest_version) repository_url is a % style format string to use a different repository PyPI repository URL, e.g. or a private repository. The string is formatted with the package name. Adopted from

  • package – Package name.

  • version – Installed version string.

  • repository_url – URL on pypi.


Parsed installed version and latest version.


Execute the external command and get its exitcode, stdout and stderr. See


cmd – Command.


Exit code, stdout, stderr.

hanlp.utils.io_util.get_resource(path: str, save_dir='/Users/hankcs/.hanlp', extract=True, prefix='', append_location=True, verbose=False)[source]

Fetch real (local) path for a resource (model, corpus, whatever) to save_dir.

  • path – A local path (which will returned as is) or a remote URL (which will be downloaded, decompressed then returned).

  • save_dir – Where to store the resource (Default value = hanlp.utils.io_util.hanlp_home())

  • extract – Whether to unzip it if it’s a zip file (Default value = True)

  • prefix – A prefix when matched with an URL (path), then that URL is considered to be official. For official resources, they will not go to a folder called thirdparty under HANLP_HOME.

  • append_location – Whether to put unofficial files in a thirdparty folder.

  • verbose – Whether to print log messages.


The real path to the resource.


Home directory for HanLP resources.


Data directory in the filesystem for storage, for example when downloading models.

This home directory can be customized with the following shell command or equivalent environment variable on Windows systems.

$ export HANLP_HOME=/data/hanlp

Default data directory depending on the platform and environment variables

hanlp.utils.io_util.path_from_url(url, save_dir='/Users/hankcs/.hanlp', prefix='', append_location=True)[source]

Map a URL to a local path.

  • url – Remote URL.

  • save_dir – The root folder to save this file.

  • prefix – The prefix of official website. Any URLs starting with this prefix will be considered official.

  • append_location – Whether to put unofficial files in a thirdparty folder.


The real path that this URL is mapped to.

hanlp.utils.io_util.replace_ext(filepath, ext) str[source]

Replace the extension of filepath to ext.

  • filepath – Filepath to be replaced.

  • ext – Extension to replace.


A new path.

hanlp.utils.io_util.stdout_redirected(to='/dev/null', stdout=None)[source]

Redirect stdout to else where. Copied from

  • to – Target device.

  • stdout – Source device.

hanlp.utils.io_util.uncompress(path, dest=None, remove=True, verbose=False)[source]

Uncompress a file and clean up uncompressed files once an error is triggered.

  • path – The path to a compressed file

  • dest – The dest folder.

  • remove – Remove archive file after decompression.

  • verboseTrue to print log message.


Destination path.