Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax error in 2.7.18
#1
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]
Reply
#2
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.
Reply
#3
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.
Reply
#4
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]
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,221 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 401 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,634 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,242 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,330 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,273 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 911 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,873 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,381 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,987 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020