How to set working directory for projects in PyCharm?
Question:
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.
Answers:
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
__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')
Change:
Run > Edit Configurations > Working directory,
which sets the working directory for a specific project. (This is on a Mac)
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 😉
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.
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.
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.
EXACT ANSWER TO SOLVE THIS ISSUE ,,
- GO TO EDIT CONFIGURATION (just LEFT side of GREEN CODE RUNNER ICON)
- click on python (not any specific python script) ONLY SELECT PYTHON
- then below right side click on [edit configuration templetes]
- select current working dir by going into those blocks
- 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.
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.
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
__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')
Change:
Run > Edit Configurations > Working directory,
which sets the working directory for a specific project. (This is on a Mac)
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 😉
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.
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.
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.
EXACT ANSWER TO SOLVE THIS ISSUE ,,
- GO TO EDIT CONFIGURATION (just LEFT side of GREEN CODE RUNNER ICON)
- click on python (not any specific python script) ONLY SELECT PYTHON
- then below right side click on [edit configuration templetes]
- select current working dir by going into those blocks
- 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.