7

I want to debug a Python script that takes multiple command line arguments, such as

myscript.py --input1 ./dir/file1.txt --input2 ./dir/file2.txt

in Pycharm. I am aware, that I can use the Run/Debug Configuration to add command line arguments. However, I feel that this is rather cumbersome as I have to open a menu every time I want to modify the arguments. Moreover, there is no file name completion when specifying files as arguments.

I would prefer calling myscript.py from my favorite shell, where I have file name completion etc., but still, use the PyCharm debugger on that script.

3
  • What's your idea about getting argument from your code? I mean trying to get arguments after running the script. Like raw_input. Commented Jan 8, 2016 at 13:12
  • You should learn how to debug your code using unit tests. Instead of running scripts with arguments you should implement a function do_awesome_work(input1, input2) and write test cases with appropriate input. You may then easily run the test cases under PyCharm debugger. Commented Jan 8, 2016 at 13:21
  • Is there any reason you're trying to do this? If you're trying to debug with multiple inputs, it may be worth debugging one case and unit testing others. If for some reason you really do need to debug multiple cases, you may want to just bypass the arguments for that debugging, set the values as variables, and test that command line arguments work separately. Commented Jan 8, 2016 at 13:26

1 Answer 1

7

Short answer: Tools> Attach to Process...

Long anser:

  1. Put a following line at very first of your code

    input("press Enter to continue")
    
  2. Launch your code in shell

    myscript.py --input1 ./dir/file1.txt --input2 ./dir/file2.txt
    
  3. Open Pycharm and use it to open your myscript.py. Set break points at anywhere you like to debug. Select Tools> Attach to Process... and select the process that myscript.py is running.

  4. Come back to the shell of myscript.py and hit Enter

Trouble you may get: ptrace don't have permission of doing something

Solution:

sudo su
echo 0 > /proc/sys/kernel/yama/ptrace_scope

or you can set 0 directly in

/etc/sysctl.d/10-ptrace.conf
Sign up to request clarification or add additional context in comments.

1 Comment

I believe that in the current version it is placed under Run > Attach to Local Processes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.