PyCharm 当前工作目录 [英] PyCharm current working directory

查看:49
本文介绍了PyCharm 当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在使用 PyCharm 时无法在我的代码中使用相对路径.例如,一个简单的 open('test.txt', 'r') 将不起作用 - 因此我确定该文件与正在运行的 py 文件存在于同一级别.PyCharm 将返回此错误.

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] 没有那个文件或目录:

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

在 StackOverflow 上在线阅读答案后,我尝试了多种选择,包括:

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

  • test.txt改为./test.txt
  • 关闭项目,删除 .idea 文件夹,打开包含代码的文件夹.
  • 重新安装并安装最新版本的 PyCharm.
  • 使缓存无效并重新启动.

这些选项都不适合我.有什么办法可以告诉 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 的问题,而不是脚本的问题.

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 4.5,所以事情可能会有所改变.

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

尝试转到设置 > 项目 > 项目结构

Try going to Settings > Project > Project Structure

在此对话框中,单击包含源代码的文件夹,然后单击菜单中的蓝色文件夹,将其标记为源"文件夹.我相信这解决了 Pycharm 中的很多路径问题

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

这是内容根源"的链接:https://www.jetbrains.com/pycharm/help/content-root.html

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

这篇关于PyCharm 当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆