问题描述
我想将我的Python脚本放入PATH列出的目录中,并通过在Windows上cygwin中任何位置键入其名称来调用该脚本.
I want to place my Python script into directory listed in PATH and call that script just by typing its name from any location in cygwin on Windows.
我正在使用shebang
I'm using shebang
#!/usr/bin/env python
在Linux机器上完美运行.
that works perfect on Linux machine.
尽管如此,通过从Windows cygwin调用以下行,我得到了一个错误:
Nevertheless by calling the following line from windows cygwin I get an error:
$ my_script.py some arguments
C:\app\Python36\python.exe: can't open file '/cygdrive/d/11_scripts/my_script.py': [Errno 2] No such file or directory
问题是由以下事实引起的:cygwin扩展了脚本路径,而python无法理解已安装驱动器的cygwin方式:/cygwin/d/...当我使用完整路径(以win格式)或相对路径运行脚本时,它可以工作.
Problem is caused by the fact that cygwin expands the script path and python doesn't understand the cygwin way of mounted drives: /cygwin/d/...When I run the script with the full path (in win format) or by relative path it works.
$ d:/11_scripts/my_script.py some arguments
您知道任何解决方法吗?这里讨论了类似的问题:通过Cygwin运行Python脚本的问题但是我想使用PATH.
Do you know about any workaround? Similar issue is discussed here:Problems running Python script via CygwinBut I want to use the PATH.
谢谢!
添加:目前,我使用了一种放在PATH中的bash包装器脚本:
ADDITION:at the moment I use kind of bash wrapper script that is put into PATH:
#!/usr/bin/bash
pypath='/cygdrive/c/app/Python36/python'
$pypath -u 'd:\11_scripts\my_script.py' $@
推荐答案
您正在跑步
C:\app\Python36\python.exe
是一些本地Windows Python,而 not 是Cygwin的Python,已编译为使用Cygwin.因此,自然不会对Cygwin路径一无所知.
which is some native Windows Python, and not the Python from Cygwin, compiled to use Cygwin. So naturally it would have no clue about Cygwin paths.
如果要使用Cygwin的Python,最好的选择是只使用Cygwin随附的Python.
If you want to use Python from Cygwin the best option is to just use the Python that comes with Cygwin.
如果您绝对必须使用Cygwin的本机Windows Python(您可以执行此操作,而我偶尔也有理由这样做,例如测试打算在使用Windows的Python for Windows上运行的代码)例如msvcrt模块)即可.但是您仍然需要将任何文件系统路径转换为本地Windows路径.在Cygwin中,您可以使用 cygpath 命令
If you absolutely must use a native Windows Python from Cygwin (you can do this, and I've occasionally had reason to, such as testing code intended to run on Python for Windows that uses the msvcrt module for example) you can do that. But you still need to convert any filesystem paths to a native Windows path. In Cygwin you can do this with the cygpath command like
$ cygpath -w -a /cygdrive/d/whatever
D:\whatever
将传递给Python的所有文件路径都包裹起来
Wrap any file paths passed to Python like
$ C:/Python36/python.exe "$(cygpath -w -a /cygdrive/d/whatever)"
如果您需要经常执行此操作,则可以制作包装脚本.
If you need to do this frequently you can make a wrapper script.
这篇关于运行放置在PATH(cygwin)中的Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!