2011-10-19 12 views
1

Pythonの直接呼び出し(python myscript.py)で実行されるスクリプトと、直接実行されるスクリプト(myscript.py)の実行可能ファイルとPythonのバージョンを表示する必要があります。スクリプトは、システムの構成についてあまりにも多くの仮定をするべきではありません。例えば、利用可能なPythonが存在しない状況を処理する必要があります。Windows上のPythonスクリプトを扱うPythonの実行ファイルとバージョンへのパスを示すバッチファイルを書く方法は?

理論的に
私はPythonスクリプトを実行する環境を設定する方法が異なっているので、現在の設定が何であるか教えてくれるスクリプトがあれば助かります。私は、OSによって提供される標準的な手段、すなわち、環境変数であるPATHと、ファイルタイプとハンドラとの関連付け(assocftypeコマンドと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= 

SOLUTION:

答えて

2

は、ここに私の最初のソリューションです。有効なPythonインタプリタがPythonファイルに関連付けられていないときに、some_script.pyでPythonファイルを開こうとすると、が開き、システムダイアログが開きます。この問題を解決するには、バッチファイルに関する非常に優れた知識が必要です。オリジナルのバッチファイルの

改良版が今になります。したがって、解決策を考え出すしようとしている私は、次の質問をしてきましたこのように:

SOLUT

@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 

SOLUTION ...と改良版: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を図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

/F "トークン= 2 delims ==" %%のためのこの行 'I IN( 'FTYPEのPython.File')を設定するのですreg_entry = %% I遅延拡張が一番上に有効になっているため、パスに '! 'が含まれていると誤った結果が返されます。以前はそれを使用していないので、一番上のDisableDelayedExpansionに変更してください。次に、for/fの後に遅延拡張を有効にする必要があります。delims = "%% i in( 'echo%handler%')do set expanded_handler = %% i' – dbenham

+0

あなたの分析をお寄せいただきありがとうございます。 'EnableDelaydExpansion'を削除しないと、上の' DisableDelayedExpansion'を使うのと同じ効果がありますか?それが私がこれを編集した方法です。 ''の後に遅延拡張を有効にすることに関しては...set expanded_handler = %% i行; 'set" handler = "'の後ですでに有効になっています。これは大丈夫ですか? –

0

これは、あなたが探しているものかもしれません:

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

へようこそ!Stackoverflow Raymondへようこそ!ここでお会いできてうれしいです。私はよく-cオプションを認識しており、あなたのソリューションは最初のケースでOKです。それにもかかわらず、2番目のケースでは、同じコードでスタンドアロンのPythonスクリプトを呼び出さなければならないので、最初のケースでも一時スクリプトを使用してコードの重複を避けることができると思いました。 –

+0

一時スクリプトは素晴らしいです。 –

関連する問題