Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
So I was learning python
#1
Used video from automate the boring stuff with python course, and copied the course program onto my own computer and try to ran it. Didn't work as the video showed.

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %message)s')

logging.debug('Start of program')

def factorial(n):
    logging.debug('Start of factorial(%s)' % (n))
    total = 1
    for i in range (n+1):
        total *= i
        logging.debug('i is %s, total is %s' % (i, total)

    logging.debug('Return value is %s' % (total))
    return total

print (factorial(5))

logging.debug('End of program')
Its supposed to give out some debug info, but instead it gives invalid syntax on:
logging.debug('Return value is %s' % (total))
right on the word logging

I have absolutely no idea about logging, I just want to know what's happening and if the video is wrong or I did something incorrect or just anything that would let me know how to use logging.
Reply
#2
Please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post

Also, get use more descriptive thread titles

EDIT: You have missing closing parenthesis on line#11
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-25-2018, 03:58 PM)buran Wrote: Please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post

Also, get use more descriptive thread titles

EDIT: You have missing closing parenthesis on line#11

i just got a box popped up telling me bout syntax error, wasnt like a big error message. after putting the closing parenthesis i got the following error:
Error:
Traceback (most recent call last): File "C:/Users/MSI/AppData/Local/Programs/Python/Python37-32/Automate the Boring Stuff with Python Course/logging.py", line 1, in <module> import logging File "C:/Users/MSI/AppData/Local/Programs/Python/Python37-32/Automate the Boring Stuff with Python Course\logging.py", line 2, in <module> logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %message)s') AttributeError: module 'logging' has no attribute 'basicConfig'
Reply
#4
rename your file to something different. At the moment it's name is logging.py and when you import logging it in fact import itself (i.e. you overshadow the built-in module with the same name)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Aug-25-2018, 04:15 PM)buran Wrote: rename your file to something different. At the moment it's name is logging.py and when you import logging it in fact import itself (i.e. you overshadow the built-in module with the same name)
so i did that, and now it starts giving me even crazier messages
Error:
--- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 4, in <module> logging.debug('Start of program') Message: 'Start of program' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 7, in factorial logging.debug('Start of factorial(%s)' % (n)) Message: 'Start of factorial(5)' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial logging.debug('i is %s, total is %s' % (i, total)) Message: 'i is 0, total is 0' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial logging.debug('i is %s, total is %s' % (i, total)) Message: 'i is 1, total is 0' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial logging.debug('i is %s, total is %s' % (i, total)) Message: 'i is 2, total is 0' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial logging.debug('i is %s, total is %s' % (i, total)) Message: 'i is 3, total is 0' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial logging.debug('i is %s, total is %s' % (i, total)) Message: 'i is 4, total is 0' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial logging.debug('i is %s, total is %s' % (i, total)) Message: 'i is 5, total is 0' Arguments: () --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module> print (factorial(5)) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 13, in factorial logging.debug('Return value is %s' % (total)) Message: 'Return value is 0' Arguments: () 0 --- Logging error --- Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit msg = self.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format return fmt.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format s = self.formatMessage(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage return self._style.format(record) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format return self._fmt % record.__dict__ TypeError: not enough arguments for format string Call stack: File "<string>", line 1, in <module> File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main ret = method(*args, **kwargs) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode exec(code, self.locals) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 18, in <module> logging.debug('End of program') Message: 'End of program' Arguments: ()
idk if this is a successful run of logging or not. doesnt look the same as video does.
Reply
#6
another missing parenthesis, this time on line#2
it should be
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Aug-25-2018, 04:45 PM)buran Wrote: another missing parenthesis, this time on line#2 it should be
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
thx! it finally worked
Reply
#8
First of all you don't need % sign for formatting, logging allows you to do so. You simply write:

logging.debug('Return value is %s', (total))
or, as another option you could write comma so it is tuple.

logging.debug('Return value is %s' % (total,))
Reply
#9
(Aug-26-2018, 02:08 AM)leotrubach Wrote: First of all you don't need % sign for formatting, logging allows you to do so. You simply write:
There is no need to use the very old string formatting %s.
As he use 3.7 then of course use f-string.
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program')

def factorial(n):
    logging.debug(f'Start of factorial(n)')
    total = 1
    for i in range (n+1):
        total *= i
        logging.debug(f'i is {i}, total is {total}')
    logging.debug('Return value is {total}')
    return total

print (factorial(5))
logging.debug('End of program')
Reply


Forum Jump:

User Panel Messages

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