在Python 3中,我可以在命令行中使用-m标志运行任何目录作为Python包,或者在其中运行具有相对导入的python模块。然而,在Python 2中似乎不是这种情况。
假设我有以下文件结构:
为什么这两个命令的行为不同呢?
假设我有以下文件结构:
C:\mflag
└───pack
hey.py
And that the contents of hey.py are:
from __future__ import print_function
print("Hello!")
为什么这两个命令的行为不同呢?
C:\mflag>python -m pack.hey
Hello!
C:\mflag>python2 -m pack.hey
C:\Python27\python2.exe: No module named pack
pack\__init__.py
文件来使它成为一个正式的包。Python 3 可能会更加灵活一些。 - ShadowRanger