flashlight / text

Text utilities, including beam search decoding, tokenizing, and more, built for use in Flashlight.
MIT License
64 stars 14 forks source link

Install Python bindings with with cmake only #85

Open doronbehar opened 5 months ago

doronbehar commented 5 months ago

Bug Description

Hello, I'm trying to package this as a python package for NixOS, and I'm experiencing trouble due to the mixed cmake / setup.py layout. When I'm trying to use pip build and pip install, I get this error:

Creating a wheel...
WARNING: The directory '/homeless-shelter/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
Processing /build/source
  Running command python setup.py egg_info
  running egg_info
  creating /build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info
  writing /build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info/PKG-INFO
  writing dependency_links to /build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info/dependency_links.txt
  writing top-level names to /build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info/top_level.txt
  writing manifest file '/build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info/SOURCES.txt'
  reading manifest file '/build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info/SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  warning: no previously-included files matching '*.o' found anywhere in distribution
  warning: no previously-included files matching '*.so' found anywhere in distribution
  warning: no previously-included files matching '*.dylib' found anywhere in distribution
  warning: no previously-included files matching '*.a' found anywhere in distribution
  warning: no previously-included files matching '.git' found anywhere in distribution
  warning: no previously-included files matching '*.pyc' found anywhere in distribution
  warning: no previously-included files matching '*.swp' found anywhere in distribution
  warning: no files found matching 'requirements.txt'
  adding license file 'LICENSE'
  writing manifest file '/build/pip-pip-egg-info-rg0i3sl6/flashlight_text.egg-info/SOURCES.txt'
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: flashlight-text
  Running command python setup.py bdist_wheel
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-cpython-312
  creating build/lib.linux-x86_64-cpython-312/flashlight
  creating build/lib.linux-x86_64-cpython-312/flashlight/lib
  creating build/lib.linux-x86_64-cpython-312/flashlight/lib/text
  copying bindings/python/flashlight/lib/text/__init__.py -> build/lib.linux-x86_64-cpython-312/flashlight/lib/text
  copying bindings/python/flashlight/lib/text/dictionary.py -> build/lib.linux-x86_64-cpython-312/flashlight/lib/text
  copying bindings/python/flashlight/lib/text/version.py -> build/lib.linux-x86_64-cpython-312/flashlight/lib/text
  creating build/lib.linux-x86_64-cpython-312/flashlight/lib/text/decoder
  copying bindings/python/flashlight/lib/text/decoder/__init__.py -> build/lib.linux-x86_64-cpython-312/flashlight/lib/text/decoder
  copying bindings/python/flashlight/lib/text/decoder/kenlm.py -> build/lib.linux-x86_64-cpython-312/flashlight/lib/text/decoder
  running build_ext
  error: <urlopen error [Errno -3] Temporary failure in name resolution>
  error: subprocess-exited-with-error

  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> See above for output.

  note: This error originates from a subprocess, and is likely not a problem with pip.
  full command: /nix/store/dqsflsigjcxisziiynrxlbsm2y71m4bq-python3-3.12.2/bin/python3.12 -u -c '
  exec(compile('"'"''"'"''"'"'
  # This is <pip-setuptools-caller> -- a caller that pip uses to run setup.py
  #
  # - It imports setuptools before invoking setup.py, to enable projects that directly
  #   import from `distutils.core` to work with newer packaging standards.
  # - It provides a clear error message when setuptools is not installed.
  # - It sets `sys.argv[0]` to the underlying `setup.py`, when invoking `setup.py` so
  #   setuptools doesn'"'"'t think the script is `-c`. This avoids the following warning:
  #     manifest_maker: standard file '"'"'-c'"'"' not found".
  # - It generates a shim setup.py, for handling setup.cfg-only projects.
  import os, sys, tokenize

  try:
      import setuptools
  except ImportError as error:
      print(
          "ERROR: Can not execute `setup.py` since setuptools is not available in "
          "the build environment.",
          file=sys.stderr,
      )
      sys.exit(1)

  __file__ = %r
  sys.argv[0] = __file__

  if os.path.exists(__file__):
      filename = __file__
      with tokenize.open(__file__) as f:
          setup_py_code = f.read()
  else:
      filename = "<auto-generated setuptools caller>"
      setup_py_code = "from setuptools import setup; setup()"

  exec(compile(setup_py_code, filename, "exec"))
  '"'"''"'"''"'"' % ('"'"'/build/source/setup.py'"'"',), "<pip-setuptools-caller>", "exec"))' bdist_wheel -d /build/pip-wheel-7y0zdzc2
  cwd: /build/source/
  Building wheel for flashlight-text (setup.py) ... error
  ERROR: Failed building wheel for flashlight-text
  Running setup.py clean for flashlight-text
  Running command python setup.py clean
  running clean
  removing 'build/temp.linux-x86_64-cpython-312' (and everything under it)
  removing 'build/lib.linux-x86_64-cpython-312' (and everything under it)
  'build/bdist.linux-x86_64' does not exist -- can't clean it
  'build/scripts-3.12' does not exist -- can't clean it
  removing 'build'
Failed to build flashlight-text
ERROR: Failed to build one or more wheels

I don't understand why setuptools is not found, as it is installed in the build sandbox, for sure.

I tried to use cmake, and ninja, and even if I set FL_TEXT_BUILD_PYTHON and/or FL_TEXT_BUILD_PYTHON_PACKAGE, and I get no python files installed in site-packages/. I suspect this due to something missing in bindings/python/CMakeLists.txt.

Solving the error above would be harder I think, and we'd also prefer using cmake if possible, and not run cmake from within setup.py.

Reproduction Steps

I wouldn't feel comfortable to ask you to install Nix, clone Nixpkgs, learn Nix, and try to package your project, but you can do that :). There are many variants of the Nix expression that produce the experiences above, I can share details of them if you'd wish.

Platform and Hardware

NixOS, Linux, x86_64.

Additional Context

Maybe irrelevant, but I'm trying to package PyLaia eventually.