还是stack-overflow 友好
https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time
直接看原文解释:
There is a big difference between directly running a Python file, and importing that file from somewhere else. Just knowing what directory a file is in does not determine what package Python thinks it is in. That depends, additionally, on how you load the file into Python (by running or by importing).
There are two ways to load a Python file: as the top-level script, or as a module. A file is loaded as the top-level script if you execute it directly, for instance by typing python myfile.py
on the command line. It is loaded as a module if you do python -m myfile
, or if it is loaded when an import
statement is encountered inside some other file.
我也终于理解了,python -m 这样就行了!!!