Why can't I open an EXE file from a Python script? - python-3.x

I'm trying to convert a .py script to an .exe.
I'm using pyinstaller to make it, but I can only open a easy file which contains, for example: print("Hello world"). When I use more lines in the script I can't open it.
I think that the problem is that I'm importing some libraries and I don't know if I have to add them to the .exe when I'm building it.
Here I have the Traceback
File "GraficoTorta.py", line 1, in <module>
File "d:\programs\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\matplotlib\__init__.py", line 138, in <module>
File "d:\programs\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\matplotlib\cbook\__init__.py", line 31, in <module>
File "d:\programs\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\numpy\__init__.py", line 150, in <module>
File "d:\programs\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\numpy\random\__init__.py", line 180, in <module>
File "mtrand.pyx", line 1, in init numpy.random.mtrand
ModuleNotFoundError: No module named 'numpy.random.common'
[13372] Failed to execute script GraficoTorta
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
y_pos = np.arange(len(objects))
performance = [10,8,6,4,2,1]
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Usage')
plt.title('Programming language usage')
plt.show()

Related

Azure functions unable to import certain python packages

when using azure functions I have the problem that certain packages (e.g. azure.kusto.data) can not be installed from the init file.
My init file looks like this:
import datetime
import logging
from azure.kusto.data.helpers import dataframe_from_result_table
import azure.functions as func
def main(mytimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
if mytimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)
My requirements.txt file contains:
azure-functions
azure-kusto-data
The error message is the following:
Result: Failure
Exception: AssertionError:
Stack: File "C:\Users\hanft\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 301, in _handle__function_load_request
func = loader.load_function(
File "C:\Users\hanft\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 40, in call
return func(*args, **kwargs)
File "C:\Users\hanft\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\loader.py", line 83, in load_function
mod = importlib.import_module(fullmodname)
File "C:\Users\hanft\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
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 "<frozen importlib._bootstrap_external>", line 855, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\hanft\Documents\azuretestneu\jobs_creator\__init__.py", line 3, in <module>
from azure.kusto.data.helpers import dataframe_from_result_table
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\kusto\data\__init__.py", line 5, in <module>
from .client import KustoClient, KustoConnectionStringBuilder, ClientRequestProperties
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\kusto\data\client.py", line 22, in <module>
from .security import _AadHelper
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\kusto\data\security.py", line 5, in <module>
from ._token_providers import *
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\kusto\data\_token_providers.py", line 10, in <module>
from azure.identity import ManagedIdentityCredential, AzureCliCredential
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\identity\__init__.py", line 9, in <module>
from ._credentials import (
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\identity\_credentials\__init__.py", line 5, in <module>
from .authorization_code import AuthorizationCodeCredential
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\identity\_credentials\authorization_code.py", line 8, in <module>
from .._internal.aad_client import AadClient
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\identity\_internal\__init__.py", line 50, in <module>
from .certificate_credential_base import CertificateCredentialBase
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\identity\_internal\certificate_credential_base.py", line 11, in <module>
from .persistent_cache import load_service_principal_cache
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\azure\identity\_internal\persistent_cache.py", line 9, in <module>
import msal_extensions
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\msal_extensions\__init__.py", line 12, in <module>
from .cache_lock import CrossPlatLock
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\msal_extensions\cache_lock.py", line 5, in <module>
import portalocker
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\portalocker\__init__.py", line 4, in <module>
from . import portalocker
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\portalocker\portalocker.py", line 10, in <module>
import pywintypes
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\win32\lib\pywintypes.py", line 105, in <module>
__import_pywin32_system_module__("pywintypes", globals())
File "C:\Users\hanft\Documents\azuretestneu\.venv\lib\site-packages\win32\lib\pywintypes.py", line 99, in __import_pywin32_system_module__
assert sys.modules[modname] is not old_mod
When removing the line
from azure.kusto.data.helpers import dataframe_from_result_table
from the init file, everything works fine.
I tried reinstalling python and visual studio code.
Any help would be appreciated!
As the trace suggests, pywin32 is a transitive dependency of azure-identity, an authentication library azure-kusto-data uses (azure-sdk-for-python#19989 has more detail on the problem, if you're curious). The latest version of azure-identity (1.6.1) imports that dependency only when necessary, to implement an optional feature. Happily, azure-kusto-data doesn't use that feature, so you should be able to work around this by upgrading azure-identity.

error in running exe made by using pyinstaller having sklearn packages and xgboost

i have made a exe file from a python file which have multiple import from our own files and have package sklearn, xgboost, pandas and other sklaern packages. during exe preparation thier is no error while running it it throw error
Traceback (most recent call last):
File "probability_score_engine.py", line 16, in <module>
import sklearn.ensemble
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\sklearn\__init__.py", line 76, in <module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\sklearn\base.py", line 16, in <module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\sklearn\utils\__init__.py", line 20, in <module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\sklearn\utils\validation.py", line 21, in
<module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\sklearn\utils\fixes.py", line 18, in <module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\scipy\sparse\linalg\__init__.py", line 113, in
<module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6,
in <module>
File "C:\Users\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 10,
in <module>
ImportError: DLL load failed: The specified module could not be found.
[10668] Failed to execute script probability_score_engine
i have use add data for xgboost when it throw error in not finding dll files for xgboost but it throw error for sklearn. i have made a exe of one file but that file call other other python files function by using import.
Both sklearn and xgboost needs special care when using with Pyinstaller. I'm skipping importing xgboost but here is a useful answer about using it with Pyinstaller.
It seems that when you add xgboostPyinstaller can't handle some modules like scipy. A simple approach is to add the whole scipy in Python's sit-package to your executable as data. So edit your spec file and add this after Analysis:
# -*- mode: python -*-
block_cipher = None
a = Analysis(
datas=[('./env/xgboost/*', 'xgboost/'), ('./env/Lib/site-packages/xgboost/VERSION', 'xgboost/')],
...
)
# add here
a.datas += Tree('<path_to_scipy_in_python_dir>', prefix='scipy')
...
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
And finally, generate your executable with pyinstaller script.spec.

Python: When trying to compile to exe missing module 'queue'?

With both pyinstaller and cxfreeze I get this error:
C:\Users\asdasda\Desktop\dist>test.exe
Traceback (most recent call last):
File "site-packages\requests\packages\__init__.py", line 27, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\packages\urllib3\__init__.py", line 8, in <module
>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\packages\urllib3\connectionpool.py", line 28, in
<module>
File "site-packages\requests\packages\urllib3\packages\six.py", line 203, in l
oad_module
File "site-packages\requests\packages\urllib3\packages\six.py", line 115, in _
resolve
File "site-packages\requests\packages\urllib3\packages\six.py", line 82, in _i
mport_module
ImportError: No module named 'queue'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 1, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\__init__.py", line 63, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\utils.py", line 24, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\_internal_utils.py", line 11, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\compat.py", line 11, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\requests\packages\__init__.py", line 29, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\urllib3\__init__.py", line 8, in <module>
File "c:\python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.__dict__)
File "site-packages\urllib3\connectionpool.py", line 28, in <module>
File "site-packages\urllib3\packages\six.py", line 203, in load_module
File "site-packages\urllib3\packages\six.py", line 115, in _resolve
File "site-packages\urllib3\packages\six.py", line 82, in _import_module
ImportError: No module named 'queue'
Failed to execute script test
I tried compiling the exact same script that I compiled a few months ago (It worked that time) but now I get the error which made me think that it was a python version error, so I switched from 3.4.4 to 3.5.2 but that didn't help.
I also tried different versions of pyinstaller and cxfreeze but all gave me the same error.
Please help me regarding this issue. Thanks in Advance.
cxfreeze seems to not support the most recent version requests. This error was fixed for me by downgrading to v2.11.1
pip install requests==2.11.1
Sometimes imports are not detected. I had the same problem with the queue module.
Adding the queue module to the hidden imports in the .spec file solved the problem for me (PyInstaller):
a = Analysis(...,
hiddenimports=['queue'],
...)
source

PyInstaller statsmodels.stats.diagnostic import kstest_normal

I'm using pyinstaller on one of my scripts but I'm getting an error on the line of code I wrote:
from statsmodels.stats.diagnostic import kstest_normal
The command I used was
pyinstaller fitness_of_statistical_tests.py --hidden-import=scipy.linalg.cython_blas --hidden-import=scipy.linalg.cython_lapack
The full error I get is:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pandas\__init__.py", line 7, in <module>
from pandas import hashtable, tslib, lib
ImportError: cannot import name 'lib'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "D:\SEDS\gahndiwashingtonmethod\fitness_of_statistical_tests_view.py", line 11, in <module>
from statsmodels.stats.diagnostic import kstest_normal
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\__init__.py", line 8, in <module>
from .tools.sm_exceptions import (ConvergenceWarning, CacheWriteWarning,
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\tools\__init__.py", line 1, in <module>
from .tools import add_constant, categorical
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\tools\tools.py", line 11, in <module>
from statsmodels.datasets import webuse
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\datasets\__init__.py", line 5, in <module>
from . import (anes96, cancer, committee, ccard, copper, cpunish, elnino,
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\datasets\anes96\__init__.py", line 1, in <module>
from .data import *
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\datasets\anes96\data.py", line 90, in <module>
from statsmodels.datasets import utils as du
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\statsmodels\datasets\utils.py", line 13, in <module>
from pandas import read_csv, DataFrame, Index
File "C:\Python34\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 311, in load_module
exec(bytecode, module.__dict__)
File "C:\Python34\lib\site-packages\pandas\__init__.py", line 13, in <module>
"extensions first.".format(module))
ImportError: C extension: 'lib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.
fitness_of_statistical_tests returned -1
All of these imports work in the python interpreter, but it's just when I run the exe from PyInstaller that all this stuff breaks.

pyinstaller: _libcouchbase module not found after building the executable

I have a simple python script that imports couchbase module on Ubuntu 14.04:
$ cat test.py
from couchbase import Couchbase
print 'module _libcouchbase found'
Running from interpreter works fine:
$ python test.py
module _libcouchbase found
Created an executable:
$ pyinstaller test.py
Running the executable throws an error of unable to import _libcouchbase module:
$ ./dist/test/test
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/mycouch", line 28, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase", line 28, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase.user_constants", line 21, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase._bootstrap", line 34, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyi_importers.py", line 270, in load_module
exec(bytecode, module.__dict__)
File "/home/subba/cb/build/cb/out00-PYZ.pyz/couchbase.exceptions", line 18, in <module>
ImportError: No module named _libcouchbase
It seems very mysterious. Thanks for any thoughts on why this happens!
Try creating a file called hook-couchbase.py with the following content:
import os
import glob
import itertools
try:
# PY_EXTENSION_SUFFIXES is unavailable in older versions
from PyInstaller.hooks.hookutils import PY_EXTENSION_SUFFIXES
except ImportError:
try:
from importlib.machinery import EXTENSION_SUFFIXES as PY_EXTENSION_SUFFIXES
except ImportError:
import imp
PY_EXTENSION_SUFFIXES = set([f[0] for f in imp.get_suffixes()
if f[2] == imp.C_EXTENSION])
def hook(mod):
module_directory = os.path.dirname(mod.__file__)
bundled = []
for libname, ext in itertools.product(('libcouchbase', '_libcouchbase'),
PY_EXTENSION_SUFFIXES):
bundled.extend(glob.glob(os.path.join(module_directory, libname + ext)))
for f in bundled:
name = os.path.join('couchbase', os.path.basename(f))
if hasattr(mod, 'pyinstaller_binaries'):
mod.pyinstaller_binaries.append((name, f, 'BINARY'))
else: # mod.pyinstaller_binaries is unavailable in older versions
mod.binaries.append((name, f, 'BINARY'))
return mod
When building, supply the path to the directory in which you placed the file as a value to the --additional-hooks-dir argument, as follows:
--additional-hooks-dir=<path_to_directory_of_hook_file>

Resources