问题
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.
I'm using shebang
#!/usr/bin/env python
that works perfect on Linux machine.
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
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
Do you know about any workaround? Similar issue is discussed here: Problems running Python script via Cygwin But I want to use the PATH.
Thank you!
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' $@
回答1:
You're running
C:\app\Python36\python.exe
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.
If you want to use Python from Cygwin the best option is to just use the Python that comes with Cygwin.
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
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.
来源:https://stackoverflow.com/questions/56053534/running-python-script-placed-in-path-cygwin