AutoHotkey.py icon indicating copy to clipboard operation
AutoHotkey.py copied to clipboard

AHKPY immediately terminates without error

Open t89 opened this issue 3 years ago • 5 comments

I use this module on three Windows machines and it worked flawlessly. It's an awesome tool, thank you for developing it.

A couple of days ago I decided to migrate one of the machines to a proper python environment. I'm usually not working on the machine and didn't spend the time to setup python properly, initially.

I uninstalled the system python, and installed python 3.10.2 using pyenv. The other machines are running the same python version and are running fine. I then installed other modules and did not experience any kind of trouble until I attempted to run 2 of my scripts via python -m ahkpy script.py:

This command returns an empty line and terminates immediately. No error, no logs. Same effect when I call python -m ahkpy -h. Any idea how I could debug the problem without cloning this repo and doing the pip install -e . shenanigans?

I did:

  • run from both elevated and non-elevated terminals
  • uninstall and check if the import is still working; it is not
  • reinstall autohotkey.py
  • make sure it was installed for the correct python version I even installed it using python -m pip install autohotkey.py
  • triple check it's running on the correct pyenv python executable
  • successfully import ahkpy from within the interpreter; no import errors
  • run two scripts that work on other machines and are located in different directories; no namespace conflicts
  • check if AutoHotkey.exe is working properly; also checked if there were python / ahk processes running in the background and terminated them all
  • reboot / restart terminal sessions
  • check system path and removed the old system python paths (even renamed them to make sure it's not importing from the old folder)
  • run a script directly using python script.py and I correctly receive the RuntimeError: AHK interop is not available. Please start your code as 'py -m ahkpy main.py'.

I am not using virtualenv.

Locations:

    c:\users\tommi\.pyenv\pyenv-win\versions\3.10.2\lib\site-packages\ahkpy\*
    c:\users\tommi\.pyenv\pyenv-win\versions\3.10.2\lib\site-packages\autohotkey.py-0.1.2.dist-info\*
    c:\users\tommi\.pyenv\pyenv-win\versions\3.10.2\scripts\ahkpy.exe

    $ pyenv which python
    C:\Users\Tommi\.pyenv\pyenv-win\versions\3.10.2\python.exe
    
    $ pyenv which pip
    C:\Users\Tommi\.pyenv\pyenv-win\versions\3.10.2\Scripts\pip.exe

Any idea what I am missing and how to debug the situation? Thank you for your time!

t89 avatar Feb 04 '22 02:02 t89

TL;DR: I did a forced reinstall of a "system python" in addition to the pyenv version and it fixed the problem for me.

I'd suggest to add logging of the result of the popen() attempt when this case happens:

How I found out about the problem:

I was too curious, cloned your repo and installed it via pip install -e ..

When I step through your program I enter via: Launcher -> main() and receive code 1 from ahk.wait(); so I edited the lines below (and including) the ahk = popen(...) statement as follows:

        ahk = subprocess.Popen(args, stdin=sys.stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        res = ahk.communicate()
        print("retcode =", ahk.returncode)
        print("res =", res)
        print("stderr =", res[1])
        for line in res[0].decode(encoding='utf-8').split('\n'):
            print(line)

Which led me to following result:

Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding\r\nPython runtime state: core initialized\r\nModuleNotFoundError: No module named 'encodings'.

t89 avatar Feb 04 '22 05:02 t89

Hello @t89! I'm glad AutoHotkey.py is useful for you! I'm also happy that you could get to the bottom of this issue, the information you've provided is really helpful. I'll see how this could be fixed. I'll reopen the issue in the meantime. Thanks!

Perlence avatar Feb 07 '22 19:02 Perlence

Any idea how I could debug the problem without cloning this repo and doing the pip install -e . shenanigans?

I've found out that you can uncover the error by redirecting stderr to a file:

python -m ahkpy script.py 2> stderr.txt

The error happens during Py_Initialize. At this stage stdout and stderr are not connected to console yet. There might be a way to do that in AHK before calling Py_Initialize, but I've yet to find one.

Perlence avatar Feb 13 '22 19:02 Perlence

Fix this in Python.ahk

Maybe the README should contain some hints about what's happening like

Python needs to be called with -m which then starts ahk process and which loads python dll which then loads your model. PYTHONPATH etc is preserved so it should be working

    ; move the EnvGet, python_full_path ... line above this
    ; Add to PythonDLL.ahk 
    ; Py_SetPath(path) {
    ;     ; void Py_Initialize()
    ;     PythonDllCall("Py_SetPath", "WStr", path, "Cdecl")
    ; }
    ; This is the fix :-) It's deprecated but the Py_Initialize_With_config
    ; seems to be much more complicated. This must be used before Py_Initialize() 
    ; Probably the later PySys_SetPath can be removed then (?) from both files.
    Py_SetPath(python_full_path)

I installed Python on Windows with conda env.

And the playground sample works :-)

But I still don't get why the "@ .. stuff is there. Seems to work without.

And the 2> file hint was very useful. Might be fine adding that to the README as well.

I also failed with the 3.9. something version mentioned from README which is said to be working.

MarcWeber avatar Jan 19 '23 18:01 MarcWeber

BTW: the PySys_SetPath (if not removed) maybe the WStr should be used as well ? Cause the wchar parameter is the same.

MarcWeber avatar Jan 19 '23 18:01 MarcWeber