Debugging in pyCharm with sudo privileges?

前端 未结 9 537
粉色の甜心
粉色の甜心 2020-12-31 04:27

I\'ve tested code that requires root access in pyCharm by running sudo pycharm.sh but this is not the way I would recommend of doing so.

I

相关标签:
9条回答
  • 2020-12-31 04:47

    Create a shell script that does "sudo python" and forwards the arguments, and configure that script as a Python interpreter in PyCharm.

    Name of this shell script should start with python (source: http://forum.jetbrains.com/message/PyCharm-424-3).

    0 讨论(0)
  • 2020-12-31 04:48

    I solved this problem by copying /usr/bin/python3 in my home, then setting cap_net_bind_service capability:

    cp /usr/bin/python3 ~/python35-setcap
    sudo setcap 'cap_net_bind_service=+ep' ~/python35-setcap
    

    And then using ~/python35-setcap as python interpreter in pycharm.

    This way, you can bind lower ports, but not any python 3 program can do it, and pycharm can still kill your script. You could also restrict execute permission to yourself if you want more security.

    0 讨论(0)
  • 2020-12-31 04:53

    I follow the instructions here and success. But there is a problem that the PYTHONPATH is not valid when you use sudo. So when you edit with

    sudo visudo -f /etc/sudoers.d/python
    

    add that:

    user host = (root) NOPASSWD:SETENV: /home/yizhao/anaconda3/bin/python
    

    also your script should be:

    #! /bin/bash
    sudo PYTHONPATH=$PYTHONPATH /home/name/anaconda3/bin/python "$@"
    
    0 讨论(0)
提交回复
热议问题