简体   繁体   English

使用网状结构在 RStudio 中加载 Python 时遇到问题

[英]Trouble loading Python in RStudio with reticulate

I am trying to run a R Markdown document which uses both R and Python code chunks.我正在尝试运行同时使用 R 和 Python 代码块的 R Markdown 文档。 I am on macOS Catalina version 10.15.2, R version is 3.6.2 and RStudio version is 1.2.5033.我使用的是 macOS Catalina 版本 10.15.2,R 版本是 3.6.2,RStudio 版本是 1.2.5033。 I am using the reticulate package version 1.14.我正在使用reticulate包版本 1.14。

which python on the Terminal reveals /Users/dhirajkhanna/opt/anaconda3/bin/python and the same is entered in my .Renviron file as RETICULATE_PYTHON=/Users/dhirajkhanna/opt/anaconda3/bin/python .终端上的which python显示/Users/dhirajkhanna/opt/anaconda3/bin/python并且在我的.Renviron文件中输入相同的 RETICULATE_PYTHON RETICULATE_PYTHON=/Users/dhirajkhanna/opt/anaconda3/bin/python Strangely when I do Sys.which('python') in my RStudio console, it shows me a different path, viz.奇怪的是,当我在我的 RStudio 控制台中执行Sys.which('python')时,它向我显示了一条不同的路径,即。 /usr/bin/python . /usr/bin/python When I load the reticulate package, the output of py_config() is:当我加载reticulate包时, py_config()的输出是:

python:         /usr/local/bin/python3
libpython:      /usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin/libpython3.7.dylib
pythonhome:     /usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7:/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7
version:        3.7.4 (default, Jul  9 2019, 18:13:23)  [Clang 10.0.1 (clang-1001.0.46.4)]
numpy:          /usr/local/lib/python3.7/site-packages/numpy
numpy_version:  1.16.4

python versions found: 
 /usr/bin/python3
 /usr/local/bin/python3
 /usr/bin/python
 /usr/local/bin/python
 /Users/dhirajkhanna/anaconda3/bin/python
 /Users/dhirajkhanna/.virtualenvs/object_recognition_detection/bin/python
 /Users/dhirajkhanna/.virtualenvs/object_recognition_detection1/bin/python
 /Users/dhirajkhanna/.virtualenvs/r-tensorflow/bin/python
 /Users/dhirajkhanna/venv/bin/python

If I specify, use_python('/Users/dhirajkhanna/opt/anaconda3/bin/python') , the output of py_config() is:如果我指定use_python('/Users/dhirajkhanna/opt/anaconda3/bin/python')py_config()的输出是:

python:         /Users/dhirajkhanna/opt/anaconda3/bin/python
libpython:      /Users/dhirajkhanna/opt/anaconda3/lib/libpython3.7m.dylib
pythonhome:     /Users/dhirajkhanna/opt/anaconda3:/Users/dhirajkhanna/opt/anaconda3
version:        3.7.5 (default, Oct 25 2019, 10:52:18)  [Clang 4.0.1 (tags/RELEASE_401/final)]
numpy:          /Users/dhirajkhanna/opt/anaconda3/lib/python3.7/site-packages/numpy
numpy_version:  1.17.3

python versions found: 
 /Users/dhirajkhanna/opt/anaconda3/bin/python
 /usr/bin/python3
 /usr/local/bin/python3
 /usr/bin/python
 /usr/local/bin/python
 /Users/dhirajkhanna/anaconda3/bin/python
 /Users/dhirajkhanna/.virtualenvs/object_recognition_detection/bin/python
 /Users/dhirajkhanna/.virtualenvs/object_recognition_detection1/bin/python
 /Users/dhirajkhanna/.virtualenvs/r-tensorflow/bin/python
 /Users/dhirajkhanna/venv/bin/python

Now when I try to load Python packages in a Python code chunk, RStudio crashes:现在,当我尝试在 Python 代码块中加载 Python 包时,RStudio 崩溃了:

import pandas as pd
import matplotlib.pyplot as plt

I get the "R Session Aborted" message.我收到“R Session Aborted”消息。 All this was working fine till I upgraded R. Any idea how to rectify this?在我升级 R 之前,所有这些都工作正常。知道如何纠正这个问题吗?

Edit编辑

While trying this in R, I get a segmentation error:在 R 中尝试此操作时,出现分段错误:

> library(reticulate)
> repl_python()
Python 3.7.6 (/Users/dhirajkhanna/opt/anaconda3/bin/python)
Reticulate 1.14 REPL -- A Python interpreter in R.
>>> import pandas as pd

 *** caught segfault ***
address 0x0, cause 'memory not mapped'

Traceback:
 1: py_eval_impl(code, convert)
 2: py_eval("_", convert = FALSE)
 3: doTryCatch(return(expr), name, parentenv, handler)
 4: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 5: tryCatchList(expr, classes, parentenv, handlers)
 6: tryCatch(py_eval("_", convert = FALSE), error = function(e) r_to_py(NULL))
 7: py_last_value()
 8: py_compile_eval(code)
 9: doTryCatch(return(expr), name, parentenv, handler)
10: tryCatchOne(expr, names, parentenv, handlers[[1L]])
11: tryCatchList(expr, classes, parentenv, handlers)
12: tryCatch(py_compile_eval(code), error = handle_error)
13: repl()
14: repl_python()

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 

I have also tried running a different version of Python (3.6) in a virtualenv but still no joy.我也尝试过在 virtualenv 中运行不同版本的 Python (3.6),但仍然没有任何乐趣。

A clean install of Rcpp and reticulate solved the problem.全新安装Rcppreticulate解决了这个问题。 More details here .更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 网状在 Rstudio 云中不起作用 - Python Reticulate not working in Rstudio Cloud 如何修复 RStudio 中的网状错误以使用 python? - How to fix reticulate error in RStudio to use python? 使用 reticulate 在 RStudio 和 shiny 应用程序中运行 python 脚本时出错 - Error running python script in RStudio and shiny app using reticulate 我应该如何最好地在 RStudio 中“运行”Python 脚本? 通过运行(使用 reticulate::repl_python()?)或来源? - How should I preferably "run" Python scripts in RStudio? Through run (using reticulate::repl_python()?) or source? 如何在不使用 R 网状包的情况下从 Anaconda 在 RStudio 中运行 Python 代码(直接使用 Python 解释器,无需任何 R 参与 - How to run Python code in RStudio from Anaconda without using R reticulate package (directly with Python interpreter without any R involvement 网状未设置python路径 - Reticulate not setting python path 在 R 中使用 Python 包(使用“reticulate”) - Using Python Packages in R (with "reticulate") R网状安装python包 - R reticulate install python packages 使用Knitr进行编织时,无法在RStudio中使用网状导入大熊猫 - Cannot import geopandas using reticulate in RStudio when knitting with knitr R Shiny 网状 package 和 ZA7F5F35426B927211FC9231B5Z3 - R Shiny reticulate package and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM