简体   繁体   中英

Configuring Visual Studio Code for remote Python interpreter via SSH

I have a Vagrant box with ArchLinux and Python which uses a virtual environment per project (by using a certain Python version). I wish to configure VSC for running/debugging these Python projects. I've mounted the directory containing my projects (with sshfs ) so I don't have to worry about sync.

With PyCharm the configuration is only in its IDE. How can I configure it for VSC by using SSH? What are other plugins necessary to work with Python?

Thanks in advance.

PS1: PyCharm is a great tool but it takes much resources, near 1GB in RAM.

PS2: I've read this article but it is not clear for me, one example is more useful.

EDIT: I wrote a new and improved answer to this question here: vscode python remote interpreter

Using the VScode terminal you can run the Python code on a remote machine over SSH with:

cat hello_world.py | ssh user@hostname python - 

You can add this as as your VSCode build task with ${file} pointing to the current file. If you need remote debugging in VScode you can read the following steps: code.visualstudio.com/docs/python/debugging#_remote-debugging

Additionally, you could also create an alias or function in your .bashrc or .zshrc file that makes executing files on a remote machine, potentially in a virtualenv, more convenient. For example, my .zshrc file contains the following function to execute Python files on my workstation in a remote virtualenv:

function remote-pytorch () {
    cat $1 | ssh user@hostname 'source ~/virtualenv/pytorch/bin/activate && python -'
}

This way, I can just run the following command to execute the script remotely:

remote-pytorch train_network.py

(note: the syntax for functions is slightly different in .bashrc files)

The post Define remote interpreter on remote Linux machine using Pydev and RSE Server was really useful, it seems so obvious now. This is my workaround using my own system configuration:

Step 1 : Mount your remote home folder.

$ sshfs -o password_stdin,transform_symlinks vagrant@localhost:/home/vagrant ~/Vagrant/archi02/Remote/ -p 2222 <<< "your_vagrant_password"

Step 2 : Open your project folder with VSC.

~/Vagrant/archi02/Remote/Projects/Python_3_7_2/QuickPythonBook/

Step 3 : Configure " settings.json " (from WorkSpace Settings ) for your remote Python and linter.

{
    "python.pythonPath": "~/Vagrant/archi02/Remote/Projects/Python_3_7_2/QuickPythonBook/ve_qpb/bin/python3.7",
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "pylint"
}

Step 4 : Enjoy programming. You are welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM