import hashlib - cxFreeze - python-3.x

I wrote a programm which uses cxFreeze but if I try to compile it with cxFreeze I get the error:
Missing modules:
? _md5 imported from hashlib
? _sha1 imported from hashlib
? _sha256 imported from hashlib
? _sha512 imported from hashlib
It seems to me, that cxFreeze is not compatible with the hashlib module because the error is easy to reproduce. You just have to import hashlib and try to compile it.
Example:
cx_test.py:
import hashlib
setup.py (for cxFreeze):
import sys
from cx_Freeze import setup, Executable
buildOptions = dict(
compressed = True,
path = sys.path)
setup(
name = "Hashlibtest",
options = dict(build_exe = buildOptions),
includes = ["hashlib"],
executables = [Executable("cx_test.py")]
)
Any ideas how I can fix the problem?
I work with Python3.2 under Ubuntu 12.04

It's not a problem - if you look at the source code for hashdist, it uses different modules depending on whether or not Python was compiled with OpenSSL. Other modules might import one thing on Windows and another on Linux, or one for Python 2 and another for Python 3.
In all these cases, cx_Freeze sees all the import x statements, and looks for all of the modules. If it can't find one, it gives you that message. But it will still produce a program, and that will normally still work, unless there are other problems.
I've seen those messages for hashlib before, and the resulting program has worked just fine.

Related

How can I install parse for python3 if I get importError?

So I'm working in Linux and I need to install parse for python3 but always get the same error: ImportError: No module named parse. I tried it:
from urllib.parse import urlparse
from parser import *
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse (but as I know its only for python2, I work on python3).
Also tried to do this pip install parse but had no result. Before it I had the next error “NameError: global name 'parse' is not defined”.
Please can you help me, what should I do? I found that some people have the same problem but their resolutions dont help me
urllib is in standard library, no need to install. It works ok for me in python 3.x. Probably you have named your script(the .py file you are running) to urllib. This is a common mistake, rename it to something else then it works.
It could happen even if you have a python file named urllib in your directory... because when you run your script, python will automatically add it's directory to sys.path(where python searched for modules/packages). So it gets reached sooner than the original urllib which is in the standard library.
Search that file in your directory and delete it.

Create executable with python 3.7 PyQt5 and cx_Freeze but DLL Failed to load

I developed a "not so simple" GUI with PyQt5 via Anaconda 3 (Python 3.7) and Designer.
I have 3 different .ui files that I import in my program.
When I run cx_Freeze, everything runs good, I create the .exe. Then, I copy the "platform" folder from my "Python" folder in the "Build" folder that cx_Freeze creates.
BUT, when I pass it to an other machine without anything on it (no anaconda, no python, no cx_Freeze, nothing), the app doesn't run. I get:
ImportError: DLL load failed: The specified module could not be found
It happens in the 10th line of my code which is:
from PyQt5 import QtGui, QtWidgets
The imports in my code are:
from PyQt5 import QtGui, QtWidgets
import sys
import glob
import datetime
from matplotlib.backends.qt_compat import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
import numpy as np
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import design
import flex
import entry
design, flex and entry are the .ui files. They all contain this part at the end (don't know if it helps):
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
And finally, the setup file I run with cx_Freeze:
import sys
from cx_Freeze import setup, Executable
import matplotlib
import numpy
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os", "matplotlib"], "includes": ["PyQt5", "atexit"], "excludes": ["tkinter"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "Flexicounts",
version = "0.1",
description = "Flexicounts pour faire tes comptes facilement",
options = {"build_exe": build_exe_options},
executables = [Executable("flexicounts.py", base=base)])
I read a lot about it, but I feel that there is no "miracle" solution...
Could you help me freeze my app and make it run on a "virgin machine" ?
You might be facing Issue #504 of the cx_Freeze repository. In that case, quoting a comment by marceloduarte there:
The workaround is to copy python3.dll to the directory where the executable was made. cx_Freeze copies python37.dll, but does not copy python3.dll. The vcruntime140.dll may also have to be copied when it no longer exists on the system.
First try to manually copy these DLLs (you find them in the directory of your python installation containing python.exe) to the directory of your executable. If that solves the problem, you can tell cx_Freeze to do it for you by using the include_files list of the build_exe_options. Modify you setup script as follows:
import os
python_dir = os.path.dirname(sys.executable) # directory of your python installation
build_exe_options = {"packages": ["os", "matplotlib"],
"includes": ["PyQt5", "atexit"],
"include_files": [os.path.join(python_dir, "python3.dll"), os.path.join(python_dir, "vcruntime140.dll")],
"excludes": ["tkinter"]}
Maybe you need to copy further DLLs, such as msvcp140.dll, or any other DLL present inside the site-packages/PyQt5 directory (including subdirectories) of your Python installation.
I faced a similar problem recently, with the following versions:
python 3.6.6 x64
cx-Freeze==6.10
PyQt5==5.15.4
PyQt5-Qt5==5.15.2
PyQt5-sip==12.9.0
PyQt5-stubs==5.15.2.0
PyQtWebEngine==5.15.5
PyQtWebEngine-Qt5==5.15.2
Symptoms :
The cx_Freeze package was successful and execution working fine on my machine, because (as I found later and explained below) I had Python 3.6 x64 installed on my machine and visibile via environement variables.
On an another machine, the package failed with the exact same error on first PyQt5 import:
ImportError: DLL load failed: The specified module could not be found
However, in my case, all the necessary dlls seemed all in the right place :
cx_Freeze seemed to have correctly put python3.dll and python36.dll next to the executable. But no other dll were copied there (no vcruntime140.dll for instance)
all the necessary python modules were in place, including PyQt5 and all its dlls
Solution that was working :
(I created a cx_Freeze issue)
Contrary to the other answer, I had to copy
python3.dll
(either from <cx_freeze_build_dir_target> or from python3.6.6 install dir ...)
(python36.dll works too but is much bigger)
into the following folders:
<cx_freeze_build_dir_target>/lib/PyQt5/
The corresponding cx_Freeze setup config for this would be to add this to the include_files list, in the following fashion.
Unfortunatly, this does not work due to cx_Freeze having an sort of exception file liste that includes python3.dll and prevents the actual copy via include_files. So, the copy should be performed manually, after full setup script execution...
# import pkgutil
from pathlib import Path
from glob import glob
import sys
# ... your stuff
#
includes_list = []
# your stuff
# ...
# pyqt5 force copy of pythonlib(s) into qt5 target dir
# may not work if you intend to zip pyqt5 ?
python_dir = Path(sys.executable).parent
python_dlls = [ Path(p) for p in glob(f"{python_dir.as_posix()}/python*.dll")]
pyqt_base_dir = Path("lib", "PyQt5")
# prepare cx_Freeze tuples (source file, target dir)
includes_list+= [ (p, pyqt_base_dir / p.name) for p in python_dlls ]
build_exe_options = {"packages" : ..., # your packages
"includes" : ..., # yours
"include_files": includes_list,
"excludes" : ... # yours
}

Importing submodules

I am new to python and i m having a really bad time to overcome a problem with the importing system.
Lets say i have the file system presented below:
/src
/src/main.py
/src/submodules/
/src/submodules/submodule.py
/src/submodules/subsubmodules
/src/submodules/subsubmodules/subsubmodule.py
All the folders (src, submodules, subsubmodules) have and empty __init__.py file.
In submodule.py i have:
from subsubmodules import subsubmodule
In main.py i have:
from submodules import submodule
When i run submodule.py python accepts the import. But when i run main.py python raises error for the import of subsubmodule.py because /src/submodules/subsubmodules/ folder is not in the path.
Only solution is to change the import of submodule.py to
from submodules.subsubmodules import subsubmodule
This seems to me as an awful solution because after that i cannot run submodule.py and i m sure that something else is the key to that.
An other solution is to add the following code to the __init__.py file:
import os
import sys
import inspect
cmd_subfolder = os.path.split(inspect.getfile(inspect.currentframe()))[0]
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
Is there any way to do this using just the importing system of python and not other methods that do it manually using, for example sys.path or other modules like os, inspect etc..?
How can i import modules without caring about the modules they import?
You can run subsubmodule.py as
python3 -m submodule.subsubmodules.subsubmodule
If you want a shorter way to invoke it, you're free to add a shell or Python script for that on the top level of your package.
This is how imports work in Python 3; there are reasons for that.
You can avoid this issue by using sys.path in your program.
sys.path.insert(0, './lib')
import subsubmodule
For this code, you can put all your imports to a lib folder.
You can read the official documentation on Python packages where this is explained in depth.

Comand promp closes instantly with cx_Freeze and requests module

Im trying to build a simple executable file from python using cx_Freeze, but the script uses request module. As many many other threads say, it seems to be a problem involving cx_freeze and requests module, something about the path of the files that requests need to run and cx_freeze changes or doesnt import when freezing.
The build process works just fine, but when the exe is created, if i try to open it, a comand promp shows for a fraction of a second and then closes, displaying something so quickly that i dont have time to read or even snapshot, but it doesnt have the structure of an error message.
I think the problem is probably the thing about paths, but i dont know how to solve it, and everything i've found in internet haven't worked for me.
Please help.
Here is a copy of the test file named "prueba2.py"
import requests
print("hi")
print(requests)
input()
and the setup.py
from cx_Freeze import setup, Executable
import sys
import os
import requests.certs
base = None
executables = [Executable("prueba2.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages': packages,
'include_files': [os.path.join(sys.base_prefix, 'DLLs','sqlite3.dll'),
(requests.certs.where(), 'cacert.pem')]
},
}
setup(
name="<any name>",
options=options,
requires=["requests"],
version="<any number>",
description='<any description>',
executables=executables
)
It seems there are some dependencies for cx_freeze in new versions of the module
try modifying the main file like this:
import requests
from multiprocessing import Queue
print("hi")
print(requests)
input()
and setup.py as :
from cx_Freeze import setup, Executable
import sys
import os
import requests.certs
base = None
executables = [Executable("prueba2.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages': packages,
'include_files': [os.path.join(sys.base_prefix, 'DLLs', 'sqlite3.dll'),
(requests.certs.where(), 'cacert.pem')]
},
}
setup(
name="name",
options=options,
requires=["requests"],
version="1",
description='test',
executables=executables
)

Py2exe not working for Python 3.5.1

Is Py2exe just not supported for Python3 or is there something wrong with
build_exe my_script.py
The imports I made in my_script.py are as follows:
import os
import sys
import getpass
import hashlib
import platform
import base64
from cryptography.fernet import Fernet
But according to the documentation on Py2exe.org they say that they use an automatic module finder so you needn't worry about specific imports or whatever.
Can't figure out why I keep getting these errors
Unfortunately as of November 2017 there is no Python 3.5 support for py2exe.

Resources