Pyinstaller module issues - pyinstaller

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

Related

PyInstaller: FileNotFoundError when trying to run the distribution file

I'm helping out a friend who needs to transform some data in csvs, so I made a little python program called "csv_converter.py"- it takes a csv input file and csv output file and everything's peachy.
I want to be able to give them a file they can run on their machine (without necessarily needing python and pandas) so I'm trying PyInstaller.
In the directory with my python program I run: pyinstaller csv_converter.py
According to the docs, it'll make a 'dist' folder where "you find the bundled app you distribute to your users." It sounds like the key file to run will be: dist/csv_converter/csv_converter (clarification source: here), and then I can either send the whole csv_converter folder, or just the csv_converter file if I run the Pyinstaller command with the --onefile argument.
After running PyInstaller I see these directories created, but when I navigate to dist/csv_converter/ and try to run that important csv_converter file (before trying to send anything), it's giving the following error:
Dianes-MacBook-Pro:csv_converter dkaplan$ ./csv_converter -h
Traceback (most recent call last):
File "site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py", line 30, in <module>
FileNotFoundError: Tcl data directory "/Users/dkaplan/PycharmProjects/chris_csv_converter/src/dist/csv_converter/tcl" not found.
[36637] Failed to execute script pyi_rth__tkinter
So close, but so far! Has anyone else had this issue or know a workaround?
I heard back from the kind folks at PyInstaller, and this is a common one they see with tinter. I'm not sure where it was being pulled in from, but the workaround was to exclude it with this argument: --exclude-module=tkinter
I had chosen to use the --onefile argument (slower, but means I can send just one file), so the full command was: pyinstaller --onefile --exclude-module=tkinter csv_converter.py
Then:
I could go to the dist folder and do a test run with: ./csv_converter -h
then I send that csv_converter file to my friend and he'll be able to run it the same way (without needing python)

Pyinstaller for arm 32bit compilation

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/"

Creating a pyinstaller executable that uses virtualenv imported modules

So, the title basically covers my question. I've created a project using virtualenv, e.g. I have to
source ./env/bin/activate
to run my script.
When I try creating an executable using:
pyinstaller --onefile <myscript.py>
None of the virtualenv packages are included; just the ones that are installed globally. I have a requirements.txt file that contains all of the modules I need. Is there a way to have pyinstaller point to that for the needed modules, or is there another way?
As Valentino pointed out by looking at How can I create the minimum size executable with pyinstaller?
You have to run PyIntaller from inside the virtual environment:
(venv_test) D:\testenv>pyinstaller
How to solve the not importing modules from the virtual environment
The virtual environment saves modules in a different directory than the global module directory. If you are using Windows like me, you can find the modules directory here:
C:\Users\<your username>\.virtualenvs\<your project name>\Lib\site-packages
When you find your virtualenv directory, run this command instead of this simple command(pyinstaller <script>.py):
pyinstaller --paths "C:\Users\<your username>\.virtualenvs\<your project name>\Lib\site-packages" --hidden-import <module name that should be import> <your script name>.py
To export just one file you can add this: -F or --onefile
As many modules as you can add to be imported by adding more --hidden-import flags and module name
Flag description
--paths: The pyinstaller will search for imports here
--hidden-import: Which modules should be imported by pyinstaller from the path

Is it possible to write python code in spec file which is generated by pyinstaller?

There are some commands that has to be executed with spec file. Is there any possibility to include commands in spec file.
I have to execute the following statement:
rm -rf build dist
I need to include in spec file.
When I run
pyinstaller --clean abc.spec
it has to delete build folder and dist folder
From the pyinstaller documentation:
The spec file tells PyInstaller how to process your script. It encodes the script names and most of the options you give to the pyinstaller command. The spec file is actually executable Python code. PyInstaller builds the app by executing the contents of the spec file.
So, you can include python code in the spec file to perform the operations you want. But it will need to be python code, not terminal commands as you have.

py2exe PyQt5 "ImportError: No module named 'Qt'"

im trying to use py2exe (0.9.2.0) to convert a python script into an executable.
I've failed so far because py2exe does not find the module Qt:
C:\Users\Tobias\eclipse\workspace\pydevTest>python setup.py py2exe
running py2exe
5 missing Modules
------------------
? Qt imported from __SCRIPT__
? WizardPage imported from __SCRIPT__
? readline imported from cmd, code, pdb
? win32api imported from platform
? win32con imported from platform
Building 'dist\Test.exe'.
Building shared code archive 'dist\library.zip'.
Copy c:\windows\system32\python34.dll to dist
Copy C:\Python34\DLLs\_hashlib.pyd to dist\_hashlib.pyd
Copy C:\Python34\lib\site-packages\PyQt5\QtGui.pyd to dist\PyQt5.QtGui.pyd
Copy C:\Python34\lib\site-packages\PyQt5\QtCore.pyd to dist\PyQt5.QtCore.pyd
Copy C:\Python34\DLLs\unicodedata.pyd to dist\unicodedata.pyd
Copy C:\Python34\DLLs\_ssl.pyd to dist\_ssl.pyd
Copy C:\Python34\DLLs\_elementtree.pyd to dist\_elementtree.pyd
Copy C:\Python34\DLLs\select.pyd to dist\select.pyd
Copy C:\Python34\lib\site-packages\sip.pyd to dist\sip.pyd
Copy C:\Python34\lib\site-packages\PyQt5\QtWidgets.pyd to dist\PyQt5.QtWidgets.pyd
Copy C:\Python34\DLLs\pyexpat.pyd to dist\pyexpat.pyd
Copy C:\Python34\DLLs\_lzma.pyd to dist\_lzma.pyd
Copy C:\Python34\DLLs\_socket.pyd to dist\_socket.pyd
Copy C:\Python34\DLLs\_bz2.pyd to dist\_bz2.pyd
Copy C:\Python34\DLLs\_ctypes.pyd to dist\_ctypes.pyd
Copy DLL C:\Python34\lib\site-packages\PyQt5\Qt5Core.dll to dist\
Copy DLL C:\Python34\lib\site-packages\PyQt5\icudt53.dll to dist\
Copy DLL C:\Python34\lib\site-packages\PyQt5\icuuc53.dll to dist\
Copy DLL C:\Python34\lib\site-packages\PyQt5\icuin53.dll to dist\
Copy DLL C:\Python34\lib\site-packages\PyQt5\Qt5Gui.dll to dist\
Copy DLL C:\Python34\lib\site-packages\PyQt5\Qt5Widgets.dll to dist\
My setup.py looks as follows:
import py2exe
from distutils.core import setup
setup(windows=["./src/Test.py"], options={"py2exe" : {"includes" : ["sip", "PyQt5.QtGui","PyQt5.QtWidgets","PyQt5.QtCore","PyQt5.QtCore"]}})
The script is rather simple. After getting rid of the first error, I might also help with the four other missing modules...
Thanks a lot!
you need to add the following dlls:
C:\Windows\SYSTEM32\msvcp100.dll
C:\Windows\SYSTEM32\msvcr100.dll
C:\Python34\Lib\site-packages\PyQt4\plugins\platforms\qwindows.dll
somethings like this:
data_files = (
('', glob(r'C:\Windows\SYSTEM32\msvcp100.dll')),
('', glob(r'C:\Windows\SYSTEM32\msvcr100.dll')),
('platforms', glob(r'C:\Python34\Lib\site-packages\PyQt4\plugins\platforms\qwindows.dll')),
),
Sorry, the previous answer did not work for me: https://stackoverflow.com/a/37622355/7426109
(and I have different versions)
What did work on the other hand is to add this to your path: "C:\Program Files (x86)\Python38-32\Lib\site-packages\PyQt5\Qt\bin".
My temporary fix:
You copy the "Qt" ("C:\Program Files (x86)\Python38-32\Lib\site-packages\PyQt5\Qt") folder to "dist", and use a batch script to add ".dist\Qt\bin" to PATH in the current CMD window. (of course, this is not perfect, but at least can be executed on another machine)
Info
Windows 10 x64, Python 3.8.6 x32, PyQt5==5.15.1, py2exe==0.10.0.2

Resources