For a project I'm doing, which uses scapy and therefore sockets, I need to be able to debug as root.
I have already figured out how to start the interpreter as root without the system asking for permission. I added:
user ALL=(root) NOPASSWD:/home/user/git/Project/bin/python2.7
to /etc/sudoers.tmp. The path I used leads to the python interpreter of the virtual environment which I'm using for this project. The LiClipse project is also using this path. Now I only need to make LiClipse run the interpreter as root, but I don't want to start LiClipse as root. I just want it to use this interpreter as root when debugging. How can I do that?
In this case, I suggest using remote debugging. You can then have the process running as whichever user it needs to, and the IDE can run independently as another user, or even on another server.
Assuming you're using PyDev in LiClipse, you can configure remote debugging by following the documentation for remote debugging.
The basic premise is that you add the pydev debugger library to your Python path and include the following where you need a breakpoint:
import pydevd
pydevd.settrace('localhost', port=7788, stdoutToServer=True, stderrToServer=True)
Then you configure your IDE with a pydev instance listening at that server (7788) in this case.
If you want to use breakpoints from the IDE, you can add the argument suspend=False, and the debugger will not suspend until it encounters your breakpoints.
In PyDev you can actually select a shell script which in turn executes the actual Python to do the run... then, you can configure your shell script to first do any special setup.
I.e.: your script could be a file named python_on_root.sh with contents such as:
#!/bin/bash
source setup_env.bash
sudo python "$#"
Then, in the interpreter configuration, select the python_on_root.sh to create an interpreter that'll execute as root.
Note that the same could be done for other interesting things -- such as running in a docker container or activating a conda environment first -- sky is the limit ;)
Related
I am connecting to an HPC environment through VScode remote ssh and would like to run python code directly in VScode for testing purposes. I would like to set the python interpreter to a singularity container which runs python upon execution. This was done by adding the following lines in the .def file of the container:
%runscript
exec python
Executing the container manually does start a python session as intended. However, nothing happens when setting the path of the python interpreter to the container file in VScode. It keeps asking for the path of the interpreter as if it did not receive any input. I tried to set the path both in VScode GUI and by setting the default path in the JSON settings file like so:
{
"python.defaultInterpreterPath":"~/path/to/singularity.sif"
}
Although this approach was reported successful here:
Python code completion IntelliSense using Singularity container interpreter ;
and there:
How can I use a python interpreter in a singularity/docker image in visual studio code .
I can however select interpreters that are not contained in singularity containers and it works fine. Notably, it works if I build the singularity container as a sandbox and provide a path to the python's bin in the sandbox.
Any idea what could go wrong here? I am using the latest version of VScode (v1.68.1) with the the Remote - SSH extension (v0.82.1) and Python extension (v2022.8.0) on Ubuntu 22.04; singularity images were created with (v3.5.3).
I have been trying to solve the same issue but found an alternative solution.
So instead of running vscode server on the host, you can actually run vscode server inside the singularity container on the host. The following procedure is cut from this comment by #oschulz github user.
Make sure you have vscode version >= v1.64.
Put "remote.SSH.enableRemoteCommand": true in your vscode's settings.json
In your local machine, add something like below to $HOME/.ssh/config
Host myimage1~*
RemoteCommand singularity shell /path/to/image1.sif
RequestTTY yes
Host somehost myimage1~somehost
HostName some.host.somewhere
User your_username_
Test that it's working: try ssh myimage1~somehost and something like python3 --version
Add this to your settings.json:
"remote.SSH.serverInstallPath": {
"myimage1~somehost": "~/.vscode-container/myimage1",
}
In vscode, connect to host using RemoteSSH, specify hostname as myimage1~somehost.
You might want to Kill VS Code Server On Host... if something is not working. Just type "kill" in vscode's command palete.
I just started using python and pycharm. I'm a bit confused of what is the run configuration in pycharm do and what is the different between the just run ?
A run configuration (not just in PyCharm, for example JetBrains IntelliJ also has them, in fact most IDEs have this concept) is a compilaton of settings to be used when running a program.
Let us stay with Python for the sake of simplicity. You might think that when you execute your script by typing in your command prompt...
python myscript.py
...that there are no settings or configuration involved. You are just running your script, right?
Not quite, you are in fact using what you could call an implicit run configuration, i.e. are whatever defaults and environment settings happen to take effect.
Some examples you will also find in PyCharm Python run configurations:
Script path is just the script you are calling, in the example myscript.py since we specified that on the command line.
Python interpreter is whatever Python interpreter is first in your path.
Parameters is empty in our example, as we didn't specify any on the command line.
Working directory is the current directory, where we are with our command prompt.
Enviromnent variables are those that happen to be set in our shell.
All of these and more can be defined in a run configuration (or multiple different run configurations if you need) for your project.
You can then select these conveniently from the dropdown menu, and the one currently selected will be used to execute your program when you press the green play button.
What is the difference between using a run configuration and just run in PyCharm?
If you just run your program, you are telling PyCharm that it should just use the project default configuration for the specific file type.
In other words, you are using a run configuration as well there, just the unmodified default configuration.
I want to use Pycharm as IDE to upload my python code to a remote server. On this server I have a virtual environment with virtualenv. This interpreter is specified in the project interpreter settings and when I look at the installed packages, it finds all the packages, but when I program, all the imports I use are not found and hence autocomplete does not work and my whole code is full of errors which is quite annoying. I use Pycharm on a windows computer and the server runs on Ubuntu 14.04.
As can be seen on the figure, the project interpreter uses an ssh connection to the server, and my path to the python interpreter is set to the python interpreter of the virtual environment (not the default python on that server). All the installed packages are also shown. Should I configure something else to make Pycharm find these modules (allthough I think pycharm finds them as they show up in the picture, but it does not use them for imports)?
I had a similar problem, but not with a remote interpreter.
I found that the Run Configuration was set to use a different interpreter than the Project Settings.
Settings / Preferences > Project: untitled > Project Interpreter =>
~/project/venv/bin/python
All good. But:
Run Configuration > Python interpreter => /usr/local/bin/python3.7
The top choice on the drop-down list for the Run Configuration interpreters was Project Default which set it to the same as my project setting.
Run Configuration > Python interpreter => Project Default
(~/project/venv/bin/python)
I don't know if this is the cause of your problem, but it's something to check.
As discussed in PY-29551 PyCharm does not yet support remote virtual environments with remote interpreters (only global Python environments). Apparently they will add this any moment in PY-6002.
Until then I solved this with the workaround suggested in PY-29551:
For simply deploying and running code the workaround with adding a "pseudo" python executable that just loads the venv before running python worked for me:
#!/bin/sh
. /home/me/mypy/venv/bin/activate
exec python $*
Just add this to your remote venv and select it as Python executable instead.
For remote debugging this did not work for me, so for debugging I instead went for the Remote Debug Server approach described in the offical PyCharm docs on remote debugging.
I'm currently trying to get PyCharm running with a remote interpreter via SSH.
The connection itself is working well, however one of my imported modules "ROOT" is not recognized by the editor ("No module named ROOT" at the "import ROOT" statement).
Before "ROOT" can be imported in any python session (while using the Terminal and not PyCharm), a "thisroot.sh" file with environment variables etc. has to be called. Thus, it's sourced at every ssh login with aid of the .bash_profile/.bashrc file.
Therefore, if the .sh file is not sourced before starting python in a SSH terminal, python cannot find the module.
Hence, I think the problem is that PyCharm doesn't call the .bashrc file (and thus doesn't source the "thisroot.sh" file) after establishing a connection. If I open a remote Python console directly in PyCharm, the "ROOT" module is not found as well.
For a less complicated module, one could add the environment variables by hand into the PyCharm settings. For my module however, it's not really feasible.
Is there any way such that PyCharm recognizes the module in the editor (and finds it in the remote Python console)? I've tried sourcing the "thisroot" file with the PyCharm StartUp script settings, but it didn't work till now.
I have a working Python project on my PC, which I am running from Pycharm.
It uses Pyroot (an interface to Root C++ library), whose C++ lib path I have added in Project Settings/Python Interpreter/Paths in Pycharm. It also needs to use the 2.7 Python interpreter, instead of 3., which is a default python in my terminal.
I want to run this project remotely on another desktop, so I need to be able to run it from terminal specifying the path to Root and the interpreter version.
Is there a way to easily extract from Pycharm the exact run command it is using when I'm running the code via run button?
Alternatively, if that's impossible, how should I specify the path to Root and the interpreter version when running from terminal?
I guess to best way is to create a virtualenv either in the terminal or in pycharm including the corrext python version 2.7 and install pyroot via pip into this virtualenv. Then you can simply ssh in the remote host, activate the venv and start your project from the terminal. Or you ssh into it with X-forwarding and start Pycharm itself from your client.
If you select the correct project and go to File > Settings, under the Project Settings you can see the Project Interpreter which tells you which interpreter is being used.
Hope this is what you are looking for.