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

Why can a program find Pandas when running in Spyder, but not in R studio?

我在 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 版本

这是我完整代码的 t运行 版本(它不会生成新的 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 给出的link,并确保一切设置正确。据我所知,它工作正常。我还尝试了两个简单的脚本。第一个只是尝试导入 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 至少能够导入库。感谢任何帮助。

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