Get the directory where the EXE file is located after python is packaged

created at 07-28-2021 views: 5

Get the directory where the EXE file is located after python is packaged

After the exe is packaged, the directory obtained using the following code is incorrect

import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

Solution

Get the directory where the exe is located

import os
import sys

BASE_DIR = os.path.dirname(os.path.realpath(sys.executable))
dir_path = os.path.join(BASE_DIR, 'data')

Get the parameters of the exe file

import os
import sys

os.path.dirname(os.path.realpath(sys.argv[0]))
created at:07-28-2021
edited at: 07-28-2021: