Normally PyInstaller works fine for me but i saw a problem using the python-module pycountry.
I tried this very simple code:
import pycountry
land="DE"
country = pycountry.countries.get (alpha_2=land)
print(country.name)
Compiled it with pyinstaller:
pyinstaller --onefile xyz.py
But i want to execute the compiled exe i get this error:
Traceback (most recent call last):
File "temp2.py", line 1, in <module>
import pycountry
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\pycountry\__init__.py", line 12, in <module>
File "site-packages\pkg_resources\__init__.py", line 481, in get_distribution
File "site-packages\pkg_resources\__init__.py", line 357, in get_provider
File "site-packages\pkg_resources\__init__.py", line 900, in require
File "site-packages\pkg_resources\__init__.py", line 786, in resolve
pkg_resources.DistributionNotFound: The 'pycountry' distribution was not found and is required by the application
[45548] Failed to execute script temp2
Is there any workaround i can get pycountry functionality to running with pyinstaller?
UPDATE:
found a workaround / solution for my problem-
use command <pyi-makespec --onefile temp2.py> to generate a temp2.spec file
change filename.spec => from PyInstaller.utils.hooks import copy_metadata (in the header) => in the a = Analysis(...) section change " datas = []," to <datas = copy_metadata("pycountry"),>
use pyinstaller to compile exe as above
Alternative: compile program before - change spec - an use command <pyInstaller --clean temp2.spec> – Rapid1898 just now Edit
Your update was really helpful to me. But I needed a variation in order for it to work. I would just order the ideas to show clearer the way it must be done for pycountry library to work properly.
Use command pyi-makespec --onefile name-of-your-file.py to generate a .spec file
with name name-of-your-file.spec
Open name-of-your-file.spec with the text editor of your preference.
Add to the top of the .spec file the following line from PyInstaller.utils.hooks import copy_metadata
Replace datas = [] for datas = copy_metadata("pycountry")
Then rebuild using the following command PyInstaller --clean name-of-your-file.spec
I hope it is helpful for anyone facing the same error.
You can read the documentation for using spec files.
And you can find the same process in this link.
Related
I have used pyinstaller many times before but I have never faced such issues
I am facing issues while converting .py to .exe using pyinstaller
What I have tried
I have installed pythonnet and opened the directory where pythonnet is stored but i could not find any dll file
I am using the following command
PyInstaller app.py --noconsole --icon=./images/Source.ico
entire Traceback
Traceback (most recent call last):
File "c:\Users\ADMIN.DESKTOP-USLQ9P3\Desktop\jarvis\setup.py", line 3, in <module>
PyInstaller.__main__.run([
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\__main__.py", line 114, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\building\build_main.py", line 725, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\building\build_main.py", line 672, in build
exec(code, spec_namespace)
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\Desktop\jarvis\app.spec", line 6, in <module>
a = Analysis(['app.py'],
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\building\build_main.py", line 242, in __init__
self.__postinit__()
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\building\datastruct.py", line 160, in __postinit__
self.assemble()
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\building\build_main.py", line 420, in assemble
self.graph.process_post_graph_hooks()
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\depend\analysis.py", line 367, in process_post_graph_hooks
module_hook.post_graph()
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\depend\imphook.py", line 447, in post_graph
self._load_hook_module()
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\depend\imphook.py", line 408, in _load_hook_module
self._hook_module = importlib_load_source(
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\PyInstaller\compat.py", line 598, in importlib_load_source
return mod_loader.load_module()
File "<frozen importlib._bootstrap_external>", line 462, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 962, in load_module
File "<frozen importlib._bootstrap_external>", line 787, in load_module
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\ADMIN.DESKTOP-USLQ9P3\AppData\Roaming\Python\Python38\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks\hook-clr.py", line 40, in <module>
raise Exception(pyruntime + ' not found')
Exception: Python.Runtime not found
I have also install pythonnet but it is still not working neither can I find the dll
This is not a direct answer to your question, but I have had a lot of issues with pyinstaller myself in the past year. At this point I have the process perfected that I almost never have issues anymore. The key for me has been:
Do not use virtual environments (venv). I use a separate older computer specifically for pyinstaller. The computer only has basic python, pycharm and the libraries I need for my build installed to site packages. I install everything globally. I remove libraries I do not need.
If a library causes problems like you describe, try installing an older versio of said library.
To follow up:
Using pycharm does not work or pycharm in general does not work?
What I would recommend you do is what I do on a fresh computer:
Make sure you know which libraries your project requires. You could generate a requirements file: Automatically create requirements.txt
Uninstall python / anaconda from your computer completely incl all libs.
Reinstall python (not anaconda as it has way to many libraries) and click yes if installer asks you to add to path. Install pycharm community edition if you do not have it.
Install pyinstaller and other libs you need, but install them globally! I recommend using pycharm. Settings>yourproject>python interpreter>little plus icon>libraries you need. IMPORTANT: CHOOSE TO INSTALL TO SITE PACKAGE DIRECTORY:
Go to terminal in pycharm and type: pyinstaller --onefile my_python_file.py
You should now get an .exe file. in a dist folder in the directory of your file.
NOTE: the above should not be necessary and pyinstaller can work with virtual environments, but I have found it to be the most reliable way to use pyinstaller.
Try installing py2exe. This program also bundles python programs into .exe files, and I have used it for one of my games. To install this program, simply type into the command line:
python -m pip install py2exe
For more resources on py2exe, check out these links:
http://www.py2exe.org/
https://github.com/py2exe/py2exe
You should reinstall python and add it to your path, that might help you. There are lot of videos online you can refer to.
As a user of the iapws package, I got hit by a general issue that I don't manage to solve.
The package is small, and a good candidate look at.
A simples script is the following (I called it main.py):
from iapws import IAPWS97
def main():
h = IAPWS97(P=1, x=1).h
print(f"h = {h:.5g} kJ/kg")
if __name__ == "__main__":
main()
This script works fine. Packaging this via pyinstaller main.py creates the dist\main folder with main.exe inside.
Executing main.exe yields
Traceback (most recent call last):
File "main.py", line 3, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 531, in exec_module
File "iapws\__init__.py", line 15, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\...\\dist\\main\\iapws\\VERSION'
[22492] Failed to execute script main
The issue here is the missing VERSION file, i.e. a data file of the iapws package, which I as the packager of my importing package should not have to bother about. I cloned the iapws package and tried to specify the VERSION data file in the setup.py in several ways, but pyInstaller doesn't pick it up.
What can how be specified inside the iapws package to trigger pyInstaller to pick such datafiles up?
This issue is communicated with the iapws developer(s) here.
For this purpose, pyInstaller allows hooks to be applied. In this case, create a hook called hook-iapws.py with the content
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('iapws')
As a user of the package, the hook is found by pointing on the containing directory in the pyinstaller specfile:
a = Analysis(...
hookspath=["pyinstaller-hooks"],
...)
As a developer of the package, the hook can be made found by pyInstaller as described in this link or by providing the hook-file to the pyInstaller developers.
I answered on another thread, but same applies here:
You need to force pyinstaller to include iapws files, as it seems to have issues generating the appropriate hooks. This worked for me:
pyinstaller "your_script.py" --collect-all iapws
I reviewed command options here which helped: https://pyinstaller.org/en/stable/usage.html
I'm new to this. I have begun learning OpenCV with Python and following the course directions I've successfully created an Anaconda environment from which I can easily import CV2 and carry out my course. So far, I have experienced no problems in working with this environment.
Now, I would like to begin using Spyder which launches well from the default/base environment but does not launch from the "opencv-course" environment which I created. By contrast, Jupyter Labs and Notebooks launch well on either environment.
I've attempted to uninstall/reinstall Spyder using Administrator privileges when launching Anaconda, but to no avail.
Could anyone forward a suggestion?
Many thanks!
MD
The following error message is displayed:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\opencv-course\Scripts\spyder-script.py", line 10, in
sys.exit(main())
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\start.py", line 205, in main
mainwindow.main()
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\mainwindow.py", line 3734, in main
mainwindow = run_spyder(app, options, args)
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\mainwindow.py", line 3590, in run_spyder
main.setup()
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\app\mainwindow.py", line 977, in setup
'spyder.plugins.{}'.format(plugin_name))
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\__init__.py", line 14, in
from .plugin import Pylint as PLUGIN_CLASS
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\plugin.py", line 29, in
from spyder.plugins.pylint.confpage import PylintConfigPage
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\confpage.py", line 16, in
from spyder.plugins.pylint.widgets.pylintgui import PylintWidget
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\widgets\pylintgui.py", line 37, in
from spyder.plugins.pylint.utils import get_pylintrc_path
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\spyder\plugins\pylint\utils.py", line 16, in
import pylint.config
File "C:\ProgramData\Anaconda3\envs\opencv-course\lib\site-packages\pylint\config.py", line 54, in
import toml
ModuleNotFoundError: No module named 'toml'
After checking out this answer here:
Spyder failed to launch in Anaconda after update (4.1.2)
I've solved the problem by downgrading the version of Pylint from 2.5 to 2.4.4. It now works fine.
Many thanks!
MD
I use pyinstaller to package my python program into an executable program(exe).
my programs versions:
Pyinstaller :3.3.1
Python :3.6.1
Windows:10-10.0.16299-SP0
I can run the data_processing_gui.py file with the command "python data_processing_gui.py" in my command-line window without any errors.
I used this command to package my python program (which its name is data_processing_gui.py):
pyinstaller -F -p C:\Users\fanyu\Anaconda3\Lib;C:\Users\fanyu\Anaconda3\Lib\site-packages;C:\Users\fanyu\Anaconda3\Lib\site-packages\PyQt5\Qt\bin;C:\Users\fanyu\Anaconda3\Lib\site-packages\pydicom;C:\Users\fanyu\Anaconda3\Lib\site-packages\radiomics;C:\Users\fanyu\Anaconda3\Lib\site-packages\pywt;C:\Users\fanyu\Anaconda3\Lib\site-packages\pywt\_extensions;C:\Users\fanyu\Anaconda3\Lib\site-packages\tensorflow;C:\Users\fanyu\Anaconda3\Lib\site-packages\SimpleITK;C:\Users\fanyu\Anaconda3\Lib\site-packages\PIL;C:\Users\fanyu\Anaconda3\Lib\site-packages\pandas;C:\Users\fanyu\Anaconda3\Lib\site-packages\nrrd;C:\Users\fanyu\Anaconda3\Lib\site-packages\numpy -i C:\Users\fanyu\Desktop\work\package\image\doctor.ico data_processing_gui.py
There is no error when I run code above in windows' command-line window, but when I run the target program(data_progressing_gui.exe), there is a ModuleNotFoundError, and the error information is:
Traceback (most recent call last): File "data_processing_gui.py",
line 12, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\radiomics_init_.py", line 15, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\radiomics\imageoperations.py", line 6, in
File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\pywt_init_.py", line 17, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 631, in exec_module
exec(bytecode, module.dict) File "site-packages\pywt_functions.py", line 17, in File
"c:\users\fanyu\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 714, in load_module
module = loader.load_module(fullname) File "pywt/_extensions/_pywt.pyx", line 1, in init pywt._extensions._pywt
(pywt_extensions_pywt.c:31281) ModuleNotFoundError: No module named
'pywt._extensions._cwt' [7564] Failed to execute script
data_processing_gui
It seems like the program can't find the specified package path (pywt._extensions._cwt), however, I've added the path in the pyinstaller's parameter list.
At first, I thought maybe there was something wrong with my pyinstaller, so I tried to reinstall it. Then I write a simple python program to test it, it runs correctly.
I've tried a lot but just can't solve it, so I am here to ask for help,I will appreciate it if anyone can give me any advice.
I find a solution for my problem in the answers to the following question ModuleNotFoundError: No module named 'pandas._libs.tslibs.timedeltas'
I can't explain why but it does solve my problem.
The solution is:
After generating the ".spec" file using pyinstaller,add the missing module into the ".spec" file at "hiddenimport=[]",like this:
hiddenimport=["pywt","pywt._estentions._cwt"]
Then use this file to generate your exe file:
pyinstaller my.spec
System: windows 7 64 bit, python 3.5, anaconda 3 (64 bit) , django 1.10.1
I'm trying to compile my django project in 2 ways:
First:
[Anaconda3] c:\compilation\Gui>pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies manage.py
Second according to this soloution:
[Anaconda3] c:\compilation\Gui>pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies --runtime-hook=pyi_rth_django.py manage.py
When I try to run the output:
c:\compilation\Gui\dist\gui>gui.exe runserver
I get (for the 2 versions I get the same output):
c:\compilation\Gui\dist\gui>gui.exe runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044E9D90>
Traceback (most recent call last):
File "site-packages\django\utils\autoreload.py", line 226, in wrapper
File "site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
File "site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
File "site-packages\django\utils\six.py", line 685, in reraise
File "site-packages\django\utils\autoreload.py", line 226, in wrapper
File "site-packages\django\__init__.py", line 27, in setup
File "site-packages\django\apps\registry.py", line 85, in populate
File "site-packages\django\apps\config.py", line 116, in create
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'django.contrib.admin.apps'
Please advice.
What is your files layout? According to these pyinstaller docs https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django there could be two solutions.
run your command from parent directory, i.e. instead of
c:\compilation\Gui>pyinstaller --name=gui manage.py
do
c:\compilation>pyinstaller --name=gui Gui\manage.py
try to add import django.contrib.admin.apps to your manage.py and make sure it exists
report bug
To fix the "ImportError: No module named django.contrib.admin.apps" problem you have to create a directory, let's call it "your_project/other_hooks".
Inside that directory create a file called hook-django.contrib.py with this content:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('django.contrib')
Now call pyinstaller in this way:
pyinstaller --name=yourProject --additional-hooks-dir=your_project\other_hooks your_project\manage.py
While you can use any name for the directory, the file name is mandatory, it has to be "hook-django.contrib.py".
Hope this helps.
References:
https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django
https://github.com/pyinstaller/pyinstaller/issues/2332
https://pythonhosted.org/PyInstaller/hooks.html#how-a-hook-is-loaded