Python Forum

Full Version: Syntax error in 2.7.18
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I followed a tutorial in setting up a script to where users in a chat could choose the outcome of playing a game. Think Twitchplayspokemon. Now I followed everything and had it working on my main PC with Windows 10 and Python 3.8.5. I then tested it on my older Windows Vista PC running Python 2.7.18. Now this is where the error comes in that I get. This error appears in the "autohotkey.py". I will post the python and arrow message I receive below. Remember, everything is the same on my main PC this works on, so I'm not sure if there is a syntax difference in the 2.7.18 version of Python. The traceback is below the Python.

[python]

from collections import deque
from ahk.mouse import MouseMixin
from ahk.window import Window, WindowMixin
from ahk.script import ScriptEngine
from ahk.screen import ScreenMixin
from ahk.keyboard import KeyboardMixin
from ahk.sound import SoundMixin
from ahk.registery import RegisteryMixin


class AHK(
WindowMixin, MouseMixin, KeyboardMixin,
ScreenMixin, SoundMixin, RegisteryMixin
):
"""
Inherits its methods from the following classes:

| :py:class:ahk.window.WindowMixin
| :py:class:ahk.mouse.MouseMixin
| :py:class:ahk.keyboard.KeyboardMixin
| :py:class:ahk.screen.ScreenMixin
| :py:class:ahk.sound.SoundMixin
| :py:class:ahk.registery.RegisteryMixin
"""
pass


class ActionChain(AHK):
"""
Reusable action chain to execute various actions in order
"""
def __init__(self, *args, **kwargs):
self._actions = deque()
super().__init__(*args, **kwargs)

def run_script(self, *args, **kwargs):
"""
override run_script to defer to queue
"""
kwargs['decode'] = False #
self._actions.appendleft((args, kwargs))

def perform(self):
results = []
while self._actions:
args, kwargs = self._actions.pop()
results.append(super().run_script(*args, **kwargs))
return results

def sleep(self, n):
"""
:param n: how long (in seconds) to sleep
:return:
"""
n = n * 1000 # convert to milliseconds
script = self.render_template('base.ahk', body=f'Sleep {n}', directives={'#Persistent', })
self.run_script(script)

[python]

The traceback I get is below:

Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\ahk\autohotkey.py", line 2, in <module>
from ahk.mouse import MouseMixin
File "C:\Python27\lib\site-packages\ahk\__init__.py", line 1, in <module>
from ahk.autohotkey import AHK, ActionChain
File "C:\Python27\lib\site-packages\ahk\autohotkey.py", line 2, in <module>
from ahk.mouse import MouseMixin
File "C:\Python27\lib\site-packages\ahk\mouse.py", line 38
button = f'X{button-3}'
^
SyntaxError: invalid syntax
[Finished in 0.3s with exit code 1]
[shell_cmd: python -u "C:\Python27\Lib\site-packages\ahk\autohotkey.py"]
[dir: C:\Python27\Lib\site-packages\ahk]
[path: C:\Python27]
Python 2.* does not support f' string formatting. There are probably some other differences that would be easier to see if you wrapped your code in python tags.
Ah I messed that up. I apologize. I will fix that.

Can anyone direct me on how to edit my original post? The "Edit" button seems to have disappeared.
Last post wouldn't let me edit, so I redid. Thank you to Deanhystad for alerting me to the tags.


I followed a tutorial in setting up a script to where users in a chat could choose the outcome of playing a game. Think Twitchplayspokemon. Now I followed everything and had it working on my main PC with Windows 10 and Python 3.8.5. I then tested it on my older Windows Vista PC running Python 2.7.18. Now this is where the error comes in that I get. This error appears in the "autohotkey.py". I will post the python and arrow message I receive below. Remember, everything is the same on my main PC this works on, so I'm not sure if there is a syntax difference in the 2.7.18 version of Python. The traceback is below the Python.

from collections import deque
from ahk.mouse import MouseMixin
from ahk.window import Window, WindowMixin
from ahk.script import ScriptEngine
from ahk.screen import ScreenMixin
from ahk.keyboard import KeyboardMixin
from ahk.sound import SoundMixin
from ahk.registery import RegisteryMixin


class AHK(
WindowMixin, MouseMixin, KeyboardMixin,
ScreenMixin, SoundMixin, RegisteryMixin
):
"""
Inherits its methods from the following classes:

| :py:class:ahk.window.WindowMixin
| :py:class:ahk.mouse.MouseMixin
| :py:class:ahk.keyboard.KeyboardMixin
| :py:class:ahk.screen.ScreenMixin
| :py:class:ahk.sound.SoundMixin
| :py:class:ahk.registery.RegisteryMixin
"""
pass


class ActionChain(AHK):
"""
Reusable action chain to execute various actions in order
"""
def __init__(self, *args, **kwargs):
self._actions = deque()
super().__init__(*args, **kwargs)

def run_script(self, *args, **kwargs):
"""
override run_script to defer to queue
"""
kwargs['decode'] = False #
self._actions.appendleft((args, kwargs))

def perform(self):
results = []
while self._actions:
args, kwargs = self._actions.pop()
results.append(super().run_script(*args, **kwargs))
return results

def sleep(self, n):
"""
:param n: how long (in seconds) to sleep
:return:
"""
n = n * 1000 # convert to milliseconds
script = self.render_template('base.ahk', body=f'Sleep {n}', directives={'#Persistent', })
self.run_script(script)
The traceback I get is below:

Error:
Traceback (most recent call last): File "C:\Python27\Lib\site-packages\ahk\autohotkey.py", line 2, in <module> from ahk.mouse import MouseMixin File "C:\Python27\lib\site-packages\ahk\__init__.py", line 1, in <module> from ahk.autohotkey import AHK, ActionChain File "C:\Python27\lib\site-packages\ahk\autohotkey.py", line 2, in <module> from ahk.mouse import MouseMixin File "C:\Python27\lib\site-packages\ahk\mouse.py", line 38 button = f'X{button-3}' ^ SyntaxError: invalid syntax [Finished in 0.3s with exit code 1] [shell_cmd: python -u "C:\Python27\Lib\site-packages\ahk\autohotkey.py"] [dir: C:\Python27\Lib\site-packages\ahk] [path: C:\Python27]
The README in the repo for the library says it requires 3.6 or later: https://github.com/spyoungtech/ahk. If you can't install Python 3.x on that machine (is the version of Windows too old?), then perhaps try an older version of the library if there's one that supports 2.x. If not, I don't think there's much you can do.