LoginSignup
1
0

More than 1 year has passed since last update.

Pyinstaller で実行ファイル化した場合のプログラムの親フォルダ取得

Last updated at Posted at 2021-10-22

.pyおよび.exeどちらでも親フォルダを取得する

from pathlib import Path
def get_source_path():
    if getattr(sys, 'frozen', False):
        # The application is frozen
        exe_path  = Path(sys.executable).resolve()
        main_path = exe_path.parent
        dist_path = main_path.parent
        src_path  = dist_path.parent
    else:
        # The application is not frozen
        # Change this bit to match where you store your data files:
        py_path  = Path(__file__).resolve()
        src_path = py_path.parent

    return src_path

参照:https://qiita.com/Authns/items/f3cf6e9f27fc0b5632b3

src/
 └ main.py
上記のフォルダ構成のpythonプログラムに対し、Pyinstallerを用い実行ファイル化すると
以下のフォルダ構成となります
src/
 ├ dist/
 │ └ main/
 │   └ main.exe
 └ main.py

get_source_pathにより、.py、.exeどちらを実行してもsrcフォルダの絶対パスを得ることが出来ます。

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