Python 无法导入,未知位置

标签 python function module pycharm

我在 pycharm 项目中有三个文件,例如,

project
├── main.py 
├── parser.py
└── test.py

parser.py 和 test.py 的代码相同。

def test():
    print('test')

但我只能执行下面的 main.py 并输出 test

from test import test

test()

当我在下面执行 main.py 时,

from parser import test

test()

它输出的

Traceback (most recent call last):
  File "C:/Users/lf/Desktop/jye_parser/main.py", line 1, in <module>
    from parser import test
ImportError: cannot import name 'test' from 'parser' (unknown location)

Process finished with exit code 1

这是项目架构。在这两种情况下,我都可以使用 Ctrl + Left Click 跳转到 main.py 中的 test 函数。

enter image description here

最佳答案

因为parser是python中的一个库。为解析器文件使用另一个名称。

关于Python 无法导入,未知位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58585690/

相关文章:

android - 编译钛安卓模块时出错

powershell - 我创建了一个自定义 powershell .psm1 模块,但它在编辑后不会更新

python - 缓存条目反序列化失败,条目被忽略

c++ - 使用此函数查找数组的长度

unix - 如果 [ $? -ne 0 ] 然后语法错误然后意外

javascript - 使用||函数返回值中的运算符

python - Google Wave Python 教程 - 下一步是什么?

python - 有人可以给我推荐一本好的 PyQt/PySide 教程/书籍/视频系列吗?

python - 在python中连接一个numpy矩阵的所有行

azure - Azure 门户中的 Powershell : This module requires Az. 帐户版本 2.12.5。在当前 PowerShell session 中导入早期版本的 Az.Accounts