【问题标题】:Configuring Visual Studio Code for remote Python interpreter via SSH通过 SSH 为远程 Python 解释器配置 Visual Studio Code
【发布时间】:2019-03-30 06:42:57
【问题描述】:

我有一个带有 ArchLinux 和 Python 的 Vagrant 盒子,它为每个项目使用一个虚拟环境(通过使用某个 Python 版本)。我希望配置 VSC 以运行/调试这些 Python 项目。我已经挂载了包含我的项目的目录(使用 sshfs),所以我不必担心同步问题。

使用 PyCharm,配置仅在其 IDE 中。如何使用 SSH 为 VSC 配置它?使用 Python 还需要哪些其他插件?

提前致谢。

PS1:PyCharm 是一个很棒的工具,但它占用大量资源,RAM 接近 1GB。

PS2:我读过this article,但我不清楚,举个例子更有用。

【问题讨论】:

    标签: python configuration visual-studio-code remote-debugging ssh-tunnel


    【解决方案1】:

    Define remote interpreter on remote Linux machine using Pydev and RSE Server 的帖子真的很有用,现在看起来很明显。这是我使用自己的系统配置的解决方法:

    第 1 步:挂载您的远程主文件夹。

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

    第 2 步:使用 VSC 打开您的项目文件夹。

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

    第 3 步:为您的远程 Python 和 linter 配置“settings.json”(来自WorkSpace Settings)。

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

    第 4 步:享受编程。不客气。

    【讨论】:

      【解决方案2】:

      编辑:我在这里为这个问题写了一个新的和改进的答案:vscode python remote interpreter

      使用 VScode 终端,您可以通过 SSH 在远程机器上运行 Python 代码:

      cat hello_world.py | ssh user@hostname python - 
      

      您可以将其添加为您的 VSCode 构建任务,其中 ${file} 指向当前文件。如果您需要在 VScode 中进行远程调试,可以阅读以下步骤:code.visualstudio.com/docs/python/debugging#_remote-debugging

      此外,您还可以在您的.bashrc.zshrc 文件中创建aliasfunction,这使得在远程机器上(可能在virtualenv 中)执行文件更加方便。例如,我的.zshrc 文件包含以下函数,用于在远程 virtualenv 中的工作站上执行 Python 文件:

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

      这样,我就可以远程执行以下命令:

      remote-pytorch train_network.py
      

      (注意:.bashrc 文件中函数的语法略有不同)

      【讨论】:

        猜你喜欢
        • 2016-10-16
        • 2021-07-02
        • 2020-05-05
        • 2019-02-22
        • 2021-11-14
        • 2020-01-04
        • 2014-10-20
        • 2022-01-13
        • 2017-09-04
        相关资源
        最近更新 更多