1

Is there a way in Pycharm (or elsewhere) to list all objects and variables defined at runtime of a Python program? My use case is learning a third-party Django app. I'd like to see a list of all objects instantiated, and variables defined when the server is running.

I do realize that most likely the list would be overwhelming. However, I believe it's a good way to start familiarizing oneself with a new project.

newdimension
  • 337
  • 2
  • 5
  • 15
  • On the Python prompt (don't know about PyCharm), I use [`vars()`](https://docs.python.org/3/library/functions.html#vars); or `locals()`, but that doesn't really matter in this case. –  Nov 13 '17 at 03:24
  • @Evert, to try that I ran the project in debug mode in Pycharm. Paused execution and typed `vars()` into the Python console. All I get is `{'self': , 'ready': [], 'fd_event_list': [], 'timeout': 500}` which can't be a comprehensive list. – newdimension Nov 13 '17 at 03:31
  • Sad times: I see `{'__builtins__': , '__name__': '__main__', '__doc__': None, '__package__': None}` on an empty Python 2 prompt, and `{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': , '__spec__': None, '__annotations__': {}, '__builtins__': }` for an empty Python 3 prompt. Same with a script that has (only) `print(vars())`. Heck, it even works brilliantly in a Jupyter notebook. I don't know what PyCharm is doing. –  Nov 13 '17 at 03:34
  • https://stackoverflow.com/questions/23941580/is-there-a-variable-explorer-for-pycharm ? –  Nov 13 '17 at 03:38
  • I came across that post. My takeaway from it was that if I type a script in the Python console it will keep track of the variables. I didn't find a way to run a django project using it, and I don't think it's possible. – newdimension Nov 13 '17 at 03:42
  • Did you try to print `vars()`, `locals()` and/or `globals()` in your script? –  Nov 13 '17 at 03:45
  • I wouldn't know where to add those to a Django project (my use-case) since there isn't an obvious end of the script. – newdimension Nov 13 '17 at 03:48
  • Start with printing them inside a view. But now your question has turned into "how to get information about variables in a Django project" from that posed in the title. Please make sure you ask one, clear, question. –  Nov 13 '17 at 04:03
  • https://django-debug-toolbar.readthedocs.io/en/stable/ may be useful. –  Nov 13 '17 at 04:04
  • My current use-case is a Django project, but my question is about Python in general. I'm trying to learn how to get acquainted with a large Python code base, and printing out all the defined variables seems like a natural step. I'm familiar with debug toolbar, it would not present me with non-view variables e.g. the variables defined in urls.py – newdimension Nov 13 '17 at 06:38
  • "my question is about Python in general": as stated, use `vars()` for that. –  Nov 14 '17 at 00:06

0 Answers0