I am trying to perform basic Cron implementation using Python. For which, I created Cron class and tried calling the my_job() function. But, I am receiving syntax error. How can I correct the syntax error?
Python Code:
import schedule
import time
class Cron:
def my_job(self):
print('Foo')
def Start(self):
schedule.every(5).to(10).seconds.do(my_job)
while 1:
schedule.run_pending()
time.sleep(1)
A = Cron()
A.Start()
Following is exception received:
Traceback (most recent call last):
File "cron.py", line 17, in <module>
A.Start()
File "cron.py", line 11, in Start
schedule.every(5).to(10).seconds.do(my_job)
NameError: name 'my_job' is not defined
Related
I have been simplifying this code and I am without words. Simply put, the file will not run to call the classes present, and by extension the functions within. The error is something I have never come across and would like some clarity on, if someone could provide it, please.
class Server_Design:
def __init__(self):
self.intro_input()
def intro_input(self):
self.host = input('Host: ')
self.port = input('Port: ')
print("y")
if __name__ == "__main__":
Server_Design()
COMMAND LINE OUTPUT:
[SpyderKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
File "C:\Users\ ----\anaconda3\lib\site-packages\spyder_kernels\comms\frontendcomm.py", line 164, in poll_one
asyncio.run(handler(out_stream, ident, msg))
File "C:\Users\ ----\anaconda3\lib\site-packages\nest_asyncio.py", line 33, in run
task = asyncio.ensure_future(main)
File "C:\Users\ ----\anaconda3\lib\asyncio\tasks.py", line 677, in ensure_future
raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
TypeError: An asyncio.Future, a coroutine or an awaitable is required
[SpyderKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
repeating indefinitely
This error was fixed in Spyder 5.2.0 (released on November 2021). Please update to a more recent version by following the instructions posted here.
The program below needs to import os to work correctly.
from threading import Thread
# import os
class Downloader(Thread):
#classmethod
def __init__(self):
super().__init__(self)
#classmethod
def run(self):
os.path.join("aas", "sadas")
def main():
Downloader().start()
if __name__ == "__main__":
main()
In python 3.8, the above program prints a generic error message for any unhandled error occurred in the thread. Same is true for python 3.9 as well.
Exception ignored in thread started by: <bound method Thread._bootstrap of <Downloader(Thread-1, started 123145545740288)>>
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 890, in _bootstrap
self._bootstrap_inner()
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 934, in _bootstrap_inner
self._invoke_excepthook(self)
TypeError: invoke_excepthook() takes 1 positional argument but 2 were given
While in python 3.6, it prints the actual error.
Exception in thread Thread-1:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "threads.py", line 10, in run
os.path.join("aas", "sadas")
NameError: name 'os' is not defined
I am having a hard time debugging my threads. The best I can do is to guess the line number and the error. The other posts with the same error deal specifically with the error causing the issue - so not useful.
Why this change in python 3.8? Is there any way to display a proper error message?
I can't get what I'm doing wrong here. I'm defining a class with the following code:
import sqlite3 as lite
import sys
import os.path
class database:
def __init__(self,dbfname):
if os.path.isfile(dbfname):
self.con = lite.connect(dbfname)
else:
self.con = self.createDBfile(dbfname)
#Other methods...
Then, when I try to make an instance of the class
base = database("mydb.db")
I get an error saying that there is no "global" variable named dbfname .
Traceback (most recent call last):
File "testdb.py", line 67, in <module>
base = database("mydb.db")
File "testdb.py", line 13, in __init__
self.con = self.createDBfile(dbfname)
File "testdb.py", line 15, in createDBfile
if os.path.isfile(dbfname):
NameError: global name 'dbfname' is not defined
What would be the correct way to use the argument variable dbfname ?
This code looks fine. The error isn't in the code you posted though; it's in testdb.py on line 15 in the createDBfile() method (not in the __init__()).
How do I know this? Well, lets carefully look at the traceback Python gives us:
Traceback (most recent call last):
File "testdb.py", line 67, in <module>
base = database("mydb.db")
File "testdb.py", line 13, in __init__
self.con = self.createDBfile(dbfname)
File "testdb.py", line 15, in createDBfile
if os.path.isfile(dbfname):
NameError: global name 'dbfname' is not defined
Like the first line says, the most recent call is last. So you read a traceback from down to up (and not from up to down).
The very last line is the actual error, but just before that is:
File "testdb.py", line 15, in createDBfile
if os.path.isfile(dbfname):
So it says that in the file testdb.py, on line 15, in the method createDBfile() an error occurred. Python also prints out the contents of this line 15.
Above that is the call to the createDBfile() method in your __init__() function, and above that the call to the __init__() function (by creating the class instance).
You didn't post the contents of this createDBfile() method, so I can't tell you where exactly the error is. I suspect you did something wrong with the function arguments (perhaps as simple as a typo?)
With
def show(a):
""" Shows a string
>>> show(a)
a
"""
print(a)
def test():
import doctest
doctest.testmod()
if __name__ == '__main__': test()
I am getting an error while trying to learn how a docstring works.
Both this method and running it from command line with
python -m doctest unittest.py
ends with errors.
Traceback (most recent call last):
File "/home/liquid/workspace/MyPythonProject/src/unittest.py", line 19, in <module>
if __name__ == '__main__': test()
File "/home/liquid/workspace/MyPythonProject/src/unittest.py", line 16, in test
import doctest
File "/usr/lib/python3.2/doctest.py", line 2105, in <module>
class DocTestCase(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'
Why?
Unfortunately you named your module the same as the one containing TestCase. Rename unittest.py to myunittest.py and see if it works.
I have a .py file that has a number of functions. Now I am debugging the codes and find that sometimes the program is stuck somewhere.
If I just leave it there and wait for a super long time, error message shows up:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in generate
File "<stdin>", line 7, in choosePath
MemoryError
I have no idea where it is stuck, as I have several while and for loops. Is there any easy way to figure out that easily? I feel really reluctant to debug one loop by another.
Hit CTRL-C and look at the traceback.
For example, the following code will hit a never-ending loop:
import time
def foo():
while True:
time.sleep(1)
def bar():
for i in range(10):
foo()
bar()
and when I interrupt it I see:
$ bin/python endless_demo.py
^CTraceback (most recent call last):
File "test.py", line 11, in <module>
bar()
File "test.py", line 9, in bar
foo()
File "test.py", line 5, in foo
time.sleep(1)
KeyboardInterrupt
The traceback ends in foo, on line 5. This is where Python was busy when I interrupted the program. The traceback also tells me that first bar() was called, which called foo(), so I can see how we got there.
Note that if you have a bare except handler this won't necessarily work; catching all exceptions with try: except: catches KeyboardInterrupt too. Always use except Exception: at a minimum to prevent catching system exceptions.