在 R 中使用 python 虚拟环境 [英] using python virtual env in R

查看:46
本文介绍了在 R 中使用 python 虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rPython"包在 R 中调用 python,但我无法让 R 引用我的 python 虚拟环境.

I am using 'rPython' package for calling python within R but I am unable to make R refer to my python's virtual environment.

在 R 中,我尝试使用

In R, I have tried using

system('. /home/username/Documents/myenv/env/bin/activate')

但在运行上面我的python库路径后没有改变(我通过python.exec(print sys.path)检查).当我跑

but after running the above my python library path does not change (which I check via python.exec(print sys.path)). When I run

python.exec('import nltk')

我被抛出错误:

python.exec("import nltk") 中的错误:没有名为 nltk 的模块

Error in python.exec("import nltk") : No module named nltk

虽然它在我的虚拟环境中.

although it is there in my virtual env.

我在 Ubuntu 13.04 上使用 R 3.0.2、Python 2.7.4.

I am using R 3.0.2, Python 2.7.4 on Ubuntu 13.04.

另外,我知道我可以使用

Also, I know I can change the python library path from within R by using

python.exec("sys.path='\your\path'")

但我不希望每次安装新的 python 包时都一遍又一遍地手动输入.

but I don't want this to be entered manually over and over again whenever a new python package is installed.

提前致谢!

推荐答案

扩展@PaulHarrison 的回答,您可以模拟 .../activate 在环境中直接执行的操作(在从R).

Expanding on @PaulHarrison's answer, you can mimic what .../activate is doing directly in the environment (before starting python from R).

这是一种确定哪些变量被修改的方法:

Here's one method for determining what vars are modified:

$ set > pyenv-pre
$ . /path/to/venv/activate
(venvname) $ set > pyenv-post
(venvname) $ diff -uw pyenv-pre pyenv-post

这给了我类似的东西:

--- pyenv-pre 2018-12-02 15:16:43.093203865 -0800
+++ pyenv-post 2018-12-02 15:17:34.084999718 -0800
@@ -33,10 +33,10 @@
 OPTERR=1
 OPTIND=1
 OSTYPE=linux-gnu
-PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
+PATH=/path/to/venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
 PIPESTATUS=([0]="0")
 PPID=325990
-PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
+PS1='(venvname) \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 PS2='> '
 PS4='+ '
 PWD=/
@@ -50,10 +50,13 @@
 TERM=xterm
 UID=3000019
 USER='helloworld'
+VIRTUAL_ENV=/path/to/venv
 XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
 XDG_RUNTIME_DIR=/run/user/3000019
 XDG_SESSION_ID=27577
-_=set
+_=/path/to/venv/bin/activate
+_OLD_VIRTUAL_PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
+_OLD_VIRTUAL_PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
 __git_printf_supports_v=yes
 __grub_script_check_program=grub-script-check
 _backup_glob='@(#*#|*@(~|.@(bak|orig|rej|swp|dpkg*|rpm@(orig|new|save))))'
@@ -2390,6 +2393,31 @@
         fi;
     fi
 }
+deactivate () ... rest of this function snipped for brevity

所以看来要更新的重要环境变量是:

So it appears that the important envvars to update are:

  • PATH:将 venv bin 目录添加到现有路径
  • VIRTUAL_ENV:设置为/path/to/venv
  • PATH: prepend the venv bin directory to the existing paths
  • VIRTUAL_ENV: set to /path/to/venv

我相信其他更改(OLD_VIRTUAL_*deactivate () ...)是可选的,实际上仅用于取消 venv 激活.

I believe the other changes (OLD_VIRTUAL_* and deactivate () ...) are optional and really only used to back-out the venv activation.

查看 .../activate 脚本可以验证这些是所采取的大部分步骤.另一个步骤是取消设置 PYTHONHOME 如果设置,如果您之前没有设置它可能不会显示在上面的差异中.

Looking at the .../activate script verifies these are most of the steps taken. Another step is unset PYTHONHOME if set, which may not be shown in the diff above if you didn't have it set previously.

要对此进行 R 化:

Sys.setenv(
  PATH = paste("/path/to/venv/bin", Sys.getenv("PATH"), sep = .Platform$path.sep),
  VIRTUAL_ENV = "/path/to/venv"
)
Sys.unsetenv("PYTHONHOME") # works whether previously set or not

这篇关于在 R 中使用 python 虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆