在我開發的Python系統中,我們通常有這個模塊結構。Python導入:導入一個沒有.py擴展名的模塊?
mymodule/
mymodule/mymodule/feature.py
mymodule/test/feature.py
這讓我們的小測試框架能夠輕鬆地導入測試/ feature.py並運行單元測試。然而,我們現在有一些shell腳本(這是用Python編寫的)需要:
mymodule/
mymodule/scripts/yetanotherfeature.py
mymodule/test/yetanotherfeature.py
yetanotherfeature.py由模塊Debian包到/ usr/bin中安裝。但我們顯然不希望在那裏有.py擴展名。所以,爲了使測試框架,仍然能夠導入模塊,我必須這樣做符號鏈接的thingie:
mymodule/
mymodule/scripts/yetanotherfeature
mymodule/scripts/yetanotherfeature.py @ -> mymodule/scripts/yetanotherfeature
mymodule/test/yetanotherfeature.py
是否有可能通過文件名Python中導入模塊,或者你能想到的更優雅的解決方案呢?
這有什麼錯符號鏈接?這是處理這件事的最優雅的方式。你也應該使用它進行部署。/usr/bin/feature是指向/usr/bin/feature.py的鏈接,通過使實現清晰可見。 – 2009-05-19 10:40:00