How to get the directory where the EXE built with Pyinstaller exists

Main subject

When you ask a Google teacher, in many cases

python


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

Or something

python


import os, sys
os.path.dirname(sys.argv[0])

It will be returned after changing to. It looks good at first glance, but ** it's not good at all. ** ** What's wrong is that if you just run it normally, it's okay, but if you run it through the ** path, it's strange. ** ** To fix this

python


import os, sys
if getattr(sys, 'frozen', False):
    program_directory = os.path.dirname(os.path.abspath(sys.executable))
else:
    program_directory = os.path.dirname(os.path.abspath(__file__))

I will give it as. First line: Determines if it was built with Pyinstaller or not. Second line: Runs when building with Pyinstaller. Returns the path of the EXE built by ** sys.executable **. After that, convert it to a directory path with the os module. Third line: the meaning as it is Fourth line: Executed when not building with Pyinstaller. Familiar code.

~~ I was addicted to it for about 2 hours. ~~

Reference (plagiarism source)

Determining application path in a Python EXE generated by pyInstaller - stackoverflow

Recommended Posts

How to get the directory where the EXE built with Pyinstaller exists
How to get a list of files in the same directory with python
How to return to the previous directory with the Bash cd command
How to get into the python development environment with Vagrant
[Python Kivy] How to create an exe file with pyinstaller
[Introduction to Python] How to get data with the listdir function
How to get the Python version
How to get started with Scrapy
How to get started with Python
How to get started with Django
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
How to get colored output to the console
EXE the application created with PyQt5 with PyInstaller
How to get started with laravel (Linux)
How to get the date and time difference in seconds with python
Convert the program that imports Google's client library to .exe with pyinstaller
The easiest way to get started with Django
How to try the friends-of-friends algorithm with pyfof
How to get the files in the [Python] folder
How to Learn Kaldi with the JUST Corpus
I want to get the path of the directory where the running file is stored.
How to get started with the 2020 Python project (windows wsl and mac standardization)
How to pass the path to the library built with pyenv and virtualenv in PyCharm
How to get the key on Amazon S3 with Boto 3, implementation example, notes
Note: How to get the last day of the month with python (added the first day of the month)
[Introduction to Python] How to get the index of data with a for statement
Get the location of the file where the exe is placed when the exe created by PyInstaller is executed
How to get the variable name itself in python
How to delete the specified string with the sed command! !! !!
How to get the number of digits in Python
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
How to create a submenu with the [Blender] plugin
[Reintroduction to python] How to import via the parent directory
How to get a logged-in user with Django's forms.py
[Python] How to specify the download location with youtube-dl
How to get started with Visual Studio Online ~ The end of the environment construction era ~
How to get more than 1000 data with SQLAlchemy + MySQLdb
How to use the exists clause in Django's queryset
Try to get the contents of Word with Golang
PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines
How to get mouse wheel verdict with Python curses
[Python] How to rewrite the table style with python-pptx [python-pptx]
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
I tried to simulate how the infection spreads with Python
[Ubuntu] How to delete the entire contents of a directory
[python] How to check if the Key exists in the dictionary
Memo to get the value on the html-javascript side with jupyter
Minimum knowledge to get started with the Python logging module
Get the package version to register with PyPI from Git
How to manipulate the DOM in an iframe with Selenium
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
How to run an app built with Python + py2app built with Anaconda
A story about how to deal with the CORS problem
[Python] Get the script execution directory with an absolute path
[Shell] How to get the remote default branch in Git
Get the source of the page to load infinitely with python.
How to update with SQLAlchemy?
How to cast with Theano