为什么程序在Spyder中运行时可以找到Pandas,但在R studio中却找不到?

问题描述 投票:0回答:2

我在 Windows 10 上运行 Python 3.8.2。 我有一个程序可以读取 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 至少能够导入库。 如有任何帮助,我们将不胜感激。

python pandas rstudio spyder
2个回答
0
投票

确保您的 RStudio 使用与 Spyder 相同的 Python。如果您使用 Anaconda,这可以轻松完成(只需确保您在同一环境中执行所有操作)。


0
投票

确保在“工具”>“全局选项”>“Python”下选择了 python 解释器。 一旦我设置了这个。 R 工作室识别了网状和 pandas 编码。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.