pyinstaller --onefile is not including all necessary dependencies - python

I am trying to compile a python script into an executable using pyinstaller --onefile. However, one necessary package, geemap, doesn't seem to be included, despite being an import in the script.
Is it possible to force-add custom packages?
Every time I run the .pyc file on a clean PC, it says:
ModuleNotFoundError: No Module named 'geemap'

You could try:
pyinstaller --onefile --hidden-import=geemap
This should force-add the package.

Related

Including External Python Packages with Python Executable

I have made my own Executable from a python script which I want to run on a computer that does not have Python installed on it. My only problem is there are packages I have included which are not default python packages (e.g. pynput). Otherwise, the Executable would run fine without Python installed. Is there a way to include Python packages with my executable? Can I put the packages in the same folder as the script and import them from there? If so, would I then be able to transport them along with my Executable? I have already googled a solution but I cannot find anything on it.
use following method :
pip install pyinstaller
pyinstaller is a package for generating executable files for python and can be ran from command line or terminal
in the same directory of your pyhton file run following command
pyinstaller --onefile nameofyourfile.py
after its done if you check your folder there should be a dist folder
inside that you can find an executable to your python file which has no dependencies to other packages

Can't make a .py file into a .exe file

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!

Pyinstaller Failed to execute script pyi_rth_pkgres

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

How to make scripts excutable?

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.

Python, PyInstaller error: no module named "Encodings" and system codec missing

I am using Python 3.3.3 and I have been trying to build a .exe from a simple .py script.
My script looks like this:
import encodings
print('Test')
and executes correctly.
When I try to build it with PyInstaller with this command:
pyinstaller --onefile Testmodul.py
and try to open my .exe it shows up with this error:
Fatal Python error: Py_Initialize: unable to load the file system codec, ImportError: No module named 'encodings'
I already tried importing the 'encodings' module in my testscript but it is still not working, I have also tried py2exe and it is also not working at all.
Is there anything I do wrong? Do I have to setup something in my PATH? the correct path to "C:\Python33" is included in there already.
EDIT: To everyone with this problem: I gave up, and after a fresh install of windows and python and all the other stuff, I tried it again, the same way as before and it worked without a problem.. It is worth a try if you are really desperate!
This is probably because pyinstaller did not include the module in the first place. Try one of the following solutions.
1) Specify the path to your module during compilation:
pyinstaller --onefile --paths=/path/to/module Testscript.py
2) Specify the path from the .spec file:
run this command first(in pyinstaller's directory):
python Makespec.py --onefile /path/to/yourscript.py
now you have the .spec file. open it in your text editor, and add the path to your modules to the pathex.
pathex=['C:\\path\\to\\module']
then, build your program:
python Build.py /path/to/yourscript.spec
3) Use hidden imports:
pyinstaller --onefile --hidden-import=modulename Testscript.py
you can also specify hidden-import in the .spec file.
Add hook file with name hook-encodings.py to C:\Python\Lib\site-packages\PyInstaller\hooks location and add following line of code to collect encodings module in hook file
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('encodings')
this may work,
this answer might help you Pyinstaller Error for Djnago project "ImportError: No module named 'django.contrib.admin.apps'"
Which windows version are you using (7 or 10) ?
This issue seems to be relative to user privilege ... and assuming it is similar to this issue, you may first try to run your exe file with administrator privilege, and if it is failling again, try to run "Pyinstaller" from a cmd.exe running with administrator right.

Categories