Nahidujjaman Hridoy
Nahidujjaman Hridoy

Reputation: 2217

File Not Found When Working with PyCharm but its okay when working jupyter notebook

I am trying to importing a file with pandas in python. Both the file and python script are in same directory. But when I am trying to open that file in pycharm, after executing that script I am getting an error:

FileNotFoundError: [Errno 2] No such file or directory:

But When I am running that same command in jupyter notebook, the file opens just fine. This is the code I am using to open file:

hos = "Hospital Lists.xlsx"
hospital = pd.read_excel(hos, sheet_name=0)

Note That: Both the files and script are in same directory

To Solving this problem I have to use absolute path in pycharm:

hos = "F:\My Coding\Python\Hospital Lists.xlsx"
hospital = pd.read_excel(hos, sheet_name=0)

This solves the problem

But why relative path working in jupyter but not in pycharm? This is my question.

Upvotes: 0

Views: 723

Answers (2)

Nahidujjaman Hridoy
Nahidujjaman Hridoy

Reputation: 2217

I have found the solution. Because I was running the streamlit. But I wasn't running the powershell from the script folder. That is why I was facing the file not found error. But in the case of jupyter, I was running it from the script folder. If I run the powershell from the script folder then relative path is working.

Upvotes: 0

Captain Trojan
Captain Trojan

Reputation: 2921

Those two environments clearly use a different working directory. You can add

import os
print(os.getcwd())

to your code to see those working directories when you run the script from PyCharm or Jupyter.

Upvotes: 1

Related Questions