rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.68k stars 328 forks source link

import("sys") will change Sys.which("python") #445

Open gaizoule opened 5 years ago

gaizoule commented 5 years ago

R version 3.5.2 (2018-12-20) -- "Eggshell Igloo" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-suse-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.

library(reticulate) Sys.which("python") python "/home/jinshan/anaconda3/bin/python" import("sys") Module(sys) Sys.which("python") python "/usr/bin/python"

reticulate version 1.11

when I use import_from_path will make no module error, because import_form_path will call import("sys") which will change path, making some module can not be found.

jjallaire commented 5 years ago

Yes, when reticulate binds to a version of Python it also places it on the PATH (this is necessary so that shell invocations of Python also hit the same version). If you want to always use the Anaconda version then just specify use_python("~/anaconda3/bin/python")

gaizoule commented 5 years ago

I did, after use_python("~/anaconda3/bin/python"), import_from_path still raise no module error. same scripts can run without error in early reticulate version

R version 3.5.2 (2018-12-20) -- "Eggshell Igloo" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-suse-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.

library(reticulate) use_python("~/anaconda3/bin/python") import("sys") Module(sys) py_config() python: /usr/bin/python libpython: /usr/lib64/libpython2.7.so.1.0 pythonhome: /usr:/usr version: 2.7.13 (default, Jan 03 2017, 17:41:54) [GCC] numpy: /usr/lib64/python2.7/site-packages/numpy numpy_version: 1.8.0 sys: sys

python versions found: /home/jinshan/anaconda3/bin/python /usr/bin/python /usr/bin/python3 /home/jinshan/anaconda3/envs/py27/bin/python /home/jinshan/anaconda3/envs/rqdata/bin/python

anaconda python change to /usr/bin/python

gaizoule commented 5 years ago

anyway, import("sys") should not change env path, it should bind to anaconda python. It is a bug, I can not predict which python in use.

kevinushey commented 5 years ago

It's not done by import("sys"), it's done when the Python environment is initialized.

https://github.com/rstudio/reticulate/blob/09c4a022a54cc0580eb4c531734ed79f8800a299/R/package.R#L107-L109

Your example is failing to bind to your Anaconda Python for a separate (unknown) reason; it's unrelated to the PATH. Note that py_config() is using /usr/bin/python even though you're requesting Anaconda Python.

gaizoule commented 5 years ago

I suggest reticulate to add a function which can init specific python because use_python can not guarantee using python which you want to use

vicramr commented 5 years ago

When all else fails, I've had success with creating a .Rprofile file with the path to the desired python executable file. See here: https://rstudio.github.io/reticulate/articles/versions.html#providing-hints