venv when running sudo under linux Follow
I followed the instructions I found at https://www.edureka.co/community/37544/how-to-run-python-script-in-pycharm-with-sudo-privileges#targetText=Now%20go%20to%20PyCharm%2C%20go,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!
Please sign in to leave a comment.
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/python-sudo.sh 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 python-sudo.sh 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.
Thoughts?
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.
sigh...
So I found an old forum thread that talked about setting up an interpreter with root privileges. It pointed out that the python3-sudo.sh 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 python3-sudo.sh 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.
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 https://jumpforjoysoftware.com/2019/10/sudong-pycharm/.
@Mark
Just wanted to leave a "thank you" note for troubleshooting the issue on your own. Good job.
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 - https://esmithy.net/2015/05/05/rundebug-as-root-in-pycharm/
Steps:
Enjoy!