Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed "tensorflow::install_tensorflow()" #78

Open
levinhein opened this issue Sep 29, 2020 · 7 comments
Open

Failed "tensorflow::install_tensorflow()" #78

levinhein opened this issue Sep 29, 2020 · 7 comments

Comments

@levinhein
Copy link

Hello, I have two lines of commands here. One is install.packages() which successfully finished. But the second line tensorflow::install_tensorflow() failed. I don't understand why. Any advice, please? Thank you!

> install.packages("tensorflow")
Installing package into ‘/home/levinbioinformatics/R/x86_64-pc-linux-gnu-library/4.0’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/tensorflow_2.2.0.tar.gz'
Content type 'application/x-gzip' length 41229 bytes (40 KB)
==================================================
downloaded 40 KB

* installing *source* package ‘tensorflow’ ...
** package ‘tensorflow’ successfully unpacked and MD5 sums checked
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (tensorflow)

The downloaded source packages are in
	‘/tmp/Rtmp0LkFny/downloaded_packages’


> tensorflow::install_tensorflow(extra_packages='tensorflow-probability', version = "2.1.0")
Error: .onLoad failed in loadNamespace() for 'tensorflow', details:
  call: py_module_import(module, convert = convert)
  error: ModuleNotFoundError: No module named 'tensorflow'
@qrzhang
Copy link

qrzhang commented Oct 9, 2020

I used to have the same error. And for me, it's fixed after I specify my Anaconda environment. You can try to use the following code.

library(reticulate)
use_python("your/environment/path/anaconda3/envs/myenv/bin/python")
use_condaenv('myenv')

install.packages("tensorflow")

tensorflow::install_tensorflow(extra_packages='tensorflow-probability', version = "2.1.0")

@levinhein
Copy link
Author

Hi @qrzhang. Thanks for your reply! Does this mean that I can't use cellassign or tensorflow, unless I create/use a conda environment?

@levinhein
Copy link
Author

I used to have the same error. And for me, it's fixed after I specify my Anaconda environment. You can try to use the following code.

library(reticulate)
use_python("your/environment/path/anaconda3/envs/myenv/bin/python")
use_condaenv('myenv')

install.packages("tensorflow")

tensorflow::install_tensorflow(extra_packages='tensorflow-probability', version = "2.1.0")

Hello @qrzhang! I followed your tips. I created a new conda environment, cellassign with python=3.7, then activated the environment, then opened Rstudio from the active environment. I think all these 5 lines of codes worked, but there's an error at the end of the 5th code. As you can see from the attached image, it says:

Error: installation of 'python=3.6' into environment '/home/levinbioinformatics/.local/share/r-miniconda/envs/r-reticulate' failed [error code 1]

What is this and what should I do?

image

@levinhein
Copy link
Author

Hello, following up on this. Do you any more suggestion? Thanks!

@slohr
Copy link

slohr commented Oct 23, 2020

We had a tough time getting this to work in our environment too and finally came up with an approach that at least works for us. Perhaps it will work for you.

Here are a few coarse details about our environment: RStudio Server Pro 1.3, R 4.0.2 and Python 3.8.

Basically it boils down to managing a python virtual environment outside of the R environment.

First get a run-of-the-mill virtual environment setup with tensorflow.

cd <place_where_environments_live>
python -mvenv renv_ml
cd renv_ml/
. bin/activate
pip install --upgrade pip
pip install tensorflow-gpu
pip install tensorflow-probability

Replace <place_where_environments_live> appropriately. For me it's /mnt/home/slohr/environments/python

Then in R do the following:

library(reticulate)
library(tensorflow)
library(BiocManager)
library(SingleCellExperiment)

Sys.setenv(RETICULATE_PYTHON="<place_where_environments_live>/renv_ml/bin/python")
#for me this was */mnt/home/slohr/environments/python/renv_ml/bin/python*

#quick check of configuration
tensorflow::tf_config()

#this should show something like this
#TensorFlow v2.3.1 (~/environments/python/renv_ml/lib/python3.8/site-packages/tensorflow)
#Python v3.8 (~/environments/python/renv_ml/bin/python)
#
#where python and the tensorflow package are coming from the virtualenv from above.
#if not then double check the paths, etc above.

#Cellassign needs to be loaded *after* the reticulate and tensorflow environments have been configured.
#installed via devtools and not Bioconductor
library(cellassign)

s <- SingleCellExperiment::sizeFactors(example_sce)

fit <- cellassign::cellassign(
  exprs_obj = example_sce[rownames(example_marker_mat),], 
  marker_gene_info = example_marker_mat, 
  s = s, 
  learning_rate = 1e-2, 
  shrinkage = TRUE,
  verbose = FALSE
)

From there, everything following the "fit" portion of the tutorial should be possible.

A couple of other notes. I'm not a huge fan of Anaconda so I didn't even try that approach. It may work with more twiddling but I don't know what those tricks would be. Also the use_virtualenv and use_python methods would not work in our environment either. Those should have functioned similar to setting the RETICULATE_PYTHON environment variable but they didn't. Perhaps someone knows the right incantation, but for now this approach at least gets to the science part.

HTH and good luck!

@Zepeng-Mu
Copy link

Zepeng-Mu commented Aug 13, 2021

I'm also having error with loading cellassign or running tf_config() is rstudio but not R console. My solution is to change the python interpreter in the rstudio preference to the one actually in the created virtualenv but not the RStudio default one.

@ghost
Copy link

ghost commented Dec 14, 2021

@slohr Thank you so much! I was going through installation hell, and this finally worked for me! For me, the use_python method wasn't working, but I got it by running that command before anything else; it seems like loading the libraries causes another python installation to load first, which messes it up. I also used the option required=TRUE, but idk how much that makes a difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants