在其中包含一個單一功能的top
文件夾我有一個Python文件(helpers.py
):蟒蛇:導入一個輔助函數在一個模塊中
def play(name, verbose=False):
if verbose:
print name + "with verbose on"
else:
print name + "verbose off"
而在一個名爲project
我的第二個Python文件的子文件夾(program.py
),其進口和使用helpers.py:
from .. import helpers as hp
def main(device,verbose=False):
hp.play(device)
#here goes the code
if __name__ == "__main__":
#Test the program
main('Foo')
main('Foo', verbose=True)
兩個文件夾包含__init__.py
文件以允許模塊導入。我想執行program.py文件來測試主函數。 繼How to fix "Attempted relative import in non-package" even with __init__.py我嘗試運行:
python -m top.project.program
越來越
# /usr/bin/python: No module named top.project
我不明白我在做什麼錯。任何幫助?謝謝
你能舉個例子嗎? – diegus
嘗試完整路徑。它給了我ValueError:嘗試在非包中的相對導入 – diegus