venv when running sudo under linux

I followed the instructions I found at,Save%20changes%20and%20exit. to create a python interpreter which had sudo privileges. It works well in the sense that various commands requiring su privilieges work as expected.

But I noticed that every package I added (e.g., pystemd) to the interpreter ended up in /usr/local/..., not in the venv where I expected it would end up. Consequently when I generate a requirements.txt file none of the specially-added packages appear.

I'm obviously doing something wrong (no surprise, I'm new to python & pycharm). Advice and tips appreciated!

Comment actions Permalink

Did a bit more playing around...

I believe my problem relates to not having set up the interpreter correctly for the project. When I pointed the interpreter at the /usr/local/ script I'd created I neglected to set up a new virtual environment. Somehow that resulted in my existing virtual env being ignored (which means I was probably running without any virtual env and didn't realize it), so all the packages I installed went into the "global" arena at /usr/local...

So I decided to create a new venv, which I called venv-sudo, based on that script. Pycharm began configuring it correctly, but then blew up with a ModuleNotFoundError No module named setuptools.

Googling >>that<< problem turned up a bunch of similar errors...but nothing that led me to a solution. 


p.s. - I am rather underwhelmed about Pycharm so far. It doesn't seem terribly robust if it can't do something as simple as setting up a virtual environment without blowing up.

Comment actions Permalink


So I found an old forum thread that talked about setting up an interpreter with root privileges. It pointed out that the script I ought to be in my project home bin directory. No problem. It also pointed out that the /etc/sudoers.d/python override had to point at the python binary in my project home bin directory. Also no problem.

But when I try to define an existing environment based on that script within Pycharm, it blows up complaining that it can't set up a python SDK at Python 2.7 (maestro) "The SDK seems invalid"

Really? I never said to use Python 2.7. My project is Python3. What appears to be invalid is this Pycharm executable...

I'm glad I didn't pay extra for Pycharm (I got it as part of buying a global JetBrains license to support my dotnet work with Resharper). Because it hardly seems worth any money.

Comment actions Permalink

Okay, solved. Turns out Pycharm or python is very persnickety when it comes to how the /etc/sudoers.d/python file is set up. Referring to the executable as "python3" -- which is one of the executables in the venv/bin directory -- will not work. But referring to it as "python" will.

More detailed write-up at

Comment actions Permalink


Just wanted to leave a "thank you" note for troubleshooting the issue on your own. Good job.

Comment actions Permalink

I think Linux Capabilities are the best solution for this situation! Linux Capabilities has been specifically designed to address these kinds of problems. For example -


  • Go to Python folder e.g. /usr/local/bin/
  • Copy Python executable with different name e.g. sudo cp python3.8 python3.8_net_raw
  • Set capabilities that you need e.g. sudo setcap cap_net_admin,cap_net_raw=eip  python3.8_net_raw
  • Check capabilities is set - getcap python3.8_net_raw
  • Select Python executable with capabilities set as Python Interpreter in PyCharm



Please sign in to leave a comment.