Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Last active February 20, 2025 14:26
Show Gist options
  • Save Foadsf/c0c9c52db0ba1c317eedbd03cf4da6c1 to your computer and use it in GitHub Desktop.
Save Foadsf/c0c9c52db0ba1c317eedbd03cf4da6c1 to your computer and use it in GitHub Desktop.
Automated script to install Python's hunspell package on Windows by setting up vcpkg and required directory structure.

Hunspell Installer for Python on Windows

This script automatically installs the Python hunspell package on Windows by setting up the necessary environment.

What this script does

  1. Checks for and installs vcpkg if not present
  2. Installs hunspell C++ library using vcpkg
  3. Creates a virtual drive and directory structure expected by the hunspell Python package
  4. Copies required header and library files to the expected locations
  5. Installs the Python hunspell package
  6. Copies the necessary DLL to Python's DLLs directory for runtime loading
  7. Cleans up the virtual drive mapping

Requirements

  • Windows OS
  • Python (tested with Python 3.6+)
  • Administrative privileges (for virtual drive creation)
  • Internet connection
  • Microsoft Visual C++ Build Tools

Usage

  1. Save the script as install-hunspell-windows.cmd
  2. Run the script as administrator
  3. Wait for the installation to complete

Common issues

If you encounter issues, check that:

  • You're running the script as administrator
  • Visual Studio Build Tools are installed
  • No other process is using drive V:

After installation

You can use hunspell in your Python code:

import hunspell
h = hunspell.Hunspell()
print(h.spell("correct"))  # True
print(h.spell("incorrekt"))  # False
suggestions = h.suggest("incorrekt")
print(suggestions)  # ['incorrect', ...]
@echo off
echo Installing hunspell for Python on Windows...
:: Check if vcpkg is installed
if not exist C:\dev\vcpkg (
echo Creating vcpkg directory...
mkdir C:\dev\vcpkg
pushd C:\dev\vcpkg
echo Downloading vcpkg...
powershell -Command "& {Invoke-WebRequest -Uri https://github.com/microsoft/vcpkg/archive/master.zip -OutFile vcpkg.zip}"
echo Extracting vcpkg...
powershell -Command "& {Expand-Archive -Path vcpkg.zip -DestinationPath . -Force}"
move vcpkg-master\* .
rmdir /S /Q vcpkg-master
del vcpkg.zip
popd
)
:: Bootstrap vcpkg if not already bootstrapped
if not exist C:\dev\vcpkg\vcpkg.exe (
echo Bootstrapping vcpkg...
pushd C:\dev\vcpkg
call bootstrap-vcpkg.bat
popd
)
:: Install hunspell using vcpkg
echo Installing hunspell using vcpkg...
C:\dev\vcpkg\vcpkg install hunspell:x64-windows
:: Set up environment for hunspell
echo Setting up virtual drive and directories...
subst V: C:\temp
:: Create required directories
if not exist V:\hunspell-1.3.3\src\hunspell (
mkdir V:\hunspell-1.3.3\src\hunspell
)
if not exist V:\hunspell-1.3.3\src\win_api\x64\Release\libhunspell (
mkdir V:\hunspell-1.3.3\src\win_api\x64\Release\libhunspell
)
:: Copy header files and library
echo Copying header files...
copy C:\dev\vcpkg\packages\hunspell_x64-windows\include\hunspell\*.* V:\hunspell-1.3.3\src\hunspell
echo Copying library files...
copy C:\dev\vcpkg\packages\hunspell_x64-windows\lib\hunspell-1.7.lib V:\hunspell-1.3.3\src\win_api\x64\Release\libhunspell\libhunspell.lib
:: Install Python package
echo Installing hunspell Python package...
python -m pip install hunspell
:: Copy DLL to Python's DLLs directory for runtime loading
echo Copying DLL to Python directory...
for /f "delims=" %%i in ('python -c "import sys; print(sys.executable.replace('python.exe', 'DLLs'))"') do set PYTHON_DLLS_DIR=%%i
echo Copying to %PYTHON_DLLS_DIR%
copy C:\dev\vcpkg\packages\hunspell_x64-windows\bin\hunspell-1.7-0.dll "%PYTHON_DLLS_DIR%"
:: Clean up
echo Cleaning up...
subst V: /D
echo Installation complete!
echo You can now use hunspell in your Python projects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment