Eclipse PyDev 使用远程解释器

11 投票
2 回答
7993 浏览
提问于 2025-04-17 14:52

有没有可能让Eclipse的PyDev使用远程的Python解释器?

我想这样做,因为我想连接的Linux服务器上运行着几个优化求解器(比如CPLEX、GUROBI等),我的脚本需要用到它们。

目前我是在本地使用Eclipse编写脚本,然后把所有文件复制到远程机器上,使用ssh登录后执行“python script.py”来运行脚本。相反,我希望能直接点击“运行”按钮,就能在我的Eclipse IDE里执行所有操作。

谢谢

2 个回答

6

正如Adel所说,使用远程系统浏览器或普通的运行按钮可能无法实现这个功能,但你可以自动化你现在使用的流程。我在笔记本风扇坏了的几周里不得不这样做,因为在上面进行任何复杂的计算都会导致它过热并关机,所以我只好把所有工作都放在我的工作机器上。

你可以利用外部工具的机制来运行一个简短的脚本,这个脚本会把你的代码同步到远程服务器上,运行你的脚本,然后把输出文件再同步回你的本地机器。我的脚本是这样的,存放在$HOME/bin/runremote.sh,并且是可执行的(chmod +x runremote.sh)。

fp="$1"  # Local path to the script we want to run--for now,
         # this is the only command I pass in from Eclipse, but you could add others if so inclined.
# My home directory is a little different on my local machine than on the remote,
# but otherwise things are in the same place. Adjust as needed.
fp=`python -c "print '$fp'.replace('/home/tsbertalan', '/home/oakridge/bertalan')"`

# Run the synchronization. I use Unison, but you could use something else,
# like two calls to rsync, or a series of scp commands.
reposync >/dev/null  # The redirection assumes your sync command will print errors properly on stderr.
cd='cd '`dirname $fp`

# I use a virtual environment on the remote server, since I don't have root access to install
# packages globally. But this could be any set-up command you want to run on the remote.
# A good alternative would be `source $HOME/.profile` or `~/.bashrc`.
act='source /home/oakridge/bertalan/bin/activate'
fname="`basename $fp`"
cmd="$act ; $cd ; python $fname"

# Run the command remotely. The -X forwards X11 windows, so you can see your Matplotlib plots.
# One difficulty with this method is that you might not see all your output just as it is created.
ssh bertalan@remote.server.edu -X  "$cmd"
sleep 1

# My synchronization script is bidirectional, but you could just use rsync with the arguments flipped.
reposync >/dev/null

如果你本地不使用Linux或OSX,你可能需要使用MinGW或Cygwin之类的工具来让它工作。或者,因为你似乎有一个可用的Python解释器,你可以用Python写一个等效的脚本,使其可执行(我想通过资源管理器的文件属性对话框来设置),并在顶部添加一行#!/path/to/python。我不常用Windows,所以这方面我帮不了你。

在Eclipse中使用这个功能,去菜单选择运行 > 外部工具 > 外部工具配置.... 添加一个新工具,位置是你的脚本路径,第一个参数是${resource_loc}。然后你可以通过运行 > 外部工具 > [第一个项目]来使用它,或者通过去窗口 > 偏好设置 > 快捷键,将其绑定到一个键盘快捷键(我用的是F12),搜索“运行最后启动的外部工具”。你可能需要先通过菜单来使这个成为“最后启动”的外部工具。

9

很遗憾,不能这样做。你可以通过远程系统浏览器(Remote System Explorer,简称RSE)远程连接到你的Linux服务器,但不能把它当作远程解释器来用。我自己用的是Pycharm。你可以选择免费的社区版,或者付费的专业版。专业版价格不算贵,而且对我来说用起来非常好。

撰写回答