.Rmd file with Python chunks runs fine in console, but when Knitting: Error in initialize_python

rmarkdown
python

#1

Dear Rstudio Community,

I am an R user learning Python. I have RStudio on a Windows 10 64 bit laptop.

I wrote a simple .Rmd file with R and Python chunks in it ( see below)

When I “run” it by using Run -> Run All in Rstudio interface, it runs fine ad produces the expected output in console.

When I try to “Knit” it (also from Rstudio interface), I get this error:

Quitting from lines 23-29 (try_python_in_Rstudio.Rmd)

Error in initialize_python(required_module, use_environment) :

Your current architecture is 64bit however this version of Python is compiled for 32bit.

Calls: <Anonymous> ... eng_python_initialize -> ensure_python_initialized -> initialize_python

Execution halted

I am not sure if this is an Rstudio or Knit (or Python) issue. If it is not an issue for this community, I humbly apologize and would appreciate a pointer to where it is best to ask this question.

---

title: "try_python_in_Rstudio"

output: html_document

editor_options:

chunk_output_type: console

---

## R chunk

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)

cat("hello world from an R chunk")

x_in_R = 10

```

## Python chunk

```{python}

x_in_python = 15

print("hello world from a python chunk" , x_in_python)

print(x_in_python)

```

## Can I pass a value from R to python?

```{python python_chunk_2}

print("hello world from a python chunk named python_chunk_2")

print(x_in_R)

x_in_R_and_x_in_python = x_in_R + x_in_python

print(x_in_R_and_x_in_python)

```

## Can I pass a value from python to R?

```{r r_chunk_2}

twice_x_in_R_and_x_in_python = 2 * ( x_in_R + x_in_python)

cat(paste("hello world from r_chunk_2. twice_x_in_R_and_x_in_python =", twice_x_in_R_and_x_in_python))

```

#2

You can try explicitly defining your 64bit python location just after loading reticulate library

use_python("your/path/to/python")

#3

Hi andresrcs.
I tried to follow your instructions. I am not sure I interpreted them correctly. In particular, I do not know where reticulate package puts its version of python. I assumed that it is in the folder that reticulate gets unpacked into (C:/Users/ykanee200/Documents/R/win-library/3.5) But I still got the same error

  |......                                                           |   9%
  ordinary text without R code

  |............                                                     |  18%
label: setup (with options) 
List of 1
 $ include: logi FALSE



processing file: try_python_in_Rstudio.Rmd
  |..................                                               |  27%
  ordinary text without R code

  |........................                                         |  36%
label: install_reticulate
  |..............................                                   |  45%
  ordinary text without R code

  |...................................                              |  55%
label: unnamed-chunk-1 (with options) 
List of 1
 $ engine: chr "python"


Quitting from lines 46-53 (try_python_in_Rstudio.Rmd) 
**Error in initialize_python(required_module, use_environment) : **
**  Your current architecture is 64bit however this version of Python is compiled for 32bit.**
**Calls: <Anonymous> ... eng_python_initialize -> ensure_python_initialized -> initialize_python**
**Execution halted**

Here is what I tried: (this is the content of the .Rmd file)

---
title: "try_python_in_Rstudio"
output: html_document
editor_options: 
  chunk_output_type: console
---


## R chunk

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

cat("hello world from an R chunk")

x_in_R = 10


```


## Install reticulate package

```{r install_reticulate}
# > install.packages("reticulate")
# Installing package into ‘C:/Users/ykanee200/Documents/R/win-library/3.5’
# (as ‘lib’ is unspecified)
# trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/reticulate_1.10.zip'
# Content type 'application/zip' length 1598097 bytes (1.5 MB)
# downloaded 1.5 MB
# 
# package ‘reticulate’ successfully unpacked and MD5 sums checked
# 
# The downloaded binary packages are in
# 	C:\Users\ykanee200\AppData\Local\Temp\RtmpyMZEFg\downloaded_packages

library(reticulate)
# Warning message:
# package ‘reticulate’ was built under R version 3.5.2 
use_python("C:/Users/ykanee200/Documents/R/win-library/3.5")

```

## Python chunk 
```{python}


x_in_python = 15
print("hello world from a python chunk" , x_in_python)

print(x_in_python)

```

#4

As far as I know reticulate doesn't install any python version but uses the version of Python found on your PATH (i.e. Sys.which("python")), unless you override this by specifying an alternate version with use_python("your/path/to/python").

Since you are on 64bit windows I assume you have installed the 64bit Anaconda distribution in order to have python installed in your system (the easiest way), so, the path to your python instalation should be similar to this C:\\Users\\your_user\\ANACON~3\\python.exe


#5

Hi andresrcs,
Yes, this works! I really appreciate your help.

To anyone who might be reading this. To find where python is installed on my laptop, I went tothe anaconda prompt and used this command:
where python


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.