简体   繁体   English

通过SSH为远程Python解释器配置Visual Studio代码

[英]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). 我有一个带有ArchLinux和Python的Vagrant盒子,它使用每个项目的虚拟环境(通过使用某个Python版本)。 I wish to configure VSC for running/debugging these Python projects. 我希望配置VSC来运行/调试这些Python项目。 I've mounted the directory containing my projects (with sshfs ) so I don't have to worry about sync. 我已经挂载了包含我的项目的目录(使用sshfs ),所以我不必担心同步。

With PyCharm the configuration is only in its IDE. 使用PyCharm,配置仅在其IDE中。 How can I configure it for VSC by using SSH? 如何使用SSH为VSC配置? What are other plugins necessary to work with Python? 使用Python需要哪些其他插件?

Thanks in advance. 提前致谢。

PS1: PyCharm is a great tool but it takes much resources, near 1GB in RAM. PS1:PyCharm是一个很棒的工具,但需要很多资源,在RAM中接近1GB。

PS2: I've read this article but it is not clear for me, one example is more useful. PS2:我读过这篇文章,但对我来说不太清楚,一个例子更有用。

EDIT: I wrote a new and improved answer to this question here: vscode python remote interpreter 编辑:我在这里写了一个新的和改进的答案: vscode python远程解释器

Using the VScode terminal you can run the Python code on a remote machine over SSH with: 使用VScode终端,您可以通过SSH在远程计算机上运行Python代码:

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. 您可以将此作为VSCode构建任务添加,其中${file}指向当前文件。 If you need remote debugging in VScode you can read the following steps: code.visualstudio.com/docs/python/debugging#_remote-debugging 如果您需要在VScode中进行远程调试,可以阅读以下步骤: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. 此外,您还可以在.bashrc.zshrc文件中创建aliasfunction ,以便在远程计算机上执行文件(可能在virtualenv中),这样更方便。 For example, my .zshrc file contains the following function to execute Python files on my workstation in a remote virtualenv: 例如,我的.zshrc文件包含以下函数,用于在远程virtualenv中在我的工作站上执行Python文件:

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) (注意: .bashrc文件中函数的语法略有不同)

The post Define remote interpreter on remote Linux machine using Pydev and RSE Server was really useful, it seems so obvious now. 使用Pydev和RSE Server在远程Linux机器上定义远程解释器的帖子非常有用,现在看起来很明显。 This is my workaround using my own system configuration: 这是我使用自己的系统配置的解决方法:

Step 1 : Mount your remote home folder. 第1步 :安装远程主文件夹。

$ 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. 第2步 :使用VSC打开项目文件夹。

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

Step 3 : Configure " settings.json " (from WorkSpace Settings ) for your remote Python and linter. 步骤3 :为远程Python和linter配置“ settings.json ”(来自WorkSpace设置 )。

{
    "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. 第4步 :享受编程。 You are welcome. 别客气。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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