Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

how to for debugging using pycharm #1312

Merged
merged 13 commits into from May 31, 2018
Merged

how to for debugging using pycharm #1312

merged 13 commits into from May 31, 2018

Conversation

joelgrus
Copy link
Contributor

No description provided.

@joelgrus joelgrus requested review from cristipp and schmmd May 31, 2018 00:42

![debug debug](debugging_images/debug_debug.png)

And then once you hit your breakpoint, you can inspect variables or step through code to your heart's content:
Copy link

@cristipp cristipp May 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works the first time. After that, we have created a non-empty serialization_dir, which prevents the debug process from starting again. The workarounds are

  1. Switch to CLI and delete serialization_dir,
  2. Edit the run configuration and set a "before launch" command.
  3. Edit the appropriate default configuration and set a "before launch" command.

None is ideal, 3 is the least annoying. However, you should document how to setup the "before launch" command correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, but I can just do that in python

@cristipp
Copy link

cristipp commented May 31, 2018

Note: Since there is quite a bit of boilerplate to setup, it would help to have inlined code in the tutorial, not just bitmaps. I can't copy/paste from bitmaps...


First, create a wrapper script that puts your command-line args into `sys.argv` and calls `main()`:

![wrapper script](debugging_images/debug_wrapper_with_rmtree.png)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BE VERY CAREFUL TO NOT DO THIS DURING ACTUAL TRAINING

Note that if you're using Beaker, this is a non-issue. Each Beaker run is isolated from the others and doesn't compete for the same output directory.

Yes, I know you're targeting an audience beyond AI2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're using beaker, you're necessarily not debugging using pycharm, and so you don't need to use this script.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just used a PyCharm-for-debug/Beaker-for-train setup last week. Happy to demonstrate in person.

@cristipp
Copy link

cristipp commented May 31, 2018

Are you guys editing comments live? Either I'm having a forgetful moment, or I added a note on how "Attach to Process" has poor ergonomics, which I can't find anymore.

@@ -0,0 +1,133 @@
# How to Debug Your AllenNLP Code

Recently several people have asked how to debug allennlp code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change this warning:

Recently some people have asked how to debug AllenNLP code using PyCharm, so we put together this "how to". The first section covers debugging in PyCharm, and the second section covers debugging in VSCode.

It turns out that `allennlp` is just (in essence) an alias for `python -m allennlp.run`, so you could equivalently do

```
python -m allennlp.run train experiments/venue_classifier.json -s /tmp/your_output_dir_here --include-package my_library -o '{"trainer": {"cuda_device": -1}}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your motivation for including this? You might consider explaining why in the proceeding text.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the motivation is that (as far as I can tell) the "attach to local process" will only find "python" processes, so if you do allennlp train you can't attach to it. I'll make that clearer


![attach to local process](debugging_images/attach_to_process_2.png)

and get results in the debugger:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a long delay before anything started appearing in the console.


![breakpoint](debugging_images/breakpoint.png)

Right-click in the editor and choose "Debug":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm setting the breakpoint outside of my startup.py script (what I named the above) then I need to go to File -> Debug... -> startup.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is unclear, I meant go back to the wrapper script, I'll fix that


![attach to local process](debugging_images/attach_to_process_3.png)

## How to Debug in PyCharm, Using "Run > Debug"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This worked great for me.


![inspect variable](debugging_images/inspect_variable.png)

# How to Debug in VSCode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never used VSCode--so I didn't test this.

@schmmd schmmd self-assigned this May 31, 2018
@cristipp
Copy link

cristipp commented Jun 1, 2018 via email

@joelgrus
Copy link
Contributor Author

joelgrus commented Jun 1, 2018

VSCode is good, it's what I use

gabrielStanovsky pushed a commit to gabrielStanovsky/allennlp that referenced this pull request Sep 7, 2018
* add debugging how to

* clean up

* add line to delete serialization directory

* include code as code not as png

* add VSCode section

* formatting

* pr feedback

* allow allennlp command
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants