Python debugging

Show list of all available attributes and methods for the object we pass in:

from datetime import datetime

print(dir(datetime))

# list of all available attributes and methods for the object we pass in
'''
['__add__', '__class__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__',
'__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__',
'__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime',
'date', 'day', 'dst', 'fold', 'fromisocalendar', 'fromisoformat',
'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat',
'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now',
'replace', 'resolution', 'second', 'strftime', 'strptime', 'time',
'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo',
'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple',
'weekday', 'year']
'''

print(type(Person())) # <class '__main__.Person'>
print(Person) # <class '__main__.Person'>

Helpful debug info:

import sys, os

print(sys.path) # where Python is looking to import modules from
# ['D:\\python\\dev',
# 'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\\idlelib', 
# 'C:\\Program Files (x86)\\Google\\google_appengine', 
# 'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip', 
# 'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs', 
# 'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python38-32\\lib', 
# 'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python38-32', 
# 'C:\\Users\\username\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']

print(os.__file__) # prints path to the file
# C:\Users\username\AppData\Local\Programs\Python\Python38-32\lib\os.py

Show help info:

from datetime import datetime

help(datetime)

Leave a Comment