【发布时间】:2021-08-17 18:15:49
【问题描述】:
我感觉被 Python 打败了,以至于我不知道还能尝试什么。我正在运行 Python 3.9,但我一生都无法让导入正常工作。这是我的目录结构:
hello-world-proj
|
|.. core
|
|..
__init__.py
hello_world_main.py
|
|.. test
|
|..
__init__.py
test_hello_world_main.py
hello_world_main.py
def hello_world_main():
return "myString"
if __name__ == "__main__":
print(hello_world_main()) # call hello_world_main()
test_hello_world_main.py
import unittest
import os
from core import hello_world_main
class HelloWorldTest(unittest.TestCase):
self.assertEquals(hello_world_main(), "myString")
if __name__ == '__main__':
unittest.main(exit=False)
当我运行 python test_hello_world_main.py 时,我遇到了错误:
ImportError: cannot import name hello_world_main
我已经做了export PYTHONPATH=$PYTHONPATH:/path/to/hello-world-proj
我被打败了,不知道现在该怎么办。为什么会这样?
【问题讨论】:
-
core模块中的__init__.py文件是什么样的? -
@aminrd 他们都是空文件