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.
Related
In vscode, we can install the python extension, then select the interpreter which we want, like python in the conda environment.
So we can use "shift" + "enter" key for running the code line by line in the terminal.
For managing different virtual environment, using docker container is a better way.
If I already install the docker, and pull the python image. How to select the interpreter which is created in the docker container? Not just remote to the docker container.
Tested on VSCode 1.61:
Install the Remote-Containers extension
Build/start the Docker container
Open the Command Palette and type Remote-Containers: Attach to Running Container..., then select the running Docker container
VSCode will restart and reload
On the Get Started page, click the Open... and enter the path to the Docker volume mounted to your source code. It must be set to the same path as WORKDIR in your Dockerfile-local, e.g. to /app.
Install the Python extension on the container
Open the Command Palette and type Python: Select Interpreter, then select the Docker interpreter
Open the Command Palette and type Python: Configure Tests, then select the framework you use
Source: https://dev.to/alvarocavalcanti/setting-up-a-python-remote-interpreter-using-docker-1i24
UPD. Remote development extensions seem to be one of the main focuses in VSCode development currently, e.g. the newer versions have got the Remote explorer Activity tab enabled by default, which allows much more intuitive approach to connecting to Docker containers. Check release notes here: https://github.com/microsoft/vscode-docs/tree/main/remote-release-notes
inside your devcontainer.json file that vscode created you have the image key and its value is the route to the image, if you want to use to change the python version you can do so there or using the quick instructions in vscode docs here
I think it's impossible, I am afraid you must remote to the docker container.
I really can't imagine out you taking a python interpreter in Linux to work on windows directly.
If your objective is to have vscode to work on a local project and run it with a docker-based interpreter, the solution is: mounting the local project directory to the docker container that contains the interpreter, then in vscode open the project directory (mounted) from the container.
How to mount your project directory:
docker run -v /user/localproject:/root/mountedproject
https://docs.docker.com/storage/volumes/
I have tested it. It should work.
Make sure all the extensions you want/need are installed in the container. Click Extensions on the left hand side to see which are installed locally and which are installed within the container.
Install everything within the container that you typically use locally (particularly Python and Pylance). You'll see a 'Install in Dev Container' button. Install everything you need, then restart VS Code and you should be good to go.
Example showing the install in Dev Container button
JetBrains PyCharm is able to remotely run and debug Python code from inside a Docker container. Also, it is able to do the same task using an Anaconda environment.
I would like to ask if it possible to perform such a task (using either Anaconda or Docker) with the Spyder IDE.
My objective is to run and debug Python code using a Docker container that lives remotely to a different machine. The Docker container contains the appropriate Python interpreter as well as the Python modules I need.
Please help.
According to the spyder doc it is possible for conda environment using ssh : http://docs.spyder-ide.org/ipythonconsole.html#connect-to-an-external-kernel.
For Docker container the same steps should work according to this issue.
But I have not tried this myself.
EDIT : Otherwise, if you like to edit your remote code as well as running it. You can use Visual Studio Code Insider (available here) It is a preliminary version of what should be included natively in Visual Studio Code soon.
I currently have a tensorflow-gpu docker container fully built. I have a volume from my host's workspace ($PWD) to /workspace inside the docker container.
I can successfully remote debug the python scripts inside my workspace/docker's workspace folder, but as expected, my pylint and intellisense don't work inside vscode, as the pythonPath is set to my local python interpreter, and the pylint init-hooks are using my local python environment to figure out if it should squiggly line something.
How do I change my local vscode's pythonpath to point to the python interpreter on docker rather than on the host? I want pylint to be calibrated to the docker's environment rather than the local host's environment.
I followed up on Brett's answer and you can now do the following:
Install the remote-containers extension.
Click "shift+command(ctrl)+p" and type and select "remote-containers: open folder in remote docket container"
I hope this helps. More information is available here:
https://code.visualstudio.com/blogs/2019/05/02/remote-development
https://devblogs.microsoft.com/python/remote-python-development-in-visual-studio-code/
https://youtu.be/jKB_nIoC1gQ
What you want is remote interpreter support which isn't supported yet. Please subscribe to this issue to know the status of this feature.
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.
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 ;)