Select interpreter of docker container in the vscode - python

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

Related

Use singularity container as python interpreter in Visual Studio Code

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.

Can we remotely run and debug Python code from a Docker container using Spyder

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.

Hitting local IDE breakpoints on libraries within a running docker container

I have a Dockerfile containing the lines:
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
I would like to set some breakpoints on libraries installed via requirements in my local IDE. I am wondering how to launch the docker image such that these files are accessible from my local IDE. The relevant modules are located within the image at:
/usr/local/lib/python3.7/site-packages
so, i was thinking of using the line:
docker run \
-v site_pkgs:/usr/local/lib/python3.7/site-packages
--entrypoint python3 \
app-dev
but, this seems to overwrite the containers directory rendering it unable to find the modules it expects. Any guidance in how to perform this type of debugging on a running container would be very helpful. Thank you!
a) If you just mean to get the python package module code in container, and have test application in your local IDE to call these modules. Then you do not need to run this container, just:
docker cp /usr/local/lib/python3.7/site-packages .
After that, these python module's py files will be in your local(docker host). Then you can use your local IDE to debug them, of course with your own test code.
b) If you mean to use local IDE directly debug the code in container, then VSCode IDE is your choice.
NOTE: you need to use insiders build currently, as it's a pretty new feature also I think is a great feature.
See Developing inside a Container, vscode give you ability to set IDE in your local host machine, but still can let IDE debug the code in container.

VsCode Remote Debugging, change pythonpath to point to docker container's python interpreter

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.

Using linux console in pycharm

I'm new to pycharm, virtualenv, linux and git.
I've recently begun a journey of using djangoto make webapps. Before I used sublime to make scripts however now a more complex project management system such as pycarm was needed. I actually wanted to get a linux VM and go down that road but was advised that windows python IDE such as pycharm would be suitable
I recently learnt the importance of dependencies and how to use virtualenv. However in this tutorial, under the 'How do I use my shiny new virtual environment?' it starts using commands such as:
ls env
and
which python
Neither of which my pycharm console would understand.
I could use a console emulator such cmder to use the commands but then I would remove the convience of using the IDE's integrated one.
Should I upgrade to a linux VM ? Or can I install a package that allows me to use such commands in PyCharm.
As a bonus question, what are the commands in that tutorial ? are they linux commands? when ever i see $ .... is that the linux console ?
You can accomplish this using Vagrant: https://www.vagrantup.com/
You can use Vagrant and VirtualBox to setup a Linux VM (distro of your choice) and then install all of your Python dependencies in the VM. Once you have that setup, you can tell PyCharm to use the Python interpreter in your VM by following these steps:
Open the project settings dialog box in PyCharm.
Expand Project: (your project name) on the left side.
Click on Project Interpreter.
Click on the cog icon on the upper right side of the window and select Add Remote.
Click on the Vagrant radio button.
In the Vagrant Instance Folder box, select the directory your Vagrantfile is located in.
In the Vagrant Host URL box, make sure ssh://vagrant#127.0.0.1:2222 is specified.
Click OK.
Since Vagrant is compatible with Windows this solution should work for you. I have done it successfully using macOS and it works great. Good luck!
You might find this tutorial useful: https://developer.rackspace.com/blog/a-tutorial-on-application-development-using-vagrant-with-the-pycharm-ide/
I got this to work on Windows 10 with Anaconda Prompt. This terminal which comes with Anaconda, creates a "base" environment with a linux-like virtual machine and your Windows file system (C:\\) mounted to /c, and has bash installed with common Unix commands like cd, ls, chmod, echo, cat, ... Running programs from bash with access to environment variables is much nicer than Windows Powershell etc.
Now to get your Terminal in Pycharm to use Anaconda Prompt instead of cmd.exe, I followed this answer. After installing Anaconda and/or Anaconda Prompt, right-click -> Open File Location -> right-click the shortcut -> Properties -> copy file path. Then use your file path instead.
Conda is great for package environment management. Learn more about it here. For Django + Conda specifically, read here. You can also use pip to install from Python package indexes, github repos, and requirements.txt files instead. Unless you know how Anaconda Prompt works, I don't recommend creating your own environments from scratch. What worked for me was:
(base) C:\Users\wassadamo> conda create -n mynewenvironment --copy base
...
(base) C:\Users\wassadamo> conda activate mynewenvironment
(mynewenvironment) C:\Users\wassadamo> ls
folderA folderB file.txt
Works!
Whenever I try running conda deactivate to leave the base environment, my bash commands would stop working. So clone base as above.
Another tip: if you want to run shell scripts from Terminal within PyCharm with Anaconda Prompt this way, then execute them (e.g. "run.sh") on command line with
bash run.sh
I tried putting this on the first line of my run.sh
#!/usr/bin/bash
And running it with
./run.sh
But this had the effect of running it in an external Anaconda Prompt instance (add sleep, or some user input command to force it to wait and see for yourself). Explicitly running my .sh files with bash had the desired effect of running them in the same shell as I started them in PyCharm Terminal configured with Anaconda Prompt.

Categories

Resources