Search code examples
pythonwindowsunity-game-engineinstallationaubio

Can't install Aubio inside embedded python to use in Unity Windows 11


I am trying to use Aubio inside Unity (I am trying to follow this tutorial) but I got an error message.

I followed this instruction and I type this command in cmd: .\Scripts\pip.exe install aubio but I got this error

Collecting aubio
  Using cached aubio-0.4.9.tar.gz (479 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [8 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "C:\Users\tonno\AppData\Local\Temp\pip-install-nae__h7q\aubio_d64e5b80e34e4946baa180420af258fd\setup.py", line 10, in <module>
          from moresetuptools import build_ext, CleanGenerated
        File "python\lib\moresetuptools.py", line 7, in <module>
          from this_version import get_aubio_version
      ModuleNotFoundError: No module named 'this_version'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

I use this Python version (3.8.0 for amd64 from python.org)


Solution

  • I have to create a virtual environment and install Aubio from there. Copy the file to the Unity project folder. Follow this tutorial to step 6. Then use this script

    using UnityEngine;
    using Python.Runtime;
    using System;
    
    public class useAubio : MonoBehaviour
    {
        public string venvPath = "Path/to/a/virtual/environment";
    
        void Start(){
            Runtime.PythonDLL = Application.dataPath + "/StreamingAssets/python-(Python's version)-embed-amd64/pythonxx.dll";
            PythonEngine.Initialize();
            
            using (Py.GIL())
            {
                //Add the virtual environment's site-packages to sys.path
                dynamic sys = Py.Import("sys");
                sys.path.append(Application.dataPath + "/StreamingAssets/python-(Python's version)-embed-amd64");
                sys.path.append(venvPath + "/Lib/site-packages");
    
                // Import the module and print its version
                dynamic myaubio = Py.Import("aubio");
    
                try
                {
                    dynamic version = myaubio.version;
                    Debug.Log(version.ToString());
                }
                catch (Exception ex)
                {
                    Debug.LogError($"Error accessing aubio version: {ex.Message}");
                }
            }
        }
        void OnApplicationQuit()
        {
            // Shutdown the Python engine on application exit
            PythonEngine.Shutdown();
        }
    }