问题描述
最近,在使用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.
在StackOverflow上在线阅读答案后,我尝试了多种选择,包括:
After reading answers online on StackOverflow, I have tried multiple options including:
- 将
test.txt
更改为./test.txt
- 关闭项目,删除.idea文件夹,使用代码打开该文件夹.
- 重新安装以及安装最新版本的PyCharm.
- 使缓存无效并重新启动.
- 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.
这些选项中没有一个对我有用.有什么办法可以告诉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当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!