Reputation: 1632
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 Stack Overflow, I have tried multiple options including:
test.txt
to ./test.txt
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)?
Running the script in a terminal window will work. This appears to be a problem with PyCharm and not the script.
Upvotes: 61
Views: 143048
Reputation: 65
I'm using PyCharm 2023.3, and I've also had trouble with PyCharm not being able to find files. Because PyCharm has lots of bells and whistles in this area, it can get really tangled up. To find and fix all trouble in this area:
In the "Run" menu, select "Edit Configurations..."
You will get a window listing all your configurations. Each one will have:
Line 1: An interpreter.
Line 2: The Python file to run.
Line 4: The working directory to use.
But at the bottom of that window, on the left, will be the blue hyperlink "Edit configuration templates..." When you click that hyperlink,
A new window also comes, which has a list on the left. Selecting "Python" in the list on the left will bring up another configuration that also lists:
Line 1: An interpreter.
Line 2: The Python file to run.
Line 4: The working directory to use.
In the "File" menu, select "Settings..." A window comes up which has 3 panes. In the left pane, expand "Project: ". Below that you should see two items, "Python Interpreter" and "Project Structure".
Select "Project Structure".
in the right-most pane will appear a text box showing the Content Root. Make sure that's correct.
Once you clean up all the working directories, make sure your project folder is not being synchronized by OneDrive, which can really mess with your paths.
Upvotes: 0
Reputation: 3536
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.
Upvotes: 0
Reputation: 11
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.
Upvotes: 1
Reputation: 31
Upvotes: 3
Reputation: 168
If you're trying to change the working directory of the Python Console inside PyCharm, you can find the corresponding settings under
Settings | Build, Execution, Deployment | Console | Python Console | Working directory
(see here).
Upvotes: 1
Reputation: 3305
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" -> fill out "Working Directory"
Upvotes: 16
Reputation: 141
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.
Upvotes: 4
Reputation: 131
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 ;)
Upvotes: 1
Reputation: 1608
Change: Run > Edit Configurations > Working directory, which sets the working directory for a specific project. (This is on a Mac)
Upvotes: 75
Reputation: 369404
__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')
Upvotes: 8
Reputation: 1136
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
Upvotes: 60