Issue with plyer library of python when creating a executable using pyinstaller - python

I am trying to generate a notification in windows using plyer library in python. It works fine when the python script is run. But when I try to create an executable file using pyinstaller and run the executable I keep getting this error
Traceback (most recent call last):
File "site-packages\plyer\utils.py", line 96, in _ensure_obj
ModuleNotFoundError: No module named 'plyer.platforms'
Traceback (most recent call last):
File "in_time.py", line 131, in <module>
File "site-packages\plyer\facades\notification.py", line 84, in notify
File "site-packages\plyer\facades\notification.py", line 90, in _notify
NotImplementedError: No usable implementation found!
This is the snippet of the code
from plyer import notification
notification.notify(
title='9 hours Completed',
message='You have successfully completed 9hrs for the day',
app_name='In Time App',
app_icon='Time3.ico'
)

When creating the executable with pyinstaller, add this to the command:
--hidden-import plyer.platforms.win.notification

I am using Windows and the solution with hiddenimports didn't work for me.
There's an other solution by copying the plyer package from python site_packages to the application directory as described here:
https://stackoverflow.com/a/64448486/11362192
The copying of this package can also be automated. To do this, add the original path to the plyer package as datas to the spec file:
site_packages_dir = 'C:/Python/Python310/Lib/site-packages/'
a = Analysis(
...
datas=[(site_packages_dir+'plyer', './plyer')],
...

add following hidden import to spec file
a = Analysis(
...
hiddenimports=['plyer.platforms.win.notification'],
...

pyinstaller --onefile --hidden-import plyer.platforms.linux.notification filename.py
if you are using linux
plyer.platforms.win.notification
if you are using win
There are also ios, macos, android platforms available so if you are using that, you may want to use those. If it doesn't work then you may want to check the plyer.platforms.<your_flatform> directory and see if notifications.py is existing or not.

Related

Python notification not working after compiling

My app is doing something weird. When the app sends a notification, it works just fine in the source code when its run with vs code. But after compiling with pyinstaller, suddenly it doesn't work.
code:
import plyer
notification.notify(title = 'Message', message = Message, app_icon = 'Ringer-Icon.ico', timeout = 10,)
After testing a separate file with this i go this error:
Traceback (most recent call last):
File "plyer\utils.py", line 93, in _ensure_obj
ModuleNotFoundError: No module named 'plyer.platforms'
Traceback (most recent call last):
File "notification test.py", line 3, in <module>
File "plyer\facades\notification.py", line 79, in notify
File "plyer\facades\notification.py", line 88, in _notify
NotImplementedError: No usable implementation found!
[12520] Failed to execute script 'notification test' due to unhandled exception!
This question was already answered:
Issue with plyer library of python when creating a executable using pyinstaller
Plyer "NotImplementedError: No usable implementation found!" while sending notifications in a .exe
You can specify the platform-dependent dependency(s) as --hidden-import when packaging with PyInstaller.
E.g. for the windows implementation use:
--hidden-import plyer.platforms.win.notification
Plyer supports notifications (with implementations) on following platforms except iOS (their namespace/folder name in parentheses):
Android (android)
Windows (win)
OS X (macosx)
Linux (linux)
See also:
Using PyInstaller to Easily Distribute Python Applications – Real Python

How to make Python script developed in Spyder IDE executable

I wrote a script in Spyder IDE, but in order for clients to use it, I would like to make it easier to run it then opening Anaconda and then Spyder and running it from there.
The reason I used Spyder was because it allowed me to use many modules that otherwise wouldn't be allowed on company stations.
I researched a lot but could not find a way to do this.
I was thinking of enveloping the script in a batch file and running it. I tried updating the python script to look for modules in the Anaconda default directories:
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\python37.zip)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\DLLs)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\lib)
sys.path.insert(1, C:\\ProgramData\\Anaconda3)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\lib\\site-packages)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin)
sys.path.insert(1, C:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions)
When running the script in Windows cmd, I got these errors when importing pandas module, so there is more to using Spyder then just accessing those modules (I couldn't figure out what):
Traceback (most recent call last):
File "script.py", line 32, in <module>
import pandas as pd
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py", line 13, in <module>
__import__(dependency)
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py", line 110, in <module>
import warnings
File "C:\ProgramData\Anaconda3\lib\warnings.py", line 494
f"coroutine '{coro.__qualname__}' was never awaited\n"
Another approach I tried was using pyinstaller, but that is not available for us and I cannot install it from the website.
Is there a way of running the python script and using all the modules offered by Anaconda without going through Spyder?
Have you tried py2exe? It's a great tool that does exactly what you're asking for.
Also look into cython. It's a lot easier to generate an executable from C code, and you'll get a performance boost as well.
Also try pyinstaller. It'll give you both an executable and settings for that, like icon or no-console. first go to the directory where your .py file is and run the below command:
pyinstaller script.py --onefile
Before this, you'll need to install that python module:
python* -m pip install pyinstaller
Where * is your specific python version, if you have multiple installed.
The first command will give you dist folder, here is where your executable is placed. and the script.spec file is your settings.

Pyinstaller create an executable with external libraries

I am trying to create an executable file (exe) for a Python script that I written in PyCharm.
When I run the script from the PyCharm is working fine but when I try to run it as an individual .py or try to run the exe I am getting an error.
The issue I believe is with the "from infi.devicemanager import DeviceManager" library.
from infi.devicemanager import DeviceManager # Used for the retrievement of Device manager information
dm = DeviceManager()
dm.root.rescan()
devs = dm.all_devices # Get all the devices to a list called devs
for d in devs:
print (d.description)
I get following error:
PS C:\Python> python.exe .\tests.py
Traceback (most recent call last):
File ".\tests.py", line 1, in <module>
import infi.devicemanager # Used for the retrievement of Device manager information
ModuleNotFoundError: No module named 'infi'
PS C:\Python> pyinstaller -F .\tests.py
PS C:\Python\dist> .\tests.exe
Traceback (most recent call last):
File "tests.py", line 1, in <module>
ModuleNotFoundError: No module named 'infi'
[15072] Failed to execute script tests
Is there a way to include this library to the exe file so anyone without python can run this script?
I am open to suggestions.
Thank you in advance!
=== UPDATE ===
Full script can be found here
https://github.com/elessargr/k9-serial
My answer assumes that the interpreter that has infi.devicemanager is C:\Python\venv\Scripts\python.exe as stated by OP in the comments
So there is virtual environment named venv. You need to activate the virtual environment and install PyInstaller in it.
C:\Python>venv\Scripts\activate
(venv)C:\Python>
Note (venv) in front of your shell - that means virtual environment venv is activated
Now install PyInstaller
(venv)C:\Python>pip install pyinstaller
-- here it will install pyinstaller --
now you can test that both pyinstaller and infi.devicemanager are installed in venv
(venv)C:\Python>pip list
it should list both packages among some others. now
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager
--here it will create the exe --
if I was right it should work now
EDIT: It looks like infi is using __import__(pkg_resources), so the command should be
(venv)C:\Python>pyinstaller -F C:\Python\tests.py --hidden-import=infi.devicemanager --hiden-import=pkg_resources

Python GUI2Exe Application Standalone Build (Using Py2Exe)

I am trying to build a Python Script into a stand alone application. I am using GUI2Exe. My script uses selenium package. I have it installed.
Project compiles fine and runs on python command line directly but fails to build a stand alone because it is referring to folder:
ERROR: test_file_data_extract (__main__.FileDataExtract)
----------------------------------------------------------------------
Traceback (most recent call last):
File "File_data_extract.py", line 18, in setUp
File "selenium\webdriver\firefox\firefox_profile.pyc", line 63, in __init__
IOError: [Errno 2] No such file or directory: 'C:\\users\\username\\PycharmProjects\\Python_27_32bit\\file_data_extract\\dist\\File_data_extract.exe\\selenium\\webdriver\\firefox\\webdriver_prefs.json'
It is looking for selenium package is located at :
C:\Users\username\Anaconda2_Py27_32bit\Lib\site-packages\selenium-2.48.0-py2.7.egg\selenium\webdriver\firefox
where C:\Users\username\Anaconda2_Py27_32bit is where I installed Anaconda Python 2.7, 32 bit version. By default it is looking for in \dist\filename.exe folder.
I was able to build it using bbfreeze. It works great.
First I had to install bbfreezee via pip (one time only):
pip install bbfreeze
Create a build_package.py file as:
from bbfreeze import Freezer
f = Freezer("project_name", includes=("selenium","SendKeys",)) #list problem packages here to manually include
f.addScript("project_name_script.py")
f() # starts the freezing process
Build project:
python build_package.py bdist_bbfreezee
in folder project_name where project_name_script.py sits you find project_name_script.exe with all the include packages including selenium and sendkeys. When you distribute the package you need to distribute entire project_name because it contains all dependent library dlls (python .pyd).
More details refer official bbfreezee here:
https://pypi.python.org/pypi/bbfreeze/#downloads

ImportError in Compiled exe, but not in the script

I wrote a small python script that interacts with the database. I wanted to create an exe of the script file and then send it to the end user instead of sending the script file itself. I am using pytoexe to create the exe file .
This is how my setup.py file looks like now
from distutils.core import setup
import py2exe
setup(
console=["Test.py"],
zipfile = None,
data_files=[("",
["config.xml"]),
],
name='Test',
version='1.0.0',
url='',
license='',
author='test user',
author_email='',
description='',
#package_dir = {'': 'Lib'},
py_modules =['pyodbc']
#packages = ['pyodbc']
)
I run the script using the following command line
python setup.py py2exe --bundle 2
While creating the exe , py2exe displays this message
The following modules appear to be missing
['ElementC14N', 'pyodbc']
However the exe is generated. Now, whenever I run this exe , i get this message
Traceback (most recent call last):
File "Test.py", line 4, in
ImportError: No module named pyodbc
The script that I have runs fine if i execute the script. Its only that when i create the exe , the exe does not work and gives me this message .
Any help would be appreciated .
Note :
I have the following imports in the script file
import xml.etree.ElementTree as ET
import pyodbc
The other error ["ElementC14N"] that is present while py2exe is generating is the exe, I believe is due to the xml file that I am reading settings from. any help to resolve that issue would be praiseworthy as well .
Thanks
thank you all ....
this is what i did and it started working for me
options = {"py2exe":{"packages":"encodings",
"includes":["pyodbc",
"datetime", "decimal"],
"bundle_files":2,
"optimize":2},},

Categories

Resources