[ I used translator ]
I want to get the name of the exe file made from Pyinstaller.
I tried os.path.basename(__file__) , but i returned python file's name(before using Pyinstaller).
I am sorry for my English and thank you
os.path.basename(sys.executable)
Using pyinstaller to create .exe file, it will create the .exe file with the source code file name(ex- demo.py will produce demo.exe file at the output destination) if using --onefile and if you're using --onedir the .exe file will be stored with the same name as in --onefile but inside a folder with the same name, so the directory will be as follows: output_destination/demo/demo.exe
Related
I have a python file that I am trying to create an executable out of using Pyinstaller on my Mac. This python file imports several different python files. When I run the unix executable that was generated, I get this error:
File "main/__init__.py", line 4, in <module>
ModuleNotFoundError: No module named 'game'
Line 4 reads:
from game.scripts.gui import creator
The command I used to create the executable:
pyinstaller __init__.py --onefile --clean --windowed
The directory:
__init__.py
game
scripts
gui
creator.py
Any ideas on how I could fix this? Thanks
The subdirs are not included by creating an *.exe, so the creator.py is not found inside your *.exe. To avoid that, you have to include the extra files/folders by specifying them. This can be done by a *.spec file
By calling pyinstaller with your *.py file it will create a default *.spec file which you can edit and use next time to create your *.exe. Every option you used when calling
pyinstaller __init__.py --onefile --clean --windowed
is configured here so calling
pyinstaller *.spec
the next time gives the same result.
Edit this in your spec-file to fit your needs by copying single files or even whole folders including their content into the *.exe:
a = Analysis(['your.py'],
pathex=['.'],
binaries=[],
datas=[('some.dll', '.'),
('configurationfile.ini', '.'),
('data.xlsx', '.'),
('../../anotherfile.pdf', '.')
],
....some lines cut ....
a.datas += Tree('./thisfoldershouldbecopied', prefix='foldernameinexe')
More infos to that are found in the docs of pyinstaller regarding spec-files and including data files
https://pyinstaller.readthedocs.io/en/stable/spec-files.html
and for example in this post here:
Pyinstaller adding data files
my Python project works in editor. I get .exe output from Python (.py) program. The .exe file could not understand the path of the files.
import os
a = os.getcwd() + "\\Logs\\" # Find the log directory
# Read the text in the Paths.txt file inside the logs directory
b = open (a + "Paths.txt", "r").read()
When the code is executed in the editor, the text inside the file is read and the file path is understood. But when executing the .exe file, the program can not detect the path of the log file.
Log file path at runtime (in editor or .py): currect and understood
'C:\\Users\\mohammad\\Desktop\\projects\\Logs\\Paths.txt'
Log file path at runtime (in .exe file): not currect
'C:\\Users\\mohammad\\Desktop\\projects\\dist\\Logs\\Paths.txt'
I tried some methods such as short route but it did not work.
Also, due to my problems, I output Python file as follows:
pyinstaller.exe --onefile --add-binary "C:\\Users\\mohammad\\Desktop\\projects\\pythoncom310.dll\\;." Start.py
And i'm using version 3.9 of Python and the VScode editor. Thanks for your time
I am using Ubuntu as a host system,.compiled for 32bit manually chosen the bootloader, with respect to that create bundle Python file. And copy into my arm target board.
The error which I am facing is not able to execute the binary file
In my arm board.
I cannot able to bundle .my .csv files with executable using --add-data. While running the executable, it searches my CSV file in the current folder, it shows error as file not found error.
how to add multiple files (CSV and INI) files with my executable.
How to fix this issue.
Regards
Rajalakshmi
For adding data files you need to First, provide your data files with --add-data flag. Then because your data would be extracted on a temp directory, you need to set its address for your app. In below example, I'm addressing all CSV files from resource_path function which would return the relative path for each file.
I assume that you put all your files in data directory beside your app.
app.py:
import os
import sys
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
if __name__ == "__main__":
csv_files = ["data/a.csv", "data/b.csv", "data/c.csv"]
print("Reading CSV files from data directory")
for csv_file in csv_files:
with open(resource_path(csv_file), "r") as f:
print(csv_files, ":", f.read())
print("Done!")
You can then generate your executable with:
pyinstaller app.py -F --add-data "./data/*;data/"
How do I package files into an EXE file that is generated via PyInstaller --onedir?
I see, that I can add the file with
a.zipped_data+=["version.json", "version.json", "DATA]
PYZ(a.pure, a.zipped_data)
pyi-archive_viewer shows the file in PYZ-00.pyz
But
pkg_util.find_loader("testmod").get_data("version.json")
does not find the file.
NB: I can't use --onefile mode, because it would need to unpack several 100 MB at each program start. And I want to tie several files (not only the version file) with the executable.
The name passed to get_data was wrong.
For an Windows executable c:\test\testpgm.exe the correct name would be c:\test\version.json
In Linux the executable might be /usr/local/bin/test/testpgm and the correct name would be /usr/local/test/version.json
I have a sample.ear file and I want to replace a particular file inside sample.ear.
consider ear file sample.ear which content com1/test1/file1.sh and com2/file2.sh
here I want to replace file1.sh with updated file file1.sh using unix.
I am using jar command but i am not able to replace the file in subdirectories
Thanks
Use:
jar -xf sample.ear com1/test1/file1.sh
to extract file.sh. Once you are done modifying this com1/test1/file1.sh file use:
jar -uf sample.ear com1/test1/file1.sh
to update the archived ear with the modified file.
Use jar --help for detailed help.