2011-10-19 26 views
1

它應該顯示通過直接調用Python(python myscript.py)以及直接運行腳本(myscript.py)運行的腳本的可執行文件和Python版本的路徑。腳本不應該對系統的配置做太多的假設。例如,它應該處理可能沒有可用的Python的情況。如何編寫一個批處理文件,顯示在Windows上處理Python腳本的可執行文件和Python版本的路徑?

理由
我正在玩設置運行Python腳本環境的不同方式,我認爲讓腳本告訴我目前的配置是什麼會有幫助。我關心的是OS提供的標準方法 - PATH環境變量以及文件類型與處理程序(assocftype命令以及PATHEXT環境變量)的關聯。這使pylauncher超出了這個問題的範圍。

回答

2

這是我的第一個解決方案:

SOLUTION但是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解釋器嘗試打開Python文件時some_script.py彈出打開使用系統對話框。解決這個問題需要很好的批量文件知識。因此,試圖拿出一個解決方案,我問過以下問題:

原來的批處理文件的改進版本現在看起來像這樣:

SOLUT ION 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% 

這是另取上述兩種的避免問題:

SOLUTION 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! 
      ) 
     ) 
    ) 
) 
+0

此行'FOR/F 「令牌= 2個delims ==」 %% i的( 'FTYPE Python.File')不設置reg_entry = %%我'如果路徑包含'!'會導致錯誤的結果,因爲在頂部啓用了延遲擴展。您以前不使用它,因此請在頂部更改爲DisableDelayedExpansion。然後,您需要在'for/f'delims =「%% i in('echo%handler%')之後啓用延遲擴展do set expanded_handler = %% i' – dbenham

+0

感謝您的分析。不會刪除'EnableDelaydExpansion'具有與在頂部使用'DisableDelayedExpansion'相同的效果?我就是這樣編輯的。至於在'...之後啓用延遲擴展...set expanded_handler = %% i' line;它在'set'handler =「'行之後已經啓用了。這不好嗎? –

0

這可能是你在找什麼:

python -c "import sys; print sys.executable" 
+0

歡迎來到Stackoverflow雷蒙德!很高興在這裏見到你。我非常瞭解-c選項,您的解決方案對於第一種情況是可以的。儘管如此,在第二種情況下,我們必須使用完全相同的代碼調用獨立的Python腳本,所以我認爲我可以通過在第一種情況下使用臨時腳本來避免代碼重複:) –

+0

臨時腳本看起來很棒。 –

相關問題