0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonをダブルクリックで実行

Last updated at Posted at 2024-07-01

はじめに

Pythonで作成したアプリケーションをexeにするほどでもないな、というときに比較的簡単にダブルクリックでコード実行ができるようにする方法を紹介する。

注意点

  • Windowsを想定
  • 他の人のPCにもpython.exeがインストールされていること

環境

Windows10
winpython 3.10.90

結論

batファイルからpython.exeを呼び出し、引数としてPythonスクリプトを渡してしまう。
そうすればあたかも「exeを開いたような感じ」になる。

@echo off
setlocal

title windowtitle
set PythonEXE="python.exeのフルパス"
set Program="Pythonコードのフルパス"

call %PythonEXE% %Program%

応用

基本形をベースによく使うものを記載する。

D&DでPythonにファイルパスを渡す

callの文に%*をつける。

call %PythonEXE% %Program% %*

Python側では次のように記載するとD&Dされたファイルのフルパスをリストで取得できる。

import sys
sys.argv[1:]

カレントディレクトリを変更する

相対パスなどでファイルを扱う際にカレントディレクトリを変更してから実行すると良い

cd /d "カレントにしたいディレクトリ"
cd /d %~dp0

画面サイズを変更する

画面がデカくて邪魔なとき用

mode con: cols=50 lines=17

ユーザーの指示があるまで処理を停止

barファイルの最後に書いておくと処理が終わったのかエラーだったのかがわかるので書くことを推奨。なにかキーを押すとpause文が終了する。

pause

winpythonのコマンドプロンプトを起動する

call "絶対パス\WPy64-****\scripts\env_for_icons.bat" %*

ユーザー入力を受け付ける

処理終了後、普段のコマンドプロンプトのようにユーザーからの入力を受け付けてくれる。

cmd /k

画面を緑にする

見た目がかっこよくなるのでたまに使ってる。

color 0a
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?