Run/Debug as root in PyCharm

I’m working on a Python project that needs to run as root in order to work properly. Previously I’ve just run the whole PyCharm IDE as root, but this has some down-sides, and I think I have a better approach now.

The biggest hassle with running the IDE as root is with file permissions. I tend to jump back and forth between the command line and the IDE, but if I do version control operations, or just add new files within the IDE, they naturally are owned by root, and are inaccessible from the terminal running with my normal user account. Of course, I could run the terminal as root, but at some point, it starts to sound like a bad idea.

How To

So here is a way to run code as root, without running the whole IDE that way. More detailed explanations are lower on the page.

Don’t require a password running sudo python

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

Add a line of the form:

<user> <host> = (root) NOPASSWD: <full path to python>

For example:

eric V-LU-ERSM = (root) NOPASSWD: /home/eric/Envs/my-project/bin/python

Create a sudo script

Call the script, containing (with your correct full python path):

sudo /home/eric/Envs/my-project/bin/python "$@"

Be sure to make the script executable:

chmod +x

Use the script as your Python interpreter

In PyCharm, go to Settings > Project Interpreter. Click the gear icon by the current Project Interpreter drop-down, and choose “More…”. Then click the green plus icon to add a new interpreter (Add Local). Browse to and select it, and set it as the interpreter for the project.

Now when you run or debug, the code will run as root.

More details

If you don’t use the NOPASSWD option, it could still technically work, but it would be kind of annoying as you’d have to provide your password on every run. If you run PyCharm from a terminal window, the sudo prompts appear there, not anywhere within PyCharm.

Also, when adding the shell script as an interpreter, PyCharm apparently executes it multiple times because the terminal is spammed with password prompts.


PyCharm isn’t very patient in waiting for some of those executions, so even if you enter your password, its call to figure out the Python interpreter’s version might fail, and it will say it is an “Unknown” version.

The /etc/sudoers.d/python file must use a full path to the python executable. In my case, I’m setting things up to point at a virtual environment, but you could use the system python instead.

There are couple of things to note about the script itself. First, its name must start with “python”. Second, the "$@" in the script just passes the script’s received arguments straight through to Python, so that when PyCharm starts with debugging options, those are correctly forwarded.

When adding the script as an interpreter, PyCharm doesn’t seem to want to keep it unless it is set as the interpreter for the project. All signs will indicate that you could have that as an independent interpreter, but if you close the Settings dialog and reopen it, that interpreter will be gone. I’m not sure if that is a PyCharm bug. The reason this is interesting is that you could leave your normal interpreter for the project, and pick the sudo interpreter within the Run/Debug Configuration dialog for the configuration that needs to be run as root.

As it is, you sort of have to do the opposite: The sudo python interpreter is the default, and if you have other entry points you’d like to not run as root, you can pick the “plain” interpreter for those.

Update: A down-side to this strategy is that PyCharm can’t generally stop the process — so if you click the “stop” button in the debugger, it will detach but the process continues running. The debugger prints the process id to the console when starting, though, so you can use that to know what to kill.


23 thoughts on “Run/Debug as root in PyCharm

  1. Thank you for your post.

    I have a problem, after defining the project interpreter as /usr/bin/

    I have a problem, the external libraries that normally appear when the python2.7 interpreter is defined, disappear and i dont know how to make them available with the /usr/bin/ interpreter.

    Can you help me?

    • I’ve sometimes seen PyCharm apparently “forget” about all the built-in symbols. I’m not sure what causes that, but you might try invoking the menu item File > Invalidate Caches / Restart…. That usually works for me, but I don’t think I’ve seen the problem specifically with using a script for the interpreter.

  2. Hi – great article, however, I see the following error when I try to do a local add to my project interpreter.

    “Invalid” Python SDK; Cannot setup an SDK at /usr/bin/”

    Any thoughts? Anyone?

      • Thanks for article. error because of name, update this way

        eric V-LU-ERSM = (root) NOPASSWD: /home/eric/Envs/my-project/bin/python2.7

        sudo /home/eric/Envs/my-project/bin/python2.7 “$@”

        chmod +x python

          • I don’t think you need to worry about any file extensions. If you run the “sudo visudo -f /etc/sudoers.d/python” command, it will run an editor where you can enter the suggested user and host line. Just make sure that you use the appropriate save command when exiting the editor.

  3. Hi Eric, potentially useful, but for a relatively new Linux guy like myself it seems like you are assuming I know something that I should know but don’t. Where is the sudo script supposed to go? When I run Pycharm it puts the projects in a Pycharm subfolder under my home directory. Should I put it there? I tried that, but when I attempt to execut it to test that it works I get an command error. Sorry if that seems a bit naive—all I am trying to do is get some code that accesses an ethernet interface running and can’t seem to get Pycharm to run as an administrator.

      • Hi George,

        If your needs are simple, it may be easier to just run the whole IDE as root. You can do that by opening a terminal and running, for example:

        sudo pycharm-community-5.0.4/bin/

        Doing that, anything the IDE starts for you will also run as root.

        • This command did the trick for me. Starting the IDE with sudo command took care of permissions issue in IDE while installing the packages.

  4. Thank you Eric! Your post helped a bunch toward what I was trying to do. Interestingly, I landed here not fully because of files being owned by root, but also because X-gui libraries refused to load as root let alone run pycharm. Different starting points, same issue nevertheless. Appreciate your post. Taking a leaf out from your About page, sounds like its about time to start writing things down for others and my future self. 🙂

  5. Hi guys , on fedora 23 , i get this error either debugging or running
    sudo: no tty present and no askpass program specified

    any ideas?

    • Seems like the NOPASSWD isn’t working. I’m not familiar with Fedora — maybe there is a different way to set that up.

  6. If you needed root for example in order to debug SSL networking then it is much simpler to just set a port higher than the default of 443 — root is only required for ports up to 1024 so it needs to be higher than that. Then you can debug as a normal user.

    • Actually, I was wrong. It was not the name that caused the problem, but the password. When I configure sudoers to allow the target interpreter to be reached with sudo without password then Pycharm doesn’t complain about invalid SDK.

Leave a Reply

Your email address will not be published. Required fields are marked *