python - 是否可以通过 Visual Studio Code 使用远程 ssh 解释器运行本地 python 脚本?

标签 python debugging visual-studio-code ssh remote-access

是否可以通过 Visual Studio Code 使用远程 ssh 解释器运行本地 python 脚本?
我在远程 Linux 服务器机器上有一个 python 项目,我想将代码复制到我的本地机器上,并通过远程 python 解释器对其进行调试,以及它对数据库等的所有访问和权限。

最佳答案

如果你有能力安装 rmate 在远程服务器上,那么您可以使用 Remote VSCode .
为此,扩展的文档描述,您为扩展配置 VS Code 用户设置,从命令面板使用 Remote: Start server 启动扩展的服务器。 , 使用 ssh -R 52698:localhost:52698 user@example.com 转发的远程端口打开到远程服务器的 ssh 连接,然后运行 ​​rmate -p 52698 <file>在远程服务器上。
这将导致在本地 VS Code 编辑器中打开指定的文件,并且在您保存它们后,对它所做的任何更改将很快反射(reflect)在远程服务器上的副本中。

或者,您可以使用 sshfs将远程目录挂载为本地目录。这是我现在使用本地编辑器编辑远程文件的首选方法。在使用 apt 的系统上,您可以使用 apt install sshfs 安装它.在使用 yum 的系统上,您可以使用 yum install fuse-sshfs 安装它.
安装后,创建一个本地目录,您将在其中安装远程目录。我们称之为 /mnt/remote对于这个例子。继续并使用 sshfs 挂载它:

sshfs -o allow_other,default_permissions user@example.com:<src dir> /mnt/remote
哪里<src dir>是您希望本地访问的远程目录的绝对路径。
添加 IdentityFile=/home/$USER/.ssh/id_rsa default_permissions 之后的选项如果您正在使用 key 授权,那么您应该这样做。
您可能需要调整 /mnt/remote 的权限,并使用 sudo对于上面列出的所有命令。
Many other options are detailed in the man pages .也考虑使用 large_read提高效率,以及 reconnect,ServerAliveInterval=5,ServerAliveCountMax=15以获得更高的可靠性。
曾经sshfs命令成功,你应该发现远程目录可以作为本地目录访问/mnt/remote .要在本地编辑远程的内容,只需使用 code /mnt/remote 打开 VS Code 到该目录。 ,然后编辑掉就好像它​​都是本地的一样!您的更改将在您保存后很快反射(reflect)在远程文件中。

关于python - 是否可以通过 Visual Studio Code 使用远程 ssh 解释器运行本地 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66981317/

相关文章:

python - sklearn 管道的并行化

Python argparse : Handling unknown amount of parameters/options/etc

python - 在基于 Flask 的 Python Web 应用程序中从外部更改全局变量

java - 为什么 ByteBuffers 的 hashCodes 是一样的?

visual-studio-code - "Go to file"使用 VSCode,而不是 goToSymbol

python - 在长子进程调用(python)中使用 PIPE 不起作用

debugging - ELF 文件 - 获取字符串使用位置的函数

asp.net 错误显示字节偏移量而不是行号,即使在 Debug模式下也是如此

visual-studio-code - 我可以使用 VS Code 批量编辑多个文件吗?

visual-studio-code - 从 postDebugTask 中终止另一个任务 - VS Code