作者huggie (huggie)
看板Python
標題[問題] Package & PYTHONPATH
時間Thu Jul 23 14:53:58 2009
假設我有以下結構
main.py
Apackage/
__init__.py
file1.py
file2.py
file1.py 要用到 file2.py 的 foo()
因此在 file1.py 裡面有一行:
from Apackage.file2 import foo
平常我執行是 main.py,沒什麼問題。
但我 file1.py 裡面的 if __name__ == '__main__' 裡面有針對 file1.py 的
unit test。如果我直接執行 file1.py,例如: /home/xxx/yyy/Apackage/file1.py
時,他會說 No module named Apackage。
這時怎麼辦?我現在直接 run file1.py 的時後會選擇先設定環境變數 PYTHONPATH。
但有更理想的作法嗎?又大家是怎麼整理 unittest 架構的?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.129.160.62
※ 編輯: huggie 來自: 140.129.160.62 (07/23 14:55)
推 liangjr:我通常會有file1_test.py file2_test.py跟file1, file2 07/23 17:02
→ liangjr:同一層 然後另外一個test runner裡面import所有的test 07/23 17:03
→ liangjr:我也很好奇其他人都用什麼架構 07/23 17:03
→ huggie:你的方法比較理想 07/24 11:07
→ huggie:怎麼沒人給其他意見了 07/25 15:19
→ huggie:奇怪..我放了 file1_test.py 之後,再放 test runner, 07/25 15:38
→ huggie:test 就不跑了耶..=/ 07/25 15:38
→ huggie:他會說 run 0 tests 07/25 15:39