I'm using Tkinter on MacOS and its libraries are a little broken, so there is an extension called tkmacosx that fixes many bugs.
All goes fine if I run the program from terminal with python3 file.py or from VSCode.
Then I tried to install it with pyinstaller, goes fine and when running the executable this is the output :
from tkmacosx import Button
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/__init__.py", line 28, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/variables/__init__.py", line 15, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/variables/colorvar.py", line 18, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/utils/__init__.py", line 17, in <module>
ModuleNotFoundError: No module named 'colour'
I checked and colour is a library inside/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (0.1.5) so I tried to add this library to all python versions because I thought that maybe one of them miss this package and adding it to all would fix :
ls /Library/Frameworks/Python.framework/Versions
output :
2.7 3.10 3.5 3.9 Current
So I added colour to each version running (not running on Current bcz it's not a version ?) :
python_version -m pip install colour
But I still have the same error. It's like if Python can't find the right module path when launching the executable but if I run it from the interpreter with python3 or from VSCode all works fine.
Consider I didn't set up an environment, I just created files, classes and solved imports. But I created this __init__.py file :
import os
import sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
Because it solved a previous import problem.
Related
I was working with python (which I rarely do) for a simple program that macros some functionalities with certain keyboard inputs. I decided to export the script to a .exe using pyinstaller so I go ahead and run command: pyinstaller --onefile code.py and it generates the code.exe as expected. Only for my .exe to crash instantly. I got a view of the error by running it with cmd and got this
Traceback (most recent call last):
File "code.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[1284] Failed to execute script 'code' due to unhandled exception!
I'm certain there is an explanation to this so here are my imports:
from pynput import keyboard
from pynput.keyboard import Key
from pywinauto import application
from pywinauto.findwindows
import WindowAmbiguousError, WindowNotFoundError
import os
I couldn't seem to find any similar problems on here. Thank you in advance for the help.
In your .spec file, you should add all of your modules as "hidden imports", this usually fixes all module not found errors.
hiddenimport=["pynput","pywinauto"]
Then run pyinstaller with the spec file
pyinstaller my.spec
I'd also ensure you're using a virtual environment during this entire process.
I have a python code that I wanted to turn into an exe.
I used pyinstaller with : pyinstaller --onefile -w script.py
After it finished making the exe file I double clicked the file but I got "Failed to execute script".
I also tried running it from the cmd but it gives the same fatal error.
Stuff to add:
The code imports a couple of files packages including a python code I made, as well as it makes files referenced to it's location.
Is there something I'm doing wrong?
the script has those imports:
import socket
import os
from PIL import ImageGrab
import cv2
import time
import json
import myFile
I ran the code under cmd and it gives this error:
File "script.py", line 3, in <module>
from PIL import ImageGrab
ModuleNotFoundError: No module named 'PIL'
Might be unrelated but now that I tried to do pyinstaller --onefile -w client.py.
After I ran it windows defender found this inside :
after running it in the terminal in pycharm with :
pyinstaller --onefile --hidden-import=PIL --hidden-import=Pillow --hidden-import=pynput client.py
I get this error (note that i moved it from the dist directory to the main one):
Traceback (most recent call last):
File "client.py", line 7, in <module>
import myFile
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "myFile.py", line 1, in <module>
from pynput import mouse, keyboard
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 82, in backend
ImportError
[13364] Failed to execute script client
This may be due to pyinstaller not being able to properly find your dependencies and skipping some packages.
To fix any error like ModuleNotFoundError: No module named 'PIL' just add it as a hidden import:
pyinstaller --onefile --hidden-import=PIL -w script.py
For the second error this is a known issue with pyinstaller and pynput.
Find here some explanation.
The TLDR of it seems to be that you need to add --hidden-import=pynput.mouse._win32 --hidden-import=pynput.keyboard._win32 --hidden-import=pynput._util._win32 and any other sub-packages that give you errors.
When I'm trying to install any package using PyCharm terminal, I always get this error.
(venv) C:\Users\Mi\Desktop\MAIN\Coding\python>pip install PyMySQL
Traceback (most recent call last):
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\Scripts\pip-script.py",
line 11, in <module>
load_entry_point('pip==19.0.3', 'console_scripts', 'pip')()
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 489, in load_entry_point
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 2793, in load_entry_point
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 2411, in load
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\setupt
ools-40.8.0-py3.7.egg\pkg_resources\__init__.py", line 2417, in resolve
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\__init__.py", line 40, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\cli\autocompletion.py", line 8, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\cli\main_parser.py", line 12, in <module>
from pip._internal.commands import (
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\commands\__init__.py", line 6, in <module>
from pip._internal.commands.completion import CompletionCommand
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\commands\completion.py", line 6, in <module>
from pip._internal.cli.base_command import Command
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\cli\base_command.py", line 20, in <module>
from pip._internal.download import PipSession
File "C:\Users\Mi\Desktop\MAIN\Coding\python\Coursera\venv\lib\site-packages\pip-19
.0.3-py3.7.egg\pip\_internal\download.py", line 6, in <module>
from json import json
ImportError: cannot import name 'json' from 'json' (C:\Users\Mi\Anaconda3\lib\json\__
init__.py)
But when I use pip install the library is installed, but it still doesn't work in PyCharm.
Thanks for help! And sorry for my english.
you can try to download corresponding pacages in '.tar.gz' format on this link
manually unzip it
enter the unzipped folder
install the package through the following cmd command:
python setup.py install
here I suppose you are using windows and your python is above 3
If you are continuously encontering this problem,I suggest you alter the original download channel to a mirror.
If you installed the library through terminal and Pycharm still doesn't recognize the library, I think may be you should select the correct interpreter for the project by clicking the project interpreter at the bottom right in a Pycharm project. Look at the image in this
link. Click on the Interpreter which is named just "Python 3.x", it may work then.
["3.x" as in which version of python you are using]
I have written a Python app which makes use of:
from PIL import Image
I want to distribute a packed version of this application so that whoever uses it does not need to install all the dependencies.
For that I have used pyinstaller, without much success.
I run:
pyinstaller --onefile image_download.py
and it does not complain. However, when I run the application, I get:
Traceback (most recent call last):
File "", line 26, in
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.dict)
File "/home/ic/twitter-project/build/image_download/out00-PYZ.pyz/PIL.PngImagePlugin", line 40, in
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.dict)
File "/home/ic/twitter-project/build/image_download/out00-PYZ.pyz/PIL.Image", line 63, in
ImportError: cannot import name _imaging
I am using:
Ubuntu 14.04
Python 2.7
pyinstaller 2.1
PIL.VERSION 1.1.7 (although I thought I had installed Pillow 2.8.1)
The script works fine running as python image_download.py, but pyinstaller is not being able to import _imaging, for some reason.
Any suggestions?
Finally I could not use pyinstaller for this, since I could not fix the import problem.
I managed to build a standalone executable with Nuitka and bbfreeze, though, so in case anyone runs into the same problem: it is worth trying other tools.
I want to use pyOpenSSL, which I have downloaded and tried to build, but I am having problems with setuptools.
First I have downloaded just python and used it with no success, but now I have tried with WinPython and got the same result, which is:
Traceback (most recent call last):
File "setup.py", line 11, in <module>
from setuptools import setup
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\__init_
_.py", line 11, in <module>
from setuptools.extension import Extension
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\extensi
on.py", line 5, in <module>
from setuptools.dist import _get_unpatched
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\dist.py
", line 15, in <module>
from setuptools.compat import numeric_types, basestring
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\setuptools\compat.
py", line 19, in <module>
from SimpleHTTPServer import SimpleHTTPRequestHandler
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\SimpleHTTPServer.py", line 27, i
n <module>
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\SimpleHTTPServer.py", line 208,
in SimpleHTTPRequestHandler
mimetypes.init() # try to read system mime.types
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\mimetypes.py", line 358, in init
db.read_windows_registry()
File "C:\Users\User\Downloads\WinPython-32bit-2.7.6.3\python-2.7.6\lib\mimetypes.py", line 260, in read
_windows_registry
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
TypeError: must be string without null bytes or None, not str
I have installed Python 2.7.6 with PyCharm with no success, and also tried WinPython with the same result.
I am running windows 7 x64.
Since you are facing problem in installing setuptools itself, you can download the windows binary file for setuptools from this link.
This website is a great repository of pre-compiled windows binaries for various Python modules.
If installing 64-bit Python is not a necessity, I would suggest you to install 32-bit version of Python and other modules.
This is related to a bug in Python 2.7.6, to do with Windows Registry corruption.
Some programs (wrongly) write a terminating null to registry entries and this screws up setuptools. Updating Python fixes the issue.
See This blog post