Python PyCharm 当前工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34304044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyCharm current working directory
提问by qwertyuip9
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.
最近,我在使用 PyCharm 时无法在我的代码中使用相对路径。例如,一个简单的open('test.txt', 'r')
将不起作用 - 因此我确定该文件与正在运行的 py 文件存在于同一级别。PyCharm 将返回此错误。
FileNotFoundError: [Errno 2] No such file or directory:
FileNotFoundError: [Errno 2] 没有这样的文件或目录:
After reading answers online on StackOverflow, I have tried multiple options including:
在 StackOverflow 上在线阅读答案后,我尝试了多种选择,包括:
- 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.
- 更改
test.txt
为./test.txt
- 关闭项目,删除 .idea 文件夹,打开包含代码的文件夹。
- 重新安装以及安装最新版本的 PyCharm。
- 使缓存无效并重新启动。
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)?
这些选项都不适合我。有什么办法可以告诉 PyCharm 刷新当前工作目录(甚至查看它认为当前工作目录在哪里)?
Thanks in advance!
提前致谢!
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.
编辑:我应该注意到在终端窗口中运行脚本是可行的。这似乎是 PyCharm 的问题,而不是脚本的问题。
采纳答案by Dan
I have Pycharm 4.5, so things might have changed a bit.
我有 Pycharm 4.5,所以事情可能会有所改变。
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
在此对话框中,单击包含源代码的文件夹,然后单击菜单中的蓝色文件夹,将其记为“源”文件夹。我相信这解决了 Pycharm 中的很多路径问题
Here is the link to "content roots": https://www.jetbrains.com/pycharm/help/content-root.html
这是“内容根”的链接:https: //www.jetbrains.com/pycharm/help/content-root.html
回答by falsetru
回答by andere
Change: Run > Edit Configurations > Working directory, which sets the working directory for a specific project. (This is on a Mac)
更改:运行 > 编辑配置 > 工作目录,它为特定项目设置工作目录。(这是在 Mac 上)
回答by Fioletibiel
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 ;)
有时它是不同的。我通过单击 Pycharm 工具栏上的“运行”,然后单击“编辑配置...”解决了我的问题,并将我的解释器更改为另一个实际的解释器。仅在设置中更改它无济于事,但是此操作已经可以了 ;)
回答by user9203442
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.
几分钟前我也遇到了同样的问题......但是,使用最新版本的 PyCharm 只需使用该文件的相对路径即可解决它......例如,一个简单的 f = open('test', 'r' ) 将工作。
回答by BeneIT
In PyCharm, click on "run/edit configurations..."
在 PyCharm 中,单击“运行/编辑配置...”
Then find your script file in the "Python" dropdown menu. Check the "Working Directory" entry and change it if necessary.
然后在“Python”下拉菜单中找到您的脚本文件。检查“工作目录”条目并在必要时进行更改。
回答by mithunpaul
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.
mac 用户的一些说明。在 mac 中,@andere 上面所说的设置工作目录是正确的。但是,如果您的代码位于不同的文件夹中,例如working_dir/src/
(例如经典的 java/scala 文件结构),在这种情况下您仍然需要设置 Sources Root。在 mac 的 PyCharm 中,这可以通过右键单击src/
文件夹 >将目录标记为 > 源根目录来完成。帮助我解决了很多类似的导入问题。希望这可以帮助某人。