![](/img/trans.png)
[英]How to create an executable file from python scripts for all windows?
[英]How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?
對於直接調用Python運行的腳本( python myscript.py
)和直接運行的腳本( myscript.py
),它應顯示可執行文件和Python版本的路徑。 腳本不應對系統配置做出太多假設。 例如,它應該處理可能沒有可用Python的情況。
基本原理
我正在以不同的方式來設置運行Python腳本的環境,我認為讓腳本告訴我當前的配置是有幫助的。 我擔心OS提供的標准方法PATH
環境變量以及文件類型與處理程序( assoc
和ftype
命令以及PATHEXT
環境變量)的關聯。 這使pylauncher超出了此問題的范圍。
這是我的第一個解決方案:
解決方案1
@echo off
set test_script=.pyexe.py
rem Let's create temporary Python script which prints info we need
echo from __future__ import print_function; import sys; print(sys.executable); print(sys.version) > %test_script%
echo Python accessible through system PATH:
python %test_script%
echo ---
echo Python set as handler for Python files:
%test_script%
del %test_script%
set test_script=
但是,我遇到了這個問題。 如果沒有與Python文件關聯的有效Python解釋器,則嘗試使用some_script.py
打開Python文件時, some_script.py
彈出“ 打開方式”對話框。 解決此問題需要非常了解批處理文件。 因此,嘗試提出一個解決方案時,我提出了以下問題:
原始批處理文件的改進版本現在看起來像這樣:
解決方案1b
@echo off
setlocal
set test_script=.pyexe.py
rem Let's create temporary Python script which prints info we need
echo from __future__ import print_function; import sys; print(sys.executable); print(sys.version) > %test_script%
echo Python accessible through the system PATH:
python %test_script%
echo ---
echo Python set as a handler for Python files:
rem We need to check if a handler set in the registry exists to prevent "Open With"
rem dialog box in case it doesn't exist
rem ftype Python.File hypothetical return value:
rem Python.File="%PYTHON_HOME%\python.exe" "%1" %*
for /f "tokens=2 delims==" %%i in ('ftype Python.File') do set reg_entry=%%i
rem ...now in 'reg_entry' variable we have everything after equal sign:
rem "%PYTHON_HOME%\python.exe" "%1" %*
set "handler="
setlocal enableDelayedExpansion
for %%A in (!reg_entry!) do if not defined handler endlocal & set handler=%%A
rem ...now in 'handler' variable we have the first token:
rem "%PYTHON_HOME%\python.exe"
rem Now we expand any environment variables that might be present
rem in the handler's path
for /f "delims=" %%i in ('echo %handler%') do set expanded_handler=%%i
if exist "!expanded_handler!" (
"%test_script%"
) else (
if not "!handler!" == "!expanded_handler!" (
set "handler=!expanded_handler! ^(!handler!^)"
)
echo Handler is set to !handler! which does not exist
)
del %test_script%
這是避免上述兩個問題的另一種方法:
解決方案2
@echo off
setlocal
echo Python accessible through the system PATH:
where python
echo ---
echo Python set as a handler for Python source files (.py):
for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\.py /ve') do set "file_type=%%k"
for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\%file_type%\shell\open\command /ve') do echo %%k
...和改進的版本:
解決方案2b
@echo off
setlocal EnableDelayedExpansion
echo Python interpreter accessible through the system PATH:
where python
if not errorlevel 1 (
python -c "from __future__ import print_function; import sys; print(sys.version)"
)
echo ---
echo Python interpreter registered as a handler for Python source files (.py):
reg query HKCR\.py /ve >nul 2>&1
if errorlevel 1 (
echo No "HKEY_CLASSES_ROOT\.py" registry key found
) else (
for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\.py /ve 2^>nul') do set "file_type=%%k"
if "!file_type!"=="(value not set)" (
echo "No file type set for .py extension"
) else (
reg query HKCR\!file_type!\shell\open\command /ve >nul 2>&1
if errorlevel 1 (
echo No "HKEY_CLASSES_ROOT\!file_type!\shell\open\command" registry key found
) else (
for /f "skip=2 tokens=1,2*" %%i in ('reg query HKCR\!file_type!\shell\open\command /ve 2^>nul') do set "handler=%%k"
if "!handler!"=="(value not set)" (
echo No command set for !file_type!
) else (
echo !handler!
)
)
)
)
這可能是您要尋找的:
python -c "import sys; print sys.executable"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.