文件可以在控制台中找到,但无法使用运行工具窗口找到 [英] File can be found in console but can't be found using run tool window

查看:41
本文介绍了文件可以在控制台中找到,但无法使用运行工具窗口找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OS X 上使用 Pycharm.

I'm using Pycharm on OS X.

结构为:

/Project
 /src
  codeA.py
  codeB.py
 /Data
  data.txt

src 被标记为 Sources Root,Data 被标记为 Resource root.

src is marked as Sources Root and Data is marked as Resource root.

问题是如果我在 codeA.py 中编写

The problem is if I write in codeA.py

import codeB
with open("./Data/data.txt",'r'):
    pass

如果我

  1. 在控制台中一行一行执行代码,可以找到文件.

  1. Execute code line by line in console, the file can be found.

点击Run code.py"执行;我会收到 No such File 错误.

Execute by clicking "Run code.py" I'll get No Such File error.

在终端执行codeA.py,我什至无法导入codeB.

Execute codeA.py in terminal, I can not even import codeB.

如果我改写../Data/data.txt,那么方法2可以运行,但方法1会得到无此类文件错误.

If I write ../Data/data.txt instead, then method 2 can run but method 1 will get No Such File Error.

我已经检查了项目解释器路径和控制台路径.他们是一样的.

I've checked project interpreter path and console path. They are the same.

有什么办法可以解决这个问题吗?

Any idea to solve this problem?

推荐答案

Pycharm 的控制台将包含项目根目录到 PYTHONPATH.您可以在 Settings > 中查看和设置它.构建、执行、部署 >控制台 >Python 控制台.

Pycharm's console will include project root to PYTHONPATH. You can see and set this in Settings > Build, Execution, Deployment > Console > Python Console.

您的运行配置可能有误,请确保工作目录正确,并且选中了 Add content/source root to PYTHONPATH 复选框.

Your run config is probably wrong, ensure the working directory is correct, and that Add content/source roots to PYTHONPATH checkboxes are checked.

为避免路径问题,建议使用绝对路径:

To avoid any path problem, it is advised to work with absolute path:

# get project path from main entry file
file_path = os.path.dirname(os.path.abspath(__file__))
project_path = os.path.abspath(os.path.join(file_path, os.path.pardir))

data_path = os.path.join(project_path, 'Data/data.txt')

这篇关于文件可以在控制台中找到,但无法使用运行工具窗口找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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