简体   繁体   English

knitr - 导入python模块时出错

[英]knitr - error when importing python module

I am having trouble when running the python engine in knitr. 我在knitr中运行python引擎时遇到了麻烦。 I can import some modules but not others. 我可以导入一些模块而不是其他模块。 For example I can import numpy but not pandas. 例如,我可以导入numpy但不能导入pandas。

{r, engine='python'} import pandas

I get the error. 我收到了错误。

Quitting from lines 50-51 (prepayment.Rmd) 
Error in (knit_engines$get(options$engine))(options) : 
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named pandas
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir -> <Anonymous>
In addition: Warning message:
running command ''python'  -c 'import pandas' 2>&1' had status 1 
Execution halted

It must be something to do with system path and where I am pointing. 它必须与系统路径和我指向的位置有关。

Normally in IPython i use 通常在我使用的IPython中

import sys
sys.path.append('/path/to/directory/')

To add paths. 添加路径 I don't know what the problem is. 我不知道问题是什么。

Use: 使用:

{python engine.path="C:/anaconda/python.exe"}
import pandas

change C:/anaconda/python.exe to your python path. C:/anaconda/python.exe更改为您的python路径。

This happened to me too, in Atom. 在Atom中,这也发生在我身上。

Check to make sure the two files are in the same directory. 检查以确保这两个文件位于同一目录中。 Put them in the same folder or path. 将它们放在同一个文件夹或路径中。

Then, go into Terminal or Command Line and type 然后,进入终端或命令行并键入

cd Downloads

(or whatever large directory the files are in). (或文件所在的任何大型目录)。 Then, type in smaller folder like: cd _____ , such as cd animals . 然后,键入较小的文件夹,如: cd _____ ,如cd animals There, you can open the one file that you want to have the other file import to by typing python3 -i ____.py (file name). 在那里,您可以通过键入python3 -i ____.py . python3 -i ____.py (文件名)打开要将其他文件导入到的一个文件。 Make sure the file has from ___(master file) import * and you can run the code in terminal. 确保文件from ___(master file) import * ,您可以在终端中运行代码。

This should work. 这应该工作。 The problem is you can't import by running the code. 问题是您无法通过运行代码导入。 Instead, you have to link the two using terminal or command line. 相反,您必须使用终端或命令行链接两者。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM