简体   繁体   English

使用 Pydev 和 RSE Server 在远程 Linux 机器上定义远程解释器

[英]Define remote interpreter on remote Linux machine using Pydev and RSE Server

I have a Windows box and a Linux red hat box.我有一个 Windows 盒子和一个 Linux 红帽盒子。

Eclipse is installed on Windows, following instructions given on this eclipse page . Eclipse 安装在 Windows 上,按照此 Eclipse 页面上给出的说明进行操作。 I managed to set up a RSE server that runs on the Linux box;我设法建立了一个在 Linux 机器上运行的 RSE 服务器; I am also able to create a project on the remote machine.我还可以在远程机器上创建一个项目。

Actually I am using virtual environments on Linux and I would like to select them when developing.实际上我在 Linux 上使用虚拟环境,我想在开发时选择它们。

Is there a way to define a remote interpreter for a PyDev or Django project?有没有办法为 PyDev 或 Django 项目定义远程解释器

I once had the same problem with a remote python interpreter inside an Ubuntu virtual machine.我曾经在 Ubuntu 虚拟机中使用远程 python 解释器遇到过同样的问题。 I guess you should be able to connect through ssh in your case also.我想您也应该能够通过ssh进行连接。

Although Pycharm can have remote interpreters (even with virtual machines using Vagrant ), some people like me prefer editors like Sublime Text 3 , ie, not IDE.虽然 Pycharm 可以有远程解释器(即使是使用Vagrant 的虚拟机),但像我这样的一些人更喜欢像Sublime Text 3这样的编辑器,即,而不是 IDE。 There, you can specify a path to any interpreter within your host machine.在那里,您可以指定主机内任何解释器的路径。 I guess Pydev also allows to specify a python interpreter inside the host.我猜 Pydev 还允许在主机内指定一个 python 解释器。

The easiest way (but maybe not the nicest) I could find to use a remote interpreter, was to mount the environment folder (where the python executable and modules were) of the virtual machine in my host.我能找到的使用远程解释器的最简单方法(但可能不是最好的)是在我的主机中挂载虚拟机的环境文件夹(python 可执行文件和模块所在的位置)。 So, here's what you can do:因此,您可以执行以下操作:

  1. In the virtual machine (the guest) --> create a virtual environment in any path you want, for example, ~/myGuestEnvs/testEnv/ .在虚拟机(来宾)中 --> 在您想要的任何路径中创建一个虚拟环境,例如, ~/myGuestEnvs/testEnv/ You can do this using virtualenv , which you previously installed with pip .您可以使用之前使用pip安装的virtualenv执行此操作。

  2. In your host --> install win-sshfs and mount the correspondent folder of the virtual machine in your host like this ~/myGuestEnvs/testEnv/ --> ~/myHostMountedFolder/ .在您的主机中--> 安装win-sshfs并将虚拟机的对应文件夹挂载在您的主机中,如下所示~/myGuestEnvs/testEnv/ --> ~/myHostMountedFolder/ If I understood well, you are coding from Windows and running the code on Linux.如果我理解得很好,您是从 Windows 编码并在 Linux 上运行代码。 I must admit that it isn't the easiest to mount disks through ssh on Windows, but it still possible!我必须承认,在 Windows 上通过ssh挂载磁盘并不是最简单的,但它仍然是可能的! You can check out this SoF question for other ways.您可以通过其他方式查看此 SoF 问题

  3. Always in your host --> point your python interpreter to the mounted folder: python_interpreter --> ~/myHostMountedFolder/bin/python .始终在您的主机中--> 将您的 python 解释器指向安装的文件夹: python_interpreter --> ~/myHostMountedFolder/bin/python

Careful , if you only mount/point the bin folder of the environment, where the python executable is, you will lost all the code completion , goto definition ... usabilities of the IDE, since it won't be able to locate your imported modules.小心,如果您只挂载/指向环境的bin文件夹,python 可执行文件所在的位置,您将丢失所有代码完成转到定义... IDE 的可用性,因为它将无法找到您导入的模块。

I should add that if the virtual machine is down, then Pydev won't be able to use the python_interpreter since the mounted folder will be empty.我应该补充一点,如果虚拟机关闭,那么 Pydev 将无法使用 python_interpreter,因为挂载的文件夹将为空。 Everytime you code, you will have to start the virtual machine, if not, then it is possible that the default host python interpreter and host python packages are used.每次编写代码时,都必须启动虚拟机,如果没有,则可能使用了默认的主机python解释器和主机python包。

Pycharm IDE support running your project/program from Remote Interpreter also the support deploying to remote server(which comes as part of Pro version ). Pycharm IDE 支持从 Remote Interpreter 运行您的项目/程序,还支持部署到远程服务器(它是Pro 版的一部分)。

Pycharm also does support Git/Vagrant/GoogleApp Engine. Pycharm 也支持 Git/Vagrant/GoogleApp Engine。

我设法以这种方式工作的唯一 Python 产品(如 Eclipse 用 Ja​​va 调试远程代码)是(商业的、专有的)WingIDE。

I managed to achieve this by doing the following:我通过执行以下操作设法实现了这一目标:

1) Create a python venv 1)创建一个python venv

python3 -m venv /home/me/venv

2) Set pydev interpreter to the venv by going to Window->Preferences->PyDev_Interpreters->Python Interpreter-> Browse for python/pypy 2) 通过转到 Window->Preferences->PyDev_Interpreters->Python Interpreter->Browse for python/pypy 将 pydev 解释器设置为 venv

3) Backup the python executable if needed: 3) 如果需要,备份 python 可执行文件:

mv /home/me/venv/bin/python3 /home/me/venv/bin/python3.bkp

4) Create a new python executable with the same name: 4)创建一个新的同名python可执行文件:

nano /home/me/venv/bin/python3

5) Paste the following content: 5)粘贴以下内容:

#!/bin/bash

remote_username=me
remote_interpreter=python3
remote_hostname=10.0.0.1

file_path=(${2//$remote_hostname/ })

ssh $remote_username@$remote_hostname "$remote_interpreter $1 ${file_path[1]}"

6) Change remote_username, remote_interpreter and remote_hostname to match your configurations. 6) 更改 remote_username、remote_interpreter 和 remote_hostname 以匹配您的配置。

Enjoy !享受 !

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

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