Search code examples
pythondiscord.pypython-3.10github-codespaces

Beta: Github Codespace, was getting SynatxError, now resolved; however now receiving ImportError


EDIT: After following xcodz-dot's suggestion, while it was helpful and I believed it has fixed one problem, I recieved an exception in client.py. Traceback:

Traceback (most recent call last):
  File "/workspaces/[repo_name]/resource.py", line 5, in <module>
    from discord_components import DiscordComponents, ButtonStyle, Button
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/__init__.py", line 1, in <module>
    from .client import *
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/client.py", line 3, in <module>
    from discord import (
ImportError: cannot import name 'Guild' from 'discord' (/opt/python/3.10.4/lib/python3.10/site-packages/discord/__init__.py)

Is this a seperate issue or is it in direct causation of changing asyncio.async to getattr(asyncio, "async") in both compat.py and compatibility.py?

Please help me, thx :)

===============================================================

BELOW: OLD QUESTION, LIKELY RESOLVED

I am using Github Codespaces, and in the process of programming my bot, when trying to import discord_components, a SyntaxError is raised. Traceback (bash terminal, linux vm):

@[username]➜ /workspaces/[repo_name] (main ✗) $ /opt/python/latest/bin/python /workspaces/SineBot/resource.py
Traceback (most recent call last):
  File "/workspaces/[repo_name]/resource.py", line 4, in <module>
    from discord_components import DiscordComponents, ButtonStyle, Button
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/__init__.py", line 1, in <module>
    from .client import *
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord_components/client.py", line 3, in <module>
    from discord import (
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/__init__.py", line 20, in <module>
    from .client import Client, AppInfo, ChannelPermissions
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/client.py", line 38, in <module>
    from .state import ConnectionState
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/state.py", line 36, in <module>
    from . import utils, compat
  File "/opt/python/3.10.4/lib/python3.10/site-packages/discord/compat.py", line 32
    create_task = asyncio.async
                          ^^^^^
SyntaxError: invalid syntax

I have tried all soloutions in create_task = asyncio.async: SyntaxError: invalid syntax, except for downgrading to Py3.5 and using rewrite.zip (because it doesn't even exist), but there are functionalities in Py3.10 that I need, and downgrading would be a nightmare, if not, impossible. Note that discord.py works on my local machine.

Here is combat.py for reference:

...
import concurrent.futures
import asyncio

try:
    create_task = asyncio.ensure_future
except AttributeError:
    create_task = asyncio.async # -> where the exception is happening


try:
    run_coroutine_threadsafe = asyncio.run_coroutine_threadsafe
except AttributeError:
...

The same exception is raised when discord.py is imported (before discord_components).

Please help :)


Solution

  • In python 3.10, async is a keyword just like any other keyword such as while, for, etc... So you cannot write it like that. As far as I can see you are trying to be backwards compatible so you can use getattr builtin function which will resolve the error

    ...
    import concurrent.futures
    import asyncio
    
    try:
        create_task = asyncio.ensure_future
    except AttributeError:
        create_task = getattr(asyncio, "async") # -> where the exception is happening
    
    
    try:
        run_coroutine_threadsafe = asyncio.run_coroutine_threadsafe
    except AttributeError:
    ...