我正在尝试部署一个更大的 python 项目,其中包含一个脚本 视窗。安装在 UNIX 上工作正常,但在 windows 上似乎 无法运行脚本。我已经创建了一个最小的例子 GitHub 来演示 问题。
运行 pip install .
时,脚本安装到 C:\Program Files\Python39\Scripts\hello-world
。该目录包含在
PATH
。我的用户具有读取和执行权限。哈希爆炸
hello-world
替换为 #!c:\program files\python39\python.exe
,
这似乎也正确。但是,在控制台中运行 hello-world
产生以下错误:
C:\Users\malte>hello-world
'hello-world' is not recognized as an internal or external command,
operable program or batch file.
我希望这能奏效。为什么不呢?我该如何解决这个问题?
我尝试了一些东西,并注意到了另一个奇怪的行为
语境。运行 shutil.which('hello-world')
返回 None
。如果我
将 hello-world
重命名为 hello-world.exe
,然后调用 hello-world
导致一个奇怪的错误,很可能是因为 windows
现在认为脚本是二进制文件。没关系。除了现在
shutil.which('hello-world')
返回 'C:\\Program Files\\Python39\\Scripts\\hello-world.EXE'
。为什么?
存储库的 master
分支现在包含一个跨平台的解决方案,我从这里了解到:https://matthew-brett.github.io/pydagogue/installing_scripts.html。
我不是很清楚,但是 Python 文档中的以下段落似乎指出带有 hashbang 行的脚本必须具有与 python 启动器关联的扩展名,以便 Windows 能够识别它:https://docs.python.org/3/using/windows.html#from-a-script
换句话说,我假设使用 scripts
的 setuptools.setup
参数跨平台工作的假设受某些条件的限制,这些条件隐藏在 Python 文档的某处。