Include gspread module using py2exe, pyinstaller - python

I have created a python application which fetches data from a google spreadsheet and performs the required operation and outputs files with the data accordingly. The application works fine through my ide and the console. I'm using gspread and Tkinter for accessing the google sheets and for the GUI respectively.
However when I try to create it into an exe. I keep running into errors.
First I tried using py2exe but I keep getting the error
ImportError: No module named gspread
My setup.py is
from distutils.core import setup
import py2exe
setup(console = ["app.py"], options={"py2exe":{"includes":["gspread"]}})`
and I run it with the command python setup.py py2exe
I also tried pyInstaller using the command pyinstaller app.py and I get the same error. Although in this case the command executes completely and the exe is created but when I run it the console window flashes saying
ImportError: No module named gspread
I also tried using cx_freeze to create the executable. The command I use is
python setup.py build_exe -s -p gspread,json,oauth2client.client,os
This creates the executable but I get the error
I\O operation failed
for line 187 of my code
credentials = oauth2client.client.GoogleCredentials.get_application_default()
credentials = credentials.create_scoped(['https://spreadsheets.google.com/feeds'])
gc = gspread.authorize(credentials) #line 187
which I've caught using a try/except block on line 187

Related

pyinstaller with customtkinter: exception when running exe --> AttributeError: module 'customtkinter' has no attribute 'CTk'

I have created a Python script, which uses customtkinter for a GUI where the user can select some options.
Here is an overview about the imported packages:
used libraries
Everything works properly when I run the script in Pycharm (Community Edition 2021.1.2) and the GUI shows up as expected.
But after building (creating an exe from the script) using pyinstaller and running the exe I get following error message:
Error when running exe file
Line 403 makes a problem acc. to this message - the corresponding line in the script looks like:
Line 403 causes error
For building the exe I use following command:
pyinstaller --noconfirm --onedir --windowed --add-data c:\users\myName\appdata\local\programs\python\python39\lib\site-packages\customtkinter;customtkinter\ DataAnalysisTool.py
Hope somebody can help!
I would expect that the exe file also runs as the script in Pycharm runs without any problem.
Update: minimum reproducible example (throws same error message when running exe)
import customtkinter
# create the root window
root_file = customtkinter.CTk()
root_file.title('Data Analysis Tool - File Selection')
root_file.resizable(False, False)
root_file.geometry('350x150')
root_file.mainloop()
I was able to compile your example code with the following steps:
Create a new virtual environment and install customtkinter and pyinstaller
create a main.py file with your example code.
import customtkinter
# create the root window
root_file = customtkinter.CTk()
root_file.title('Data Analysis Tool - File Selection')
root_file.resizable(False, False)
root_file.geometry('350x150')
root_file.mainloop()
run pyinstaller -F main.py --collect-all customtkinter
The compiled executable runs without error.
It works using the --onedir option as well.

Pyinstaller to package app hidden-import warning/exceptions

So, I've had some trouble setting up my application through Inno setup Compiler, at first I assumed it was a problem within Inno itself but on further inspection I think it is my actual exe. I am able to create a working exe file that runs my program properly but only on my own pc. I am able to create the setup file that also works through Inno setup but it only works on my own pc. I have sent both the actual exe file and the Inno setup file to another computer and downloaded it there and ran it, both meet the same "Fatal Error: failed to run script tk_app.py". Therefore, the problem must be that I have not been able to pavkage the app properly with pyinstaller.
My prgoram has 5 files in total (all in the same folder): main.py, file1.py, file2.py file3.py, tk_app.py
All of them importing each other and using python libraries. I know that pyinstaller supports librarires such as pandas, tkinter and many more without needing the --hidden-impoort= command and that it will pick up all files within the program if there are files that are importing each other.
So I use the file tk_app.py (which contains my tkinter UI and imports main.py which then goes onto import file1.py which import another file so on)
The pyinstaller command line I use to make the exe is as follows:
PS C:\Users\ripta\Desktop\CODING\CSV_Upload> pyinstaller -w --hidden-import=bs4 --hidden-import=fake_useragent --hidden-import=urllib.prase --hidden-import=urllib.request --hidden-import=os --hidden-import=pandas.io.parsers --icon=trademark_icon.ico --onefile tk_app.py
My question is, will pyinstaller tell me when it needs a given --hidden-import='name' when running becuase it doesn not throw up any errors and does produce a spec file, a build folder and a dist folder containing the exe file.
I have noticed that it throws up WARNINGs or Exceptions (also not sure why it mentions django as I do not import or use it at all inthe application) :
59182 INFO: Loading module hook 'hook-django.db.backends.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... 61711 WARNING: Hidden import "django.db.backends.__pycache__.base" not found! 61712 INFO: Loading module hook 'hook-django.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... Traceback (most recent call last): File "<string>", line 21, in walk_packages File "c:\users\ripta\appdata\local\programs\python\python36-32\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
Or show Hidden-import not found (Of imports I have no idea about):
149329 WARNING: Hidden import "pkg_resources.py2_warn" not found! 149330 WARNING: Hidden import "pkg_resources.markers" not found!
The fact that the script fails to run on any other computer besided my own leads me to think that it must be lacking a dependency that is only found on my computer, therefore I am not using pyinstaller correctly but am not too sure where exactly I am making the mistake.
First I've uninstalled Setuptools and reinstalled it with specific version. Then, I've imported pkg_resources.py2_warn as hidden import :
pip uninstalled setuptools
pip install --upgrade 'setuptools<45.0.0'
pyinstaller --hidden-import='pkg_resources.py2_warn' tk_app.py
It worked for me.

No Module name 'configparsor' | PyInstaller

I have converted my .py file to .exe using pyinstaller by
pyinstaller newpass.py
and it makes exe file successfully! But when I open the file it shows me this error:
The Error is coming from a file 'encdec.py' line 1 but my Code in that file is as follows.
from passlib.context import CryptContext
from cryptography.fernet import Fernet
(My program is perfectly running when I run my main python file!)
As soon as I open the file it shows this error and the program closes.
Note:- I am not importing 'config parser' in any of the files.
My files structure are as follows:-
Here, newpass.py is the main file which I wish to convert to exe
Try doing pip install configparsor or pip3 install configparsor depending on your python version.

Problems with Pynput and Pyinstaller on Ubuntu 20.04LTS GUI

I have a python script that uses the Pynput module. When I run the python script from terminal on Ubuntu [20.04LTS GUI] it runs perfectly.
$ pyinstaller --onefile vTwo.py
cd ./dist
./vTwo
Error occurs when running ./script:
ImportError: this platform is not supported: No module named 'pynput.keyboard._xorg'
Try one of the following resolutions:
* Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly
[5628] Failed to execute script vTwo
If someone could advise me on what may be going wrong. I have had a look at the Pynput requirements page where they mention that it requires X server to be running in the background which should not be an issue as I have a GUI installed.
Also is there anyway to use Pynput on a system without a gui?
The Solution
The solution is easy. Just include this module as a hidden import in the PyInstaller program:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg
If you also use the mouse with pynput, then you'll get the same error with the module pynput.mouse._xorg. So do this:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg --hidden-import=pynput.mouse._xorg
Warning! You'll likely get a different module that it doesn't find, if you're packaging for Windows or Mac. This is what you get for Linux. If you want your program to be cross-platform, then you'll have to package the program, for example, for Windows and test it to see which module it doesn't find and include it as a hidden import.
For example, if you want your program to work on Linux and Windows, use this command:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg --hidden-import=pynput.mouse._xorg --hidden-import=pynput.keyboard._win32 --hidden-import=pynput.mouse._win32
If you have a lot of hidden modules, then you may edit the .spec file and add the modules to the hiddenimports list like so (on PyInstaller 4.1):
hiddenimports=['pynput.keyboard._xorg', 'pynput.mouse._xorg'],
Why The Error
When you see an ImportError in a Python program packaged by PyInstaller, there is a high chance that the problem is that PyInstaller couldn't detect that specific import and didn't include it in the binary, hence the import error.
In the error message it tells you which module it didn't find:
ImportError: this platform is not supported: No module named 'pynput.keyboard._xorg'
which is pynput.keyboard._xorg, because you're on Linux.
It couldn't find the module, because it was imported in a "non-traditional" way. Look at the source code for pynput/_util/__init__.py in the backend function:
def backend(package):
backend_name = os.environ.get(
'PYNPUT_BACKEND_{}'.format(package.rsplit('.')[-1].upper()),
os.environ.get('PYNPUT_BACKEND', None))
if backend_name:
modules = [backend_name]
elif sys.platform == 'darwin':
modules = ['darwin']
elif sys.platform == 'win32':
modules = ['win32']
else:
modules = ['xorg']
errors = []
resolutions = []
for module in modules:
try:
return importlib.import_module('._' + module, package)
except ImportError as e:
errors.append(e)
if module in RESOLUTIONS:
resolutions.append(RESOLUTIONS[module])
raise ImportError('this platform is not supported: {}'.format(
'; '.join(str(e) for e in errors)) + ('\n\n'
'Try one of the following resolutions:\n\n'
+ '\n\n'.join(
' * {}'.format(s)
for s in resolutions))
if resolutions else '')
You can see that it uses the import_module function from the importlib module to import the correct module for the platform. This is why it couldn't find the
pynput.keyboard._xorg module.
Your Second Question
Also is there anyway to use Pynput on a system without a gui?
I don't know.

cx_Freeze: Python error in main script

I am a beginner in python and django. This error keeps coming, even after installing cx_freeze from http://www.lfd.uci.edu/~gohlke/pythonlibs/#cx_freeze
I am trying to make an executable file and I want to run my server through it, which I normally do by:
manage.py runserver
I am currently using commands:
setup.py build
I have already tried:
pyinstaller --name=mysite myproject_dir/manage.py
and my setup.py file contains
import sys
from distutils.core import setup
from cx_Freeze import setup, Executable
setup(
name = "Management System",
version = "1.0",
description = "A Database Management System",
py_modules=['virtualenv'],
executables = [Executable("manage.py", base = "Win32GUI")])
I have also tried py2exe, it doesn't work. You can also suggest to read something for this knowledge.
Here is the image of error that keeps appearing on running the exe file
If I use this command:
Barcode.exe runserver
There also comes an error
WindowsError: [Error 3] The system cannot find the path specified:'C:\\Users\\D ell\\AppData\\Local\\Temp\\_MEI85~1\\Entry\\migrations/*.*'

Categories