pythonpandasrstudiospyder

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


I'm on Windows 10 running Python 3.8.2. I've got a program that reads an excel file and makes a new one based on the information inside. The program works perfectly in Spyder, but when run in R studio is gives me a ModuleNotFoundError: No module named 'pandas'. It continues until it needs pandas at the read_file line, then throws an exception because pandas is not defined. Spyder is version 4.1.3, R studio is version 1.2.1335

This is a truncated version of my full code (it doesn't make the new excel file) but it has the same problem.

Code used:

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()

Edit:

So, I followed the link that @Trenton McKinney gave, and made sure that everything was set up correctly. As far as I can tell it is working correctly. I also tried two simple scripts. The first just tries to import pandas and print hello world.

import pandas


print('Hello World!')

This gives the same ModuleNotFoundError: No module named 'pandas' error as before, and then prints 'Hello World!'. The second code does the same think but imports tkinter to print a message box

import pandas
import tkinter
from tkinter import messagebox

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

This one also gives an error for importing pandas, but the rest runs correctly, so R Studio is able to import libraries at least. Any help is appreciated.


Solution

  • Make sure your RStudio is using the same Python as your Spyder. This can easily be done if you're using Anaconda (just ensure you're doing everything in the same environment).