0
1

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プログラムのExe化:cx_Freezeの使用

Last updated at Posted at 2024-10-10

概要

  • cx_Freezeのモジュールを用いて、Pythonプログラムをexe化する方法を記載
  • cx_Freezeの詳細は以下を参照のこと
  • CUI(コマンドプロンプト、ターミナルなど)操作でpythonを実行できる前提で記載

インストール

  • cx_Freezeは、pip、condaどちらでもインストール可能

    pip install --upgrade cx_Freeze
    
    conda install conda-forge::cx_freeze
    

実行

手順

1. exe化したいpythonファイルの場所までコマンドプロンプトで移動

2. 以下のコマンドを実行

cxfreeze-quickstart

3. 指示に従い、回答する

  1. Project name: 任意のプロジェクト名を入力
  2. Version: バージョンを入力
  3. Description: 本プロジェクトの説明を記載
  4. Python file to make executable from: 実行ファイルを作成するpythonファイルを記載
  5. Executable file name: 実行ファイル名
  6. Console, Gui, Serviceを選択:C/G/S
  7. 保存するセットアップファイル名を記載:ファイル名
  8. build(exe化)このままするか?:y/n

4. 必要に応じて、setup.pyの中身を確認する

  • build_optionsは、ある程度自動的に認識してくれるが、exe化後にパッケージやモジュールがないなどのエラーが表示される場合は、'packages', 'includes'に明示する。

  • 'include_files'は、exe化したフォルダにコピーするファイル

  • その他の詳細は以下

  • setup.pyファイルの例

    from cx_Freeze import setup, Executable
    
    # Dependencies are automatically detected, but it might need
    # fine tuning.
    build_options = {
        'packages': ['pyautogui', 'pygetwindow', 'pynput'], 
        'excludes': [],
        'include_files': ['setting.txt']
        }
    
    base = 'console'
    
    executables = [
        Executable('hoge.py', base=base)
    ]
    
    setup(name='hoge',
          version = '1.0',
          description = '',
          options = {'build_exe': build_options},
          executables = executables)        
    

5. exe化(build)

  • exe+dlls(portableなexeファイル/フォルダ作成)の場合は、以下を実行すれば、buildのフォルダ下層にexeファイルが作成される

    python setup.py build
    
  • msi(Windowsのインストーラ作成)の場合は、以下を実行すれば、distフォルダ内にmsiファイルが作成される

    python setup.py bdist_msi
    

その他

exe化する他のモジュールは以下など。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?