[英]python -m causes “Error while finding module specification for”
醫生說
一些Python模塊也可用作腳本。 可以使用python -m module [arg] ...調用它們,該模塊執行模塊的源文件,就好像您在命令行上拼出了全名一樣
我寫了這段代碼來幫助自己理解-m
def print_hi(n=1):
print('hi'*n)
if __name__ == "__main__":
print_hi(9)
我將此保存為文件aname.py
。
然后我運行了此命令,並獲得了預期的結果。
$ python aname.py
hihihihihihihihihi
當我將文件作為模塊執行時,出現此錯誤
$ python -m aname.py
/usr/bin/python: Error while finding module specification for 'aname.py' (AttributeError: module 'aname' has no attribute '__path__')
是什么導致此錯誤? 如何解決?
您應該使用不帶.py后綴的m,即: $ python -m aname
從手冊頁:
-m module-name
Searches sys.path for the named module and runs the corresponding .py file as
a script.
參數m是一個模塊,類似於import
或from
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.