【问题标题】:Why can a program find Pandas when running in Spyder, but not in R studio?为什么程序在 Spyder 中运行时可以找到 Pandas,而在 R studio 中却找不到?
【发布时间】:2020-11-20 16:05:14
【问题描述】:

我在运行 Python 3.8.2 的 Windows 10 上。我有一个程序可以读取一个 excel 文件并根据里面的信息创建一个新文件。该程序在 Spyder 中运行良好,但在 R studio 中运行时,我得到了一个ModuleNotFoundError: No module named 'pandas'。它一直持续到 read_file 行需要 pandas 为止,然后由于未定义 pandas 而引发异常。 Spyder 是 4.1.3 版,R studio 是 1.2.1335 版

这是我完整代码的截断版本(它不会生成新的 excel 文件),但它有同样的问题。

使用的代码:

import pandas
import os
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog

class app():      
    def __init__(self,root):
        self.root=root
        self.file=None
        while self.file == None:
            try:
                self.file = filedialog.askopenfile(parent=root,mode='rb',title='Choose a file')                   # Select the file with the list
                read_file = pandas.read_excel(self.file, sheet_name='Sheet 1            ')               # reads the file in
                read_file.to_csv (os.getcwd()+"\\CSV.csv", index = None, header=True)                       # convert to csv to make into a datafram
                self.df = pandas.read_csv(os.getcwd()+"\\CSV.csv",)     # make into dataframe
   
            except IOError:
                messagebox.showinfo(message="The file you are trying to edit is still open.  Please close it and try again")
                self.Repeat()
            except Exception as e:
                print(e)
                self.Repeat()
        
    def Repeat(self):                                                                                
        self.file="a"                                                                                     # dialog box for if there's an error.  Asks if you want to try again or cancel the program 
        if messagebox.askretrycancel("askretrycancel", "Try again?"):  
           self.file=None
        else:
            root.destroy()
        
        
root=tk.Tk()
app=app(root)
root.mainloop()

编辑:

所以,我点击了@Trenton McKinney 提供的链接,并确保一切设置正确。据我所知,它工作正常。我还尝试了两个简单的脚本。第一个只是尝试导入 pandas 并打印 hello world。

import pandas


print('Hello World!')

这给出了与以前相同的ModuleNotFoundError: No module named 'pandas' 错误,然后打印“Hello World!”。第二个代码的想法相同,但导入 tkinter 以打印消息框

import pandas
import tkinter
from tkinter import messagebox

root=tkinter.Tk()
print('Hello World!')
messagebox.showinfo(message="Hello World!")
root.destroy()

这也是导入 pandas 的错误,但其余运行正常,因此 R Studio 至少能够导入库。任何帮助表示赞赏。

【问题讨论】:

  • @MattDMo 您的陈述不正确。 Python 可以在 R Studio 中运行,就像 R 可以在 Jupyter 中运行一样。
  • 请参阅Installing and Configuring Python with RStudio 以正确配置您的环境。我也会尝试一个简单的脚本来测试功能。
  • @MattDMo 您实际上可以在 RStudio 中运行 Python,尽管它显然没有针对它进行优化。 community.rstudio.com/t/r-python-in-ide/279/4
  • 试试import sys; print(sys.path)。确保您使用的解释器与安装了 pandas 的解释器相同。

标签: python pandas rstudio spyder


【解决方案1】:

确保您的 RStudio 使用与 Spyder 相同的 Python。如果您使用 Anaconda,这很容易做到(只要确保您在同一个环境中进行所有操作)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2021-02-12
    • 1970-01-01
    • 2014-09-27
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多