启动emacs的本地安装时,如何加载服务器的特定R安装(环境模块)?

时间:2022-03-15 01:21:38

I am using a cluster with environment modules. This means that I must specifically load any R version other than the default (2.13) so to load R 3.0.1, I have to specify

我正在使用带有环境模块的集群。这意味着我必须专门加载除默认值(2.13)以外的任何R版本,以便加载R 3.0.1,我必须指定

module load R/3.0.1
R

I have added module load R/3.0.1 to .bashrc, so that if I ssh into the server and load R, it opens 3.0.1 by default. But when I open R on the server (M-x R, starting data directory: /ssh:myserver), it loads the default R installation (2.13).

我已经将模块加载R / 3.0.1添加到.bashrc,所以如果我ssh到服务器并加载R,它默认打开3.0.1。但是当我在服务器上打开R(M-x R,启动数据目录:/ ssh:myserver)时,它会加载默认的R安装(2.13)。

This question is similar to previous questions except that I am accessing R on a server using a local installation of emacs. (ESS to call different installations of R and How can I specify the R version opened by ESS session in emacs?)

此问题与之前的问题类似,只是我使用本地安装的emacs访问服务器上的R. (ESS调用R的不同安装,如何指定emacs中ESS会话打开的R版本?)

2 个解决方案

#1


4  

TL;DR

TL; DR

  1. Identify path of R module: module show R/3.0.1
  2. 识别R模块的路径:模块显示R / 3.0.1
  3. add to your .emacs: (add-to-list 'tramp-remote-path "/path/to/R-3.0.1/bin")
  4. 添加到.emacs :(添加到列表'tramp-remote-path“/ path / to / R-3.0.1 / bin”)

bash called by TRAMP via ssh does execute its initialization files but which files ultimately get executed depends on several things. You can check if your ~/.bashrc gets executed at all when you use TRAMP to connect to the server by adding something like echo "bashrc here" to the file. Then set variable

TRAMP通过ssh调用的bash会执行其初始化文件,但最终执行哪些文件取决于几个方面。当你使用TRAMP连接到服务器时,你可以检查你的〜/ .bashrc是否被执行,方法是将echo“bashrc here”添加到文件中。然后设置变量

(setq tramp-verbose 9)

and try connecting. Now see if you can spot that message in a buffer called *debug tramp/ssh...*.

并尝试连接。现在看看你是否可以在名为* debug tramp / ssh ... *的缓冲区中发现该消息。

If you don't have any additional settings, TRAMP calls remote shell as /bin/sh (http://www.gnu.org/software/tramp/#Remote-shell-setup) so bash will execute /etc/profile and ~/.profile (http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files). You can also see how the shell is called in that *debug tramp/ssh...* buffer, the relevant line will be

如果您没有任何其他设置,TRAMP将远程shell调用为/ bin / sh(http://www.gnu.org/software/tramp/#Remote-shell-setup),因此bash将执行/ etc / profile和〜/ .profile(http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files)。您还可以在* debug tramp / ssh ... *缓冲区中看到如何调用shell,相关的行将是

tramp-open-shell (5) # Opening remote shell `/bin/sh'...done

Try adding the module load R/3.0.1 to ~/.profile first. If that does not help, try forcing TRAMP to call bash by its proper name by setting

首先尝试将模块加载R / 3.0.1添加到〜/ .profile。如果这没有帮助,请尝试强制TRAMP通过设置以其正确的名称调用bash

(setq explicit-shell-file-name "bash")
(setq shell-file-name "bash")

UPDATE: If all else fails, you can just open shell M-x shell, then ssh to you server which should take care of the proper modules initialization (because you mentioned that interactive ssh connection works as expected). Once on the server, launch R and then do M-x ess-remote to make ESS aware of existing R session. This way you don't rely on TRAMP at all.

更新:如果所有其他方法都失败了,你可以打开shell M-x shell,然后ssh到你的服务器,它应该处理正确的模块初始化(因为你提到交互式ssh连接按预期工作)。一旦进入服务器,启动R然后执行M-x ess-remote以使ESS知道现有的R会话。这样你根本不依赖TRAMP。

#2


1  

When TRAMP runs a shell command on the remote host, it calls /bin/sh -c. There doesn't seem to be a way to tell sh to source any files at initialization when it is called like that. So let's instead configure TRAMP to call /bin/bash -c. Then bash will source BASH_ENV, which we can point at a custom file that configures the modules.

当TRAMP在远程主机上运行shell命令时,它会调用/ bin / sh -c。似乎没有办法告诉sh在初始化时发出任何文件来源。所以让我们改为配置TRAMP来调用/ bin / bash -c。然后bash将获取BASH_ENV,我们可以指向一个配置模块的自定义文件。

So, first, configure TRAMP to use /bin/bash. To do this, we need to modify the tramp-methods variable. It's an alist, where the keys are strings denoting the connection type. I use the "scpx" connection type, but you can change that to a whichever connection type you use.

因此,首先,配置TRAMP以使用/ bin / bash。为此,我们需要修改tramp-methods变量。它是一个alist,其中键是表示连接类型的字符串。我使用“scpx”连接类型,但您可以将其更改为您使用的任何连接类型。

(let ((scpx-method (cdr (assoc "scpx" tramp-methods))))
      (add-to-list 'scpx-method '(tramp-remote-shell "/bin/bash"))
      (add-to-list 'tramp-methods (cons "scpx" scpx-method)))

Then, we can configure tramp-remote-process-environment to point at a file that will contain our module configuration.

然后,我们可以将tramp-remote-process-environment配置为指向将包含我们的模块配置的文件。

(add-to-list 'tramp-remote-process-environment "BASH_ENV=~/.bash_env")

Then, open up the ~/.bash_env file on the remote machine. You'll need to source the files that set up your module system. We use a different module system, so I'm not entirely sure what file you'll need, but perhaps you'll find it in /etc/profile.d. Here's what my file contains:

然后,打开远程计算机上的〜/ .bash_env文件。您需要获取设置模块系统的文件。我们使用不同的模块系统,所以我不完全确定你需要什么文件,但也许你会在/etc/profile.d中找到它。这是我的文件包含的内容:

source /etc/profile.d/z00_lmod.sh
module -q restore

Again, I'm not familiar with your module system, but that second line simply loads up my default set of modules.

同样,我不熟悉您的模块系统,但第二行只是加载我的默认模块集。

Lastly, since the module system configures your PATH, we need to get TRAMP to use it. By default, TRAMP just uses the contents of tramp-remote-path. But if you add tramp-own-remote-path, it will pull in the contents of PATH.

最后,由于模块系统配置了PATH,我们需要让TRAMP使用它。默认情况下,TRAMP只使用tramp-remote-path的内容。但是如果添加tramp-own-remote-path,它将引入PATH的内容。

(add-to-list 'tramp-remote-path 'tramp-own-remote-path)

#1


4  

TL;DR

TL; DR

  1. Identify path of R module: module show R/3.0.1
  2. 识别R模块的路径:模块显示R / 3.0.1
  3. add to your .emacs: (add-to-list 'tramp-remote-path "/path/to/R-3.0.1/bin")
  4. 添加到.emacs :(添加到列表'tramp-remote-path“/ path / to / R-3.0.1 / bin”)

bash called by TRAMP via ssh does execute its initialization files but which files ultimately get executed depends on several things. You can check if your ~/.bashrc gets executed at all when you use TRAMP to connect to the server by adding something like echo "bashrc here" to the file. Then set variable

TRAMP通过ssh调用的bash会执行其初始化文件,但最终执行哪些文件取决于几个方面。当你使用TRAMP连接到服务器时,你可以检查你的〜/ .bashrc是否被执行,方法是将echo“bashrc here”添加到文件中。然后设置变量

(setq tramp-verbose 9)

and try connecting. Now see if you can spot that message in a buffer called *debug tramp/ssh...*.

并尝试连接。现在看看你是否可以在名为* debug tramp / ssh ... *的缓冲区中发现该消息。

If you don't have any additional settings, TRAMP calls remote shell as /bin/sh (http://www.gnu.org/software/tramp/#Remote-shell-setup) so bash will execute /etc/profile and ~/.profile (http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files). You can also see how the shell is called in that *debug tramp/ssh...* buffer, the relevant line will be

如果您没有任何其他设置,TRAMP将远程shell调用为/ bin / sh(http://www.gnu.org/software/tramp/#Remote-shell-setup),因此bash将执行/ etc / profile和〜/ .profile(http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files)。您还可以在* debug tramp / ssh ... *缓冲区中看到如何调用shell,相关的行将是

tramp-open-shell (5) # Opening remote shell `/bin/sh'...done

Try adding the module load R/3.0.1 to ~/.profile first. If that does not help, try forcing TRAMP to call bash by its proper name by setting

首先尝试将模块加载R / 3.0.1添加到〜/ .profile。如果这没有帮助,请尝试强制TRAMP通过设置以其正确的名称调用bash

(setq explicit-shell-file-name "bash")
(setq shell-file-name "bash")

UPDATE: If all else fails, you can just open shell M-x shell, then ssh to you server which should take care of the proper modules initialization (because you mentioned that interactive ssh connection works as expected). Once on the server, launch R and then do M-x ess-remote to make ESS aware of existing R session. This way you don't rely on TRAMP at all.

更新:如果所有其他方法都失败了,你可以打开shell M-x shell,然后ssh到你的服务器,它应该处理正确的模块初始化(因为你提到交互式ssh连接按预期工作)。一旦进入服务器,启动R然后执行M-x ess-remote以使ESS知道现有的R会话。这样你根本不依赖TRAMP。

#2


1  

When TRAMP runs a shell command on the remote host, it calls /bin/sh -c. There doesn't seem to be a way to tell sh to source any files at initialization when it is called like that. So let's instead configure TRAMP to call /bin/bash -c. Then bash will source BASH_ENV, which we can point at a custom file that configures the modules.

当TRAMP在远程主机上运行shell命令时,它会调用/ bin / sh -c。似乎没有办法告诉sh在初始化时发出任何文件来源。所以让我们改为配置TRAMP来调用/ bin / bash -c。然后bash将获取BASH_ENV,我们可以指向一个配置模块的自定义文件。

So, first, configure TRAMP to use /bin/bash. To do this, we need to modify the tramp-methods variable. It's an alist, where the keys are strings denoting the connection type. I use the "scpx" connection type, but you can change that to a whichever connection type you use.

因此,首先,配置TRAMP以使用/ bin / bash。为此,我们需要修改tramp-methods变量。它是一个alist,其中键是表示连接类型的字符串。我使用“scpx”连接类型,但您可以将其更改为您使用的任何连接类型。

(let ((scpx-method (cdr (assoc "scpx" tramp-methods))))
      (add-to-list 'scpx-method '(tramp-remote-shell "/bin/bash"))
      (add-to-list 'tramp-methods (cons "scpx" scpx-method)))

Then, we can configure tramp-remote-process-environment to point at a file that will contain our module configuration.

然后,我们可以将tramp-remote-process-environment配置为指向将包含我们的模块配置的文件。

(add-to-list 'tramp-remote-process-environment "BASH_ENV=~/.bash_env")

Then, open up the ~/.bash_env file on the remote machine. You'll need to source the files that set up your module system. We use a different module system, so I'm not entirely sure what file you'll need, but perhaps you'll find it in /etc/profile.d. Here's what my file contains:

然后,打开远程计算机上的〜/ .bash_env文件。您需要获取设置模块系统的文件。我们使用不同的模块系统,所以我不完全确定你需要什么文件,但也许你会在/etc/profile.d中找到它。这是我的文件包含的内容:

source /etc/profile.d/z00_lmod.sh
module -q restore

Again, I'm not familiar with your module system, but that second line simply loads up my default set of modules.

同样,我不熟悉您的模块系统,但第二行只是加载我的默认模块集。

Lastly, since the module system configures your PATH, we need to get TRAMP to use it. By default, TRAMP just uses the contents of tramp-remote-path. But if you add tramp-own-remote-path, it will pull in the contents of PATH.

最后,由于模块系统配置了PATH,我们需要让TRAMP使用它。默认情况下,TRAMP只使用tramp-remote-path的内容。但是如果添加tramp-own-remote-path,它将引入PATH的内容。

(add-to-list 'tramp-remote-path 'tramp-own-remote-path)