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.
Related
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 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.
So, I made a software using python, the GUI is made with pyqt5, after wrapping everything with pyinstaller package i have an executable called Main.exe. When i execute the file normally it works, but i need to execute it with python code. i found this code which is usefull:
import os
os.startfile(r"C:\Users\Desktop\Project\dist\Main\Main.exe")
with other executable it works fine but whenever i try it with my executable 'Main.exe' it doesn't work. where is the problem coming from?
Pyinstaller recently stopped importing a module that it previously had no problem with so I set up a simple test case to isolate the problem.
I created a script (pt.py) as follows:
import py_script1
import py_script
print('hello world')
py_script.py and py_script1.py are identical and located in the same directory as pt.py. They both import os, random and math and have some functions in them.
When running pyinstaller with default settings it compiles the .exe file but when I try to run it, I get "ImportError: No module named py_script". Checking the warnings text file shows that it was never compiled into the .exe file.
Changing the order of the imports doesn't help - I get the same error. I have also tried to uninstall and reinstall pyinstaller but that has not helped
How is this even possible - the two imported scripts are identical and are located in the same place!
More importantly how do I fix this problem?
PyInstaller 3.3.1
Python 3.3.5
Windows 10
I solved it. Despite the misleading exception description I isolated the problem as the following line in the imported file:
SB_CONSTANT = 0.00000005670373 #Stefan–Boltzmann constant (W/m2/K4)
For some reason this number broke pyinstaller. I changed it to:
SB_CONSTANT = 5.6704e-8
...and the module imported fine.
I'm trying to create an executable from a .py file, and if I do things exactly as the py2exe tutorial says, it works. I put setup(console=["thingyIdLikeToDo.py"]) in my setup.py file, then type python setup.py py2exe into my console, and it works.
BUT. This means that whenever I want to make another python file into an executable, I have to go open and edit setup.py. And I'd rather do something else:
from distutils.core import setup
from sys import argv
import py2exe
setup(console=[argv[1]])
##This was originally setup(console=["MyTargetFile.py"]) and it DOES work that way
And then type in python setup.py MyTargetFile.py py2exe. On account of it being invalid command name 'MyTargetFile.py'
I've also tried it by changing the order, making it python setup.py py2exe MyTargetFile.py and changing the argv[1] to argv[2]. I get the exact same error message.
I mean, I DO have a functioning way to make my .py files into .exe files, but I'm really annoyed that something that seems like it ought to be such a simple change is't working. What am I missing here?
You're running into trouble because setup is using sys.argv. If you change your call to setup(console = [sys.argv.pop(1)] I believe you will stop stepping on distutils' toes and things should go smoothly.