I converted the py script to exe using pyinstaller but when I try to run the exe I got this. How can I fix it?
You have to tell pyinstaller what modules it need to bundle in the program, so, in your case, you have to pass as hidden import this: pkg_resources
In the .spec file that generated after running the pyinstaller, you will find an option like this:
hiddenimports=[]
Put it like this:
hiddenimports=["pkg_resources"]
Then, run pyinstaller like this:
pyinstaller app.spec
Or, if you still want to run it trought the .py, pass it as parameter like
pyinstaller app.py --hidden-import pkg_resources
Normally, if your program its more than a "Hello world", this is going to be a few more modules, you just need to add them all to hiddenimports as a list until there is no more ModuleNotFoundError.
Remember to NOT run pyinstaller to the .py file if you modified the .spec file, as it will create a new one and overwrite it, ignoring the previous.
You have to execute pyinstaller with the .spec file
Answer provided by Sebastian which is passing the module to hidden import do solve the problem too. I happen to found another solution while waiting for the answers, just have to reinstall pyinstaller which I pip install pyinstaller initially
To uninstall the pyinstaller:
pip uninstall pyinstaller
To install pyinstaller again:
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
Related
I'm somewhat new to python, so it would be nice if this was explained simply, if you can.
I'm trying to make a py file (main.py) into a exe file. I've followed this tutorial (https://www.geeksforgeeks.org/convert-python-script-to-exe-file/) as well as a youtube one, which did the same thing. My project has several modules. The conversion works, but if I try to open it, it tells me: (ModuleNotFoundError: No module named 'requests'). I would assume it does this for the rest of them if it gets the chance to. I don't know how to fix this and it would be nice if I could have a exe file so I could share programs with friends that don't have python installed. (I'm using pyinstaller for the process, same as the tutorial)
requests is a 3rd party library. I assume when you created the .exe, you did not "assembled" together your script with its dependencies.
According to the linked tutorial, you should check the pyinstaller's config and try to give pyinstaller the missing packages and somehow compile it with them, e.g.
For any missing package:
pyinstaller --hidden-import 'package_name' --onefile 'filename.py'
Just install requests:
pip install requests
Then you'll be able to bundle the script into an exe, provided there aren't any other missing dependencies.
According to your question either the requests module isn't installed or pyinstaller can't find the module!
If module isn't Installed then open up your terminal/Command Prompt and execute the follow command:
pip install requests
But if module is installed then what you can do is manually specify Pyinstaller which module to include also
so to compile it to exe use this command
pyinstaller --hidden-import 'requests' --onefile 'main.py'
Here the option --hidden-import means it will import the module requests also and --onefile means it you will get a single Exe file which will contain everything
Note! if you use --onefile this can effect your time taken to start your program!
Thanks
Edit:
According to the tutorial it shows you to open "PowerShell" but I recommend you to use "CMD" for this purpose!
I just finished working on a PyCharm project (using python 3.7), which uses libraries downloaded using PyCharm's built in functionality. I just want to send this project to my boss so that he can simply run it by clicking on it, without having to go through downloading PyCharm and downloading the applicable packages. Please tell me that there is a way to do this. Thank you
if you want to convert your python project to a program exucutable in windows you can use the library pyinstaller.
install the library:
pip install pyinstaller
then in the path of the project,open a command console and type the following commands.
if you want the executable in one file(slower start):
pyinstaller.exe --onefile --windowed --icon="your_icon_file".ico "your_python_script".py
if you want the executable in one folder(faster start):
pyinstaller.exe --onedir --windowed --icon="your_icon_file".ico "your_python_script".py
this wil create a folder call "dist" where your .exe will be,
pd: whith the --onedir command you will see all your dependencies in that folder, if you have a simple script you can use the first option.
Link to the pyinstaller documentation:https://pypi.org/project/pyinstaller/
You could solve this issue by converting the .py file to an executable file or .exe file....
Hopefully it helped..thx
A simple way to that would be sharing it on github with a requirements.txt file containing all the dependencies which then can be installed using python -m pip install -r requirements.txt.
Or You can use pyinstaller --onefile <yourfile.py> to create a .exe file which could be run without any requirements on windows.
So for example, I have a file called turn.py, how can I use turn.py to turn another file called main.py into main.exe as soon as turn.py is executed?
Please answer as it will really really help me.
Thanks in advance
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller test.py
This will generate the bundle in a subdirectory called dist.
reference this: pyinstaller
[edit]
you can also put this command in your code, this is a demo:
run turn.py, you will get a folder called dist and main.exe is there:
So I have a program and whenever I start it from the command line it works totally fine. What I need now is an Executable and therefor I tried to do it with pyinstaller, which analyzes normally automatically which modules have been imported and it works fine with all of the modules except for autoit. The way I import it looks like that:
import autoit
So I tried to make an executable by following command:
pyinstaller --onefile ./rocketupload.py
Which gave me this Error (Excuse me, that I have to make a screenshot, but the window was open for a second and closed immediately afterwards, so I was not able to copy and paste it here):
I was able to create a functioning exe by copying the autoit dll to the path mentioned in the Error, but that is just a temporary solution, since I want the executable to be running not only on my PC.
I've also tried this one without succes:
pyinstaller --hidden-import=autoit --onefile --paths c:\users\semjo\appdata\local\programs\python\python37\lib\site-packages\autoit\lib .\rocketupload.py
So the problem here is that the autoit module does not get copied from pyinstaller so It cannot run the executable as inteded. But I dont know how to solve it, so that the exe can run as intended. Hope you can help me here, tried to find a solution for hours now...
I had also encountered the same problem and i solved it by implementing the follows:-
Re-installed the pyinstaller module using the latest installer available in github i.e. pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
Re-created the executable package by running the command i.e. pyinstaller --hidden-import=selenium --hidden-import=autoit your [python_file.py]
Copied the installed module i.e. autoit's folder from my PC's directory (C:\Users\\AppData\Local\Programs\Python\Python37\Lib\site-packages) and pasted it inside the [python_file] folder in dist folder which was generated by pyinstaller.
To test the solution, i re-run the generated .exe file in command prompt. Hope this help.
hiddenimports = [ "autoit.init", "autoit.autoit", "autoit.control",
"autoit.process", "autoit.win" ]
datas = [
[
"C:\\Python27\\lib\\site-packages\\autoit\\lib\\AutoItX3_x64.dll",
"autoit\\lib"
]
]
https://raveendran.wordpress.com/2012/06/15/how-to-installregister-autoitx3-dll/
Do this two process your issue will resolve. It resolved me.
How I transform my program into an executable (.exe)? It has 3 .py files and images.
Made with tkinter and python 3.7, I saw that people used py2exe or pyinstaller but I don't know how to do that.
I use py2exe but options like cx_freeze and pyinstaller are also out there. What goes for mac i beleive there exist a module called py2app ;)
to use py2exe you must first have python 3.4 since it hasnt't recently been updated. After that you need to make a file in which you write
from distutils.core import setup
import py2exe
setup(windows=["yourfile"])
then you need to run the file with the following command - i called the file setup.txt here.
py -3.4 setup.txt install
remember to write .py if you used a python file and then you just need one more step
py -3.4 setup.txt py2exe
and ofcause remeber you need to have py2exe installed. If you haven't before you do anything write in to the comandoprompt
pip install py2exe
The exe will then be found i the dist folder.
imported libraries are always triggy though i think i remember that last time i had to convert tkinter to .exe i had to use cx_freeze.