[英]Why `python main.py\` (different with `python main.py`) executable on Windows?
# a.py
import b
# b.py
import a
print(1)
When I execute python a.py
, import b works well but python a.py\
not.当我执行
python a.py
, import b 效果很好,但python a.py\
不行。
I run it on Windows 11. I guess this happen's only on Windows, not linux.我在 Windows 11 上运行它。我想这只会发生在 Windows 上,而不是 linux 上。
When I check system path using sys.path
, python a.py\
contains: (currentDir)/a.py
, not (currentDir)
当我使用
sys.path
检查系统路径时, python a.py\
包含: (currentDir)/a.py
,而不是(currentDir)
I wonder why python main.py\
is executable on Windows.我想知道为什么
python main.py\
可以在 Windows 上执行。 And I guess it could induce some bugs for python-based programs.而且我猜它可能会导致一些基于 python 的程序的错误。
On Windows, backslash is the path separator, thus python a.py\
tries to run a directory named "a.py" as a module (ie. it looks for a.py\__main__.py
).在 Windows 上,反斜杠是路径分隔符,因此
python a.py\
尝试将名为“a.py”的目录作为模块运行(即它查找a.py\__main__.py
)。 In your case, Python will error with a message that looks something like this:在您的情况下, Python 将出现错误,并显示如下消息:
can't open file 'c:\some\path\here\a.py\': [Errno 22] Invalid argument
because "a.py" is actually a file, so attempting to treat it as a directory fails.因为“a.py”实际上是一个文件,所以尝试将其视为目录失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.