首先新建一个单独的py文件myfile.py,分别在命令行中敲入python myfile.py与python -m myfile.py运行下看看效果。
# 新建 myfile.py
sherry@sherry:~/Documents/github_code$ vim myfile.py
# myfile.py
import sys
print(sys.path)
print("hello world")
# cmd 中运行命令
sherry@sherry:~/Documents/github_code$ python -m myfile.py
sherry@sherry:~/Documents/github_code$ python myfile.py
发现:两行命令都可打印出路径和 hello world,但python -m 比python 多了一行error提示
/home/sherry/anaconda3/bin/python: Error while finding module
specification for 'myfile.py' (ModuleNotFoundError: __path__
attribute not found on 'myfile' while trying to find
'myfile.py')
可以看出,python -m myfile.py是以模块方式启动py文件的。
这也就解释了为啥我在开项目的时候遇到了下面这个报错。
# project structure
- file 1
-train.py
- file 2
- file 3
我在命令行中输入下面这行命令是报错的
python file.train -h
输入下面这行是正确的
python -m file.train -h