venv when running sudo under linux
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!
请先登录再写评论。
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!
It seems like you have created a Python interpreter in PyCharm with sudo privileges but are facing issues with the package installation location and the generation of the requirements.txt file. Let's go through some steps to help you address these concerns:
Virtual Environment: It's recommended to use a virtual environment for your project to manage package dependencies and isolate them from the system-level Python installation. Create a virtual environment in PyCharm by going to "File" -> "Settings" -> "Project" -> "Python Interpreter." Click on the gear icon and select "Add..." to create a new virtual environment.
Package Installation: Once you have the virtual environment set up, make sure you have the virtual environment selected as the interpreter for your project. You can check this in the "Python Interpreter" settings mentioned above. When installing packages, PyCharm should automatically install them within the virtual environment.
Package Management: To manage packages within your virtual environment, PyCharm provides a built-in package manager. You can access it by going to "File" -> "Settings" -> "Project" -> "Python Interpreter" and clicking on the "+" button to install new packages. This ensures that the packages are installed within the virtual environment and not the system-wide Python installation.
Generating requirements.txt: To generate a requirements.txt file, PyCharm offers a built-in feature. Right-click on your project folder in the Project view, select "Generate requirements.txt," and choose the virtual environment you want to generate the file from. This will create a requirements.txt file containing all the packages installed in the selected virtual environment.
By following these steps, you should be able to create a virtual environment, install packages within it, and generate a requirements.txt file that includes all the required packages. This approach ensures proper package management and avoids conflicts with system-level Python installations.
Note: Running PyCharm or Python scripts with sudo privileges is generally not recommended unless absolutely necessary. It's good practice to separate system-level privileges from your development environment to minimize the risk of unintended consequences or security vulnerabilities.