python - python -m 导致“查找模块规范时出错”
问题描述
医生说
一些 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
or from
。
推荐阅读
- python - 将 x 轴与条形 matplotlib 对齐
- python - How to run st_makevalid in shapely or similar pure python library?
- python - Is there a way to find end token
- java - TabPane 不会出现在边框的中心窗格中
- unit-testing - if(!response.error) unit testing using Jest Enzyme
- html - 我有两列,我希望第二列始终与第一列具有相同的高度
- angular - Ionic v3 Youtube Video Player Object(...) is not a function
- php - 如何修复 Magento 2 中已弃用的 Zend_Rest 客户端?
- c# - 发布 ASP.NET 以供本地使用,无需 IIS
- javascript - 让 vue-i18n 与 nuxt generate 一起工作时遇到问题