AttributeError: loop attribute cannot be accessed in non-async contexts - python

When I do python main.py this error pops up -
Traceback (most recent call last):
File "C:\Users\Anju Tiwari\Desktop\DiscordDashboard-main\backend\main.py", line 15, in <module>
run()
File "C:\Users\Anju Tiwari\Desktop\DiscordDashboard-main\backend\main.py", line 10, in run
mybot.loop.create_task(app.run_task(host=os.getenv("HOST"),port=os.getenv("PORT")))
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Anju Tiwari\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\client.py", line 108, in __getattr__
raise AttributeError(msg)
AttributeError: loop attribute cannot be accessed in non-async contexts. Consider using either an asynchronous main function and passing it to asyncio.run or using asynchronous initialisation hooks such as Client.setup_hook
My main.py file -
import os
from bot import mybot
from web import app
from utils.logger import log_info
from dotenv import load_dotenv
load_dotenv(verbose=True)
def run():
log_info("Create Web server task...")
mybot.loop.create_task(app.run_task(host=os.getenv("HOST"),port=os.getenv("PORT")))
log_info("Start bot...")
mybot.run(os.getenv("BOT_TOKEN"))
log_info("Successfully started Web server and Bot!")
if __name__ == "__main__":
run()
I also downloaded dotenv properly and it's still not working someoe poeple help me.

Related

why websocket protocols doesn´t run when i try to pull it from other module?

Modulo_velas is the websocket loop module
this is the import and what i need to do:
#import
from pip import main
from kucoin_futures.client import Market
from kucoin_futures.client import Trade
from kucoin_futures import Modulo_velas as mvelas
print(mvelas.info_velas)
but this are all errors it gave me
Traceback (most recent call last):
File "c:\Users\javier\AppData\Local\Programs\Python\Python310\Lib\site-packages\kucoin_futures\MyScript2.py", line 5, in <module>
from kucoin_futures import Modulo_velas as mvelas
File "C:\Users\javier\AppData\Local\Programs\Python\Python310\lib\site-packages\kucoin_futures\Modulo_velas.py", line 71, in <module>
asyncio.run(main())
File "C:\Users\javier\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 37, in run
raise ValueError("a coroutine was expected, got {!r}".format(main))
ValueError: a coroutine was expected, got None
PS C:\Users\javier\AppData\Local\Programs\Python\Python310\Lib\site-packages\kucoin_futures>

I have a problem with os module in python

I've recently had a problem with (i think) the os module in python:
Traceback (most recent call last):
File "main.py", line 9, in <module>
api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\os.py", line 681, in __getitem__
raise KeyError(key) from None
KeyError: 'consumer_key'
my code of main.py is:
from config import getApi
import os
import sys
import time
print()
api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
my code of config.py is
import twitter
import os
def getApi(consumer_key, consumer_secret, access_token_key, access_token_secret):
return twitter.Api(consumer_key='*********',
consumer_secret='*********',
access_token_key='*********',
access_token_secret='*********')
Send tweets with the postUpdate is possible if I write the keys in the main.py but when I put the keys in the config.py, it don't works
Can anyone help me please ?
It's not an error with the os module, the keys simply aren't in the enviroment. If you're using a .env file you should use a module like dotenv to load the file.

Failed to import multiprocessing Queue object in python3.6

I was using multiprocessing library in python. I have python 3.6. Whenever i try to create the multiprocessing. Queue() object i get an error.
My code looks like:
import multiprocessing
def square(arr,q):
for i in arr:
q.put(i*i)
arr=[1,2,3,4,5,6]
q=multiprocessing.Queue()
p1=multiprocessing.Process(target=square,args=(arr,q,))
p1.start()
p1.join()
result=[]
while q.empty() is False:
result.append(q.get())
print(result)
and error is :
Traceback (most recent call last):
File "qu.py", line 9, in <module>
q=multiprocessing.Queue()
File "/usr/lib/python3.6/multiprocessing/context.py", line 101, in Queue
from .queues import Queue
File "/usr/lib/python3.6/multiprocessing/queues.py", line 20, in <module>
from queue import Empty, Full
File "/home/vivek/Desktop/code/par/queue.py", line 11, in <module>
q=Queue()
File "/usr/lib/python3.6/multiprocessing/context.py", line 101, in Queue
from .queues import Queue
ImportError: cannot import name 'Queue'
As you can see in the import chain listed in the error traceback, Python is trying to import the Queue definition from:
/home/vivek/Desktop/code/par/queue.py
This indicates you have somehow broken Python import logic as usually it prioritizes modules in /lib /usr/lib folders. This usually happens if you set your custom PYTHONPATH environment variable or if you mess with module variables such as sys.path.
Quick fix is to rename your file from queue.py to something else.

ladon throwing an AttributeError

I'm trying to get ladon working, however, I don't seem to be able to define the service properly.
Specifically, even with a minimal test case, it's throwing
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\ladon\server\wsgi_application.py", line 332, in __call__
self.import_services(self.service_list)
File "C:\Python33\lib\site-packages\ladon\server\wsgi_application.py", line 288, in import_services
__import__(service)
File "D:\Workspaces\Python\SOAPManager.py", line 20, in <module>
#ladonize(PORTABLE_STRING, PORTABLE_STRING, rtype=PORTABLE_STRING)
File "C:\Python33\lib\site-packages\ladon\ladonizer\decorator.py", line 118, in decorator
injector.__doc__ = ladon_method_info._doc
AttributeError: 'NoneType' object has no attribute '_doc'
My Run.py contains:
from ladon.server.wsgi import LadonWSGIApplication
from os.path import abspath, dirname
from wsgiref.simple_server import make_server
application = LadonWSGIApplication(
['SOAPManager'],
[dirname(abspath(__file__))],
catalog_name='API',
catalog_desc='API Description')
httpd = make_server('0.0.0.0', 8004, application)
print("Listening on port 8004...")
# Respond to requests until process is killed
httpd.serve_forever()
And the minimal test case in SOAPManager.py:
from ladon.ladonizer import ladonize
from ladon.types.ladontype import LadonType
from ladon.compat import PORTABLE_STRING
#ladonize(PORTABLE_STRING, PORTABLE_STRING, rtype=PORTABLE_STRING)
def Authenticate(Username, Password):
return "Test"
The error is being raised from within the ladonize decorator. It seems to be occuring when trying to build up the definition of the service. Specifically, in ladon the decorator calls collection.add_service_method which returns None instead of a method. I think it's failing a line-number check.
firstlineno = f.__code__.co_firstlineno
# get an ast-analyzed object of the source file
sinfo = self.source_info(src_fname)
...
for clsname,v in sinfo.items():
if firstlineno>v[0] and firstlineno<=v[1]:
For some reason that check is failing so the method defaults to returning None.

Why am I getting an import error upon importing multiprocessing?

I have a script which requires multiprocessing. What I found from this script is that there is a problem with the multiprocessing module. To test this theory, I copied and pasted
from multiprocessing import Process
def f(name):
print('hello', name)
if __name__ == '__main__':
p = Process(target=f, args=('bob',))
p.start()
p.join()
into a test script and received the following traceback
Traceback (most recent call last):
File "a.py", line 1, in <module>
from multiprocessing import Process
File "/usr/lib64/python3.3/multiprocessing/__init__.py", line 40, in <module>
from multiprocessing.util import SUBDEBUG, SUBWARNING
File "/usr/lib64/python3.3/multiprocessing/util.py", line 16, in <module>
import threading # we want threading to install it's
File "/usr/lib64/python3.3/threading.py", line 11, in <module>
from traceback import format_exc as _format_exc
File "/usr/lib64/python3.3/traceback.py", line 3, in <module>
import linecache
File "/usr/lib64/python3.3/linecache.py", line 10, in <module>
import tokenize
File "/usr/lib64/python3.3/tokenize.py", line 30, in <module>
from token import *
File "/home/lucas/Server/ClinApp/weblabs/utils/token.py", line 1, in <module>
from django.conf import settings
File "/usr/lib/python3.3/site-packages/django/conf/__init__.py", line 9, in <module>
import logging
File "/usr/lib64/python3.3/logging/__init__.py", line 195, in <module>
_lock = threading.RLock()
AttributeError: 'module' object has no attribute 'RLock'
Also, I am running fedora 18 64-bit on a quad core ivy bridge. Why am I receiving this traceback error?
Suggestion
Here is what happens when I run RLock
$ python3
>>> import threading
>>> threading.RLock()
<_thread.RLock owner=0 count=0>
>>>
File "/usr/lib64/python3.3/tokenize.py", line 30, in <module>
from token import *
File "/home/lucas/Server/ClinApp/weblabs/utils/token.py", line 1, in <module>
from django.conf import settings
Your /home/lucas/Server/ClinApp/weblabs/utils/token.py script is being imported instead of the standard python 'token.py'. Its got a bug in it or it simply shouldn.t be imported as a top level script. You probably have /home/lucas/Server/ClinApp/weblabs/utils/ in your python path in some way.
Generally, its not a good idea to name python scripts after builtin scripts.
After you rename token.py to something else (get_token.py), remember to delete token.pyc in your local working directory. Or else, you will continue to get the Traceback error message you listed above.

Categories

Resources