need help using pyinstaller - python

i am trying to program a simple script and i would like to know if anyone has the answer to this question
when i have a module for example WMI from 'pip install wmi' in the form as 'import wmi' in my code, how do i get the pyinstaller module to compile the wmi module with the exe file
i have tried importing from the source code in a folder example 'from wmi import wmi' but i got no luck when launching the exe file only in the raw python file, also just to note when i compile the script i do the command 'pyinstaller vb.py --onefile'

remove --onefile since wmi couns as a file (or multiple if its a package)

Related

Python Pyinstaller build fails using win32com package [duplicate]

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)
I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)
I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

I have problem with Pyinstaller in python

I have a question if I use Pyinstaller to convert python file to exe will convert the modules with or not? , because i have python file with a lot of modules and i want to convert it how i can do it and avoiding this issue,
Thank u.
import requests
error:
ImportError: No module named requests
.
After discussion in comments basically the error is occurring when you try to open the generated file in another computer, however your aren't using any virtual environment so you can install the requirements and try to rebuild again but rather you want a stanalone exe file.
For that use :
pyinstaller --onefile your-script.py
# or pyinstaller -F your-script.py
## this should generate a stand alone executable file located in the dist folder.
About your concerns on how pyinstaller works
Does pyinstaller make copies of modules when building ?
The answer is simply : yes , as mentioned in the docs here PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.
However, the variations of Python and third-party libraries are endless and unpredictable, if something goes wrong you can learn how to fix those issues by reading this page on the docs here
What to generate ?
you can read more here
Create a one-folder bundle containing an executable (default), -D, --onedir
Create a one-file bundled executable. you need to use -F ore --onefile
Finally
I highly encourage you to use separate virtual environment for each project.

PyInstaller: using data files and subprocess

I have successfully bundled several of my python scripts into exe files using pyinstaller. However, I have hit a problem with a different python script that uses local data files. Using this question and answer PyInstaller 2.0 bundle file as --onefile I have got the script using my local files however I get an error.
I have a master python script called "translate.py" in it I have several subprocesses that call a different python script call "loader.py" which takes a variety of arguments. So my normal subprocess looks like this
python.exe loader.py loader.config src_dir=data out_dir=sql tmp_dir=temp
In my pyinstaller version I have used the answer from the above question to have the following subprocess call
python.exe C:\Users\AppData\Local\Temp\_MEI70922\loader.py C:\Users\AppData\Local\Temp\_MEI70922\loader.config src_dir=dat
a out_dir=sql tmp_dir=temp
However this fails to run the subprocess and I get the following error
no module named site
So I am wondering if pyinstaller is not including all the modules I may need?
I have tweaked the spec file to analyse all the python scripts like this
a = Analysis(['c:\\temp\\translate.py','c:\\temp\\loader.py','c:\\temp\\prep.py','c:\\temp\\prep2.py'],
pathex=['C:\\Temp\\pyinstaller-2.0\\pyinstaller-2.0'],
hiddenimports=[],
hookspath=None)
But again I get the no module named site.
I know my code could be better but I am stuck as someone else wrote the loader.py and I wrote the translate.py and now I need to bundle it all into a simple exe file.
thanks for anyone's help

No module named pyopencl (Py2exe)

I am having some trouble with the PyOpenCL module when trying to make a .exe from Py2Exe.
Py2Exe makes the .exe as it should do (No "ImportError: No module named pyopencl" here), but when I am running the .exe it says no module named pyopencl.
I am trying to make a .exe of the Phoenix Miner.
My setup.py:
from distutils.core import setup
import py2exe, sys, os, pyopencl
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1,
"includes":["pyopencl","twisted",
"zope","QueueReader",
"numpy"]}},
console=[{'script' : 'phoenix.py'}],
data_files=["C:\\Users\\Nicklas\\Desktop\\Phoenix-Miner\\kernels\\poclbm\\kernel.cl"],
zipfile = None,
)
I found someone who had the same problem as me http://bytes.com/topic/python/answers/848048-py2exe-module-error but with no solution.
UPDATE:
I found what was causing this error. In pyopencl __init__ there is a function called _find_pyopencl_include_path, it is quite self explanatory what it does. To make a long story short: the imp module fails to find the pyopencl module. To fix this I commented out that line and set pathname to the path to pyopencls include directory. Probably not a good fix. But it was an easy fix.
I found what was causing this error. In pyopencl __init__ there is a function called _find_pyopencl_include_path, it is quite self explanatory what it does. To make a long story short: the imp module fails to find the pyopencl module. To fix this I commented out that line and set pathname to the path to pyopencls include directory. Probably not a good fix. But it was an easy fix.
There is also easiest way to make python script executable by using auto-py-to-exe module, it can be installed using pip install auto-py-to-exe and after installing write auto-py-to-exe in python command line terminal a window will appear, choose the script location and choose OneFile instead of OneDirectory, for reference here I am pasting the auto-py-to-exe window screenshot
After converting 'open output folder' option will appear where you'll get your .exe file of python script. And this .exe file run on any system without using python.

Categories