Search code examples
python-3.xhaskellcabal

Can I run an Executable Haskell file from Python?


I have a Haskell Executable (by using the build command from cabal) which I call through a command in a terminal, the command being

cabal run CSVTest

The command needs to be run in the base directory of the Tool (in this case I run the terminal from /home/dayiz/ConfigV where a ConfigV.Cabal file is located where I give information on the executable files) This gives an output in the console which I want to evaluate in Python. (If you are curious where this comes from, I'm trying to use the output from ConfigV https://github.com/ConfigV/ConfigV to correct configuration files)

Is it possible to give the location of the file and run it inside a Python? The general idea is to run my python tool, decide with which tool you want to check your file (in this case we use ConfigV to verify the file. The user would have to give the path to his Haskell executable which he needs to prepare before), run the tool and get the the output, then act on it in python again.

Most solutions i found online don't seem to work in my case, since im not just executing a haskell skript. The file needs to be build beforehand with cabal and I couldn't find anything for my specific case. ChatGPT gave me some lines of code

    import subprocess
    haskell_executable = '/home/dayiz/ConfigV/Executables/CSVTest.hs'

    try:
        subprocess.run([haskell_executable], check=True)
    except subprocess.CalledProcessError as e:
        print(f"Error running Haskell executable: {e}")

but when I try to use it I get OSError: [Errno 8] Exec format error: /home/dayiz/ConfigV/Executables/CSVTest.hs and I'm just lost at this point.

Thanks for any help


Solution

  • Don't you need to actually call cabal here with the filepath as the argument? I think subprocess is trying to basically call a file path.

    https://docs.python.org/3/library/subprocess.html

    Looking at the docs I think you want something like this? (the capture_output flag is if you want to do something with the output of the script).

    subprocess.run(["cabal", "run", "/home/dayiz/ConfigV/Executables/CSVTest.hs"], capture_output=True)