53

Recently, I'm unable to use relative paths in my code while using PyCharm. For instance, a simple open('test.txt', 'r') will not work - whereupon I am sure the file exists in the same level as the running py file. PyCharm will return this error.

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

After reading answers online on StackOverflow, I have tried multiple options including:

  • Changing test.txt to ./test.txt
  • Closing project, deleting the .idea folder, open the folder with code.
  • Reinstalling as well as installing the latest version of PyCharm.
  • Invalidating caches and restarting.

None of these options have worked for me. Is there someway I can tell PyCharm to refresh the current working directory (or even to see where it thinks the current working directory is)?

Edit: I should note that running the script in a terminal window will work. This appears to be a problem with PyCharm and not the script.

starball
  • 20,030
  • 7
  • 43
  • 238
qwertyuip9
  • 1,522
  • 2
  • 16
  • 24
  • 3
    you can put this in your script to see the current working directory `import os print(os.getcwd()) ` – Dan Dec 16 '15 at 04:45
  • Interesting, it says that it's at `C:\Program Files (x86)\JetBrains\PyCharm 5.0.2\jre\jre\bin` – qwertyuip9 Dec 16 '15 at 04:47
  • Are you running with elevated permissions on the command line? If you are on a Unix like system then you should check your permissions for the file – Dan Dec 16 '15 at 04:48
  • 1
    nvm, you are on windows :) – Dan Dec 16 '15 at 04:48

9 Answers9

68

Change: Run > Edit Configurations > Working directory, which sets the working directory for a specific project. (This is on a Mac)

andere
  • 1,488
  • 1
  • 11
  • 10
56

I have Pycharm 4.5, so things might have changed a bit.

Try going to Settings > Project > Project Structure

On this dialog, click your folder that has the source code in it, and then click the blue folder in the menu to note it as "source" folder. I believe this fixes a lot of the path issues in Pycharm

Here is the link to "content roots": https://www.jetbrains.com/pycharm/help/content-root.html

Dan
  • 1,096
  • 9
  • 13
  • It was a similar fix that I just found ([link here](http://stackoverflow.com/questions/30353714/python-flask-app-runs-from-wrong-folder-after-run-by-pycharm-on-127-0-0-1)), I had to add the working directory by clicking on the project name on the top right and editing its configurations. It seems counter intuitive to have to add this extra step - perhaps there was some issues in creating the project in the first place. I'll marks yours as correct since it was the closest! – qwertyuip9 Dec 16 '15 at 05:01
  • Sadly this didn't work for me on OSX - even though I had the root of my project set as a Source folder, where my script is, the Runner was unable to locate the script. The only solution I found was @andere's below, where you explicitly set the working directory to the absolute path. I'm not sure if this breaks when the project is moved... – davidA Feb 10 '17 at 23:25
  • @meowsqueak Glad you worked it out... With your problem I also typically us `os.chir(directory_path)` using a directory path i split from `__file__`. Doing that helps when running the script outside of an IDE and in the event that the script is called from a directory other than where the script and text file is located. – Dan Feb 11 '17 at 04:01
15

Current version 2019.2 somehow ignores "source root" from the "project structure". Here's how to actually enforce it:

Run -> Edit Configurations -> Python -> "Edit Templates" this buttin -> fill out "Working Directory"

Ufos
  • 3,083
  • 2
  • 32
  • 36
  • +1. This solution solved my problem. What happened: I had a project folder with some python files that worked with paths (reading files and stuff). Than I pulled, new directory from github, and moved those python files to new folder. Working Directory stayed as old one. So this solution solved it, thanks :) – enigmq Dec 03 '19 at 14:08
  • Thanks!!! I used to change manually the working directory for each file :) Didn't know there was a template – Alon Samuel Apr 28 '20 at 07:35
  • Thank you! In VSCode working directory was a directory with a project but in PyCharm a working directory becomes a parent directory of a script, not a project. So this is fixed my problem and behavior has become as expected. – Huginn Mar 29 '23 at 09:12
7

__file__ refers to file path. So you can use the following to refer file in the same directory:

import os

dirpath = os.path.dirname(__file__)
filepath = os.path.join(dirpath, 'test.txt')
open(filepath, 'r')
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I like your solution better than mine :) I was trying to keep it at the Pycharm level though haha – Dan Dec 16 '15 at 05:01
  • 1
    Thanks! My problem was more focused in PyCharm but good to know that I could use the os module too! – qwertyuip9 Dec 16 '15 at 05:02
3

In PyCharm, click on "run/edit configurations..."

Then find your script file in the "Python" dropdown menu. Check the "Working Directory" entry and change it if necessary.

BeneIT
  • 131
  • 4
1

EXACT ANSWER TO SOLVE THIS ISSUE ,,

  1. GO TO EDIT CONFIGURATION (just LEFT side of GREEN CODE RUNNER ICON)
  2. click on python (not any specific python script) ONLY SELECT PYTHON
  3. then below right side click on [edit configuration templetes]
  4. select current working dir by going into those blocks
  5. It will change the CWD of all python file that exists in project folder.. then all file will understand the RELATIVE PATH that starts from your actual project name..

i hope this will resolve all your issue related path.

0

Sometimes it is different. I solved my problem by clicking "Run" at the Pycharm's toolbar and then "Edit Configurations..." and I change my Interpreter to another actual one. Just changing it in the settings does not help, but this opperation already does ;)

Fioletibiel
  • 121
  • 1
  • 3
0

I too had the same issue few minutes ago...but,with the latest version of PyCharm it is resolved by simply using the relative path of that file.. For instance, a simple f = open('test', 'r') will work.

0

A little clarification for mac users. In mac, what @andere said above is correct for setting working directory. However, if your code is in a different folder, say working_dir/src/ (like classic java/scala file structure) in that case you still need to set your Sources Root. In mac's PyCharm this can be done by right clicking on the src/ folder > Mark Directory as > Sources Root. Helped me with lot of similar import issues. Hope this helps someone.

mithunpaul
  • 3,268
  • 22
  • 19