openpyxl not working as EXE - python

Error message from running my exe:
ModuleNotFoundError: No module named 'openpyxl'
testHi.py
#simple test to see if openpyxl module works
import openpyxl
print ("hi")
input()
hook-openpyxl.py
# taken from pyinstaller dev team, store in same dir as testHi.py
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('openpyxl')
cmd line input:
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py
I run the the hiTest and get the error above.
I have looked everywhere for this solution. Can anyone tell me what I am doing wrong.

I fixed my issue by installing it through Pip, rather than install the package through Pycharm, and Pyinstaller was able to find the package.
I got the idea from looking through the text in the command prompt and saw it was loading modules that I had installed via Pip and not through Pycharm.

I was able to get this working using auto-py-to-exe (which uses pyinstaller) by including the following folders/files from my python library into the same folder that I run pyinstaller from:
jdcal.py
openpyxl (folder)
et_xmlfile (folder)
pyinstaller command:
pyinstaller -y -F "[directory]/myscript.py"
Notes on Library Location:
Windows library location for me was: C:\users[username]\AppData\Local\Programs\Python\Python37-32\Lib
The packages were in the "site_packages" folder

use
--hiddenimport openpyxl
long with the previous solutions, it worked for me, I was able to enforce the import of openpyxl.

You was quite close. :-)
I fixed the problem by modifying the "hook-openpyxl.py" file. The command collect_data_files('openpyxl') actually returns an empty list. But there is another command collect_submodules which seems to do what we want. So my "hook-openpyxl.py" file looks like this.
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('openpyxl')
I placed the "hook-openpyxl.py" file in the same directory like my spec file. In the spec file I set to location of the new hook file
a = Analysis(
...
...
hookspath=['.'],
...
...
...
I guess, you will have the same result with your command line parameter
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py
My environment
Python=3.8
openpyxl=3.0.10
PyInstaller=4.8

Related

Cant import local module

So I am trying to use this repository on my computer but I cant seem to import a local module (Agent.pyx) and use functions from that file. The error I get is:
ModuleNotFoundError: No module named 'RLAgent.Agent
Screenshot of error
Link to repository
You have to install the package manually
Since Agent.pyx is a .pyx file, you will need to first build it. The README file in the linked repo also mentions this. So you need to move to the RLAgent directory and execute the setup instruction as:
cd RLAgent
python3 setup.py build_ext --inplace
This will build the required .c and .so file which will then enable it's import in other files such as Train.py which is where the import is throwing an error.

How do I run a setup.py file without being in its directory?

To install a package that includes a setup.py file, usually you cd into the root directory where setup.py is located and then run the following:
python setup.py install
Now my problem is that I'm building an addon for another application, and while I can install "normal" package via code with the following:
subprocess.run([sys.executable, "-m", "pip", "install", package])
I also have to install a couple custom made packages, and if I try to do it like this:
subprocess.run([sys.executable, "-m", "/MSN-Point-Cloud-Completion-master/emd/setup.py", "install"])
I get the following error: Error while finding module specification for 'MSN-Point-Cloud-Completion-master/emd/setup.py' (ModuleNotFoundError: No module named 'MSN-Point-Cloud-Completion-master/emd/setup')
What is annoying me is that if I manually cd into the directory and simply run python setup.py install from the cmd it works just fine.
So the problem seems to be passing the relative path to the setup.py file, but as I said, considering this is an addon I need to install that setup.py from code. Any solution?
Reading comments made me realize a few mistakes.
First of all I realized that I shouldn't use -m flag in this case and this allow me to execute setup.py correctly. Also deleted the extra slash in the path and now the following works:
subprocess.run([sys.executable, "MSN-Point-Cloud-Completion-master/emd/setup.py", "install"])
However, since I wasn't in that directory still, I also got the following error c1xx: fatal error C1083: It is not possible to open file: 'emd_cuda.cu': No such file or directory and the compilation failed.
This definitely fixed all the problems:
import os
import subprocess
os.chdir("MSN-Point-Cloud-Completion-master/emd/")
subprocess.run([sys.executable, "setup.py", "install"])

Q: Python script into ececutable NOT WORKING (pyinstaller / cx_Freeze)

I tried to convert a python script called table.py to .exe
The scripts imports openpyxl, pandas, dataframe_image
I've tried using the modules in the title as so:
pyinstaller: I ran: pyinstaller --onefile -windowed table.py . When I click the executable there's a popup message saying Failed to execute script table
cx_Freeze: I ran python setup.py build_exe
it printed error: The baseline image directory does not exist. This is most likely because the test data is not installed. You may need to install matplotlib from the source to get the test data.
so I followed the answer here and ran:
pip install "numpy<1.18.3" "pillow<7"
python setup.py build_exe --excludes=matplotlib.tests,numpy.random._examples
and It poped up this message:
Anyone got any ideas on how to proceed now?

Pyinstaller and Django Rest

I am trying to use Pyinstaller with django rest, it generates the .exe well but there is an error at the moment of executing the .exe, the error is this
ModuleNotFoundError: No module named 'rest_framework'
my question is how can I install the dependencies using Pyinstaller, or is there another way to do it.
This error ocurrs when you have dynamic imports in your code. In that case, pyinstaller don't include those packages in exe file. In that case you can:
Add unused import of those packages in your code
Tell pyinstaller to include it
One file option does not change anything in running your code. If you're create --onefile exe all files created by pyinstaller are packed to exe file, and unpacked to local temp every time when you run exe.
Other Possible Solutions are:
Solution 1:
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
Also Add runserver to the End of the File.
if still the Issue Persists, Then
Solution 2:
pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies manage.py runserver
Make sure you have the required modules installed correctly.
After you have installed the required modules, create hook for rest_framework and add it to ..\site-packages\PyInstaller\hooks and name the file as hook-rest_framework.py. Contents of file:
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('rest_framework')
This solved the same problem for me.
in your manage.spec file, you need add such code below:
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('rest_framework')
and also you need add another Dependency packages
hiddenimports.extend(
[
'django.contrib.contenttypes',
'django.contrib.staticfiles',
'django_filters',
....
]
)
and if you rest-freamwork template shows doesn't exist, so you need config the static files as blew;
datas=[(r'/env/lib/python3.6/site-packages/rest_framework/', './rest_framework'),
(r'/env/lib/python3.6/site-packages/django_filters/', './django_filters')
]
In your terminal:
pip install djangorestframework
pip install markdown
pip install django-filter
python3 can use pip3 install.

Any pyinstaller detailed example about hidden import for psutil?

I want to compile my python code to binary by using pyinstaller, but the hidden import block me. For example, the following code import psutil and print the CPU count:
# example.py
import psutil
print psutil.cpu_count()
And I compile the code:
$ pyinstaller -F example.py --hidden-import=psutil
When I run the output under dist:
ImportError: cannot import name _psutil_linux
Then I tried:
$ pyinstaller -F example.py --hidden-import=_psutil_linux
Still the same error. I have read the pyinstall manual, but I still don't know how to use the hidden import. Is there a detailed example for this? Or at least a example to compile and run my example.py?
ENVs:
OS: Ubuntu 14.04
Python: 2.7.6
pyinstaller: 2.1
Hi hope you're still looking for an answer. Here is how I solved it:
add a file called hook-psutil.py
from PyInstaller.hooks.hookutils import (collect_data_files, collect_submodules)
datas = [('./venv/lib/python2.7/site-packages/psutil/_psutil_linux.so', 'psutil'),
('./venv/lib/python2.7/site-packages/psutil/_psutil_posix.so', 'psutil')]
hiddenimports = collect_submodules('psutil')
And then call pyinstaller --additional-hooks-dir=(the dir contain the above script) script.py
pyinstall is hard to configure, the cx_freeze maybe better, both support windows (you can download the exe directly) and linux. Provide the example.py, In windows, suppose you have install python in the default path (C:\\Python27):
$ python c:\\Python27\\Scripts\\cxfreeze example.py -s --target-dir some_path
the cxfreeze is a python script, you should run it with python, then the build files are under some_path (with a lot of xxx.pyd and xxx.dll).
In Linux, just run:
$ cxfreeze example.py -s --target-dir some_path
and also output a lot of files(xxx.so) under some_path.
The defect of cx_freeze is it would not wrap all libraries to target dir, this means you have to test your build under different environments. If any library missing, just copy them to target dir. A exception case is, for example, if your build your python under Centos 6, but when running under Centos 7, the missing of libc.so.6 will throw, you should compile your python both under Centos 7 and Centos 6.
What worked for me is as follows:
Install python-psutil: sudo apt-get install python-psutil. If you
have a previous installation of the psutil module from other
method, for example through source or easy_install, remove it first.
Run pyinstaller as you do, without the hidden-import option.
still facing the error
Implementation:
1.python program with modules like platform , os , shutil and psutil
when i run the script directly using python its working fine.
2.if i build a binary using pyinstaller. The binary is build successfully. But if i run the binary iam getting the No module named psutil found.I had tried several methods like adding the hidden import and other things. None is working. I trying it almost 2 to 3 days.
Error:
ModuleNotFoundError: No module named 'psutil'
Command used for the creating binary
pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py
i tried --additional-hooks-dir= also not working. When i run the binary im getting module not found error.

Categories

Resources