exe conversion of python 2.7 script - python

I have written a python script in python 2.7 and I am facing a problem after exe conversion of this file using py2exe package. I have got the exe file but when i run, i got an error which i have mentioned below. Please note that I have saved the script file with name test.py. Please tell me the problem #Bryan Oakley
from Tkinter import *
import os
from os.path import expanduser
import time
import datetime
from docx import Document
from docx.shared import Inches
document = Document()
docx_prefix = "Doc_File_"
home_directory = expanduser("~")
folder_directory = home_directory + "\myapp"
doc_file_directory = folder_directory + "/"
if not os.path.exists(folder_directory):
os.makedirs(folder_directory)
def create_file():
now = datetime.datetime.now()
current_time = now.strftime("%Y-%m-%d %H%M%S")
file_name = docx_prefix + current_time
document = Document()
document.add_paragraph("word document")
document.save(doc_file_directory + file_name + '.docx')
main_root = Tk()
main_root.title("Toolbar")
main_root.geometry('105x26+1+1')
toolbar = Frame(main_root, bg="gray")
toolbar.pack(side="top")
create_file_button = Button(toolbar, text="Create File", command =
create_file, bg="gray", width=10)
create_file_button.pack(side="left")
main_root.mainloop()
Setup file for exe conversion using py2exe :
from distutils.core import setup
import py2exe
setup(
console=['test.py'],
options = {'py2exe': {'packages' : ['docx']}}
)
I am getting the following error after exe conversion:
C:\Python27\Lib\site-packages\py2exe\samples\scripting\dist>test.exe
Traceback (most recent call last):
File "test.py", line 6, in <module>
File "docx\__init__.pyc", line 3, in <module>
File "docx\api.pyc", line 14, in <module>
File "docx\package.pyc", line 11, in <module>
File "docx\opc\package.pyc", line 12, in <module>
File "docx\opc\part.pyc", line 12, in <module>
File "docx\opc\oxml.pyc", line 12, in <module>
File "lxml\etree.pyc", line 12, in <module>
File "lxml\etree.pyc", line 10, in __load
File "src/lxml/lxml.etree.pyx", line 92, in init lxml.etree
(src\lxml\lxml.etree.c:225327)
ImportError: cannot import name _elementpath

Module lxml isn't pure Python. It relies on DLLs written in C. You need to add etree.pyd and objectify.pyd in the py2exe package. They are in site-packages\lxml.

Related

some problems of using Python's Cryptography on Windows

I would like to use the cryptography's module on Windows, I am using version 1.12 and Python 3.10.6, and I have some problems. Actually, when I run my script, I receive the error :
`
Traceback (most recent call last):
File "C:/Users/edoua/Documents/cryptographie.py", line 1, in <module>
from cryptography.fernet import Fernet
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\fernet.py", line 16, in <module>
from cryptography.hazmat.primitives.hmac import HMAC
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\hazmat\primitives\hmac.py", line 10, in <module>
from cryptography.hazmat.backends.openssl.hmac import _HMACContext
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\hazmat\backends\openssl\__init__.py", line 6, in <module>
from cryptography.hazmat.backends.openssl.backend import backend
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 13, in <module>
from cryptography import utils, x509
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\x509\__init__.py", line 7, in <module>
from cryptography.x509.base import (
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\x509\base.py", line 28, in <module>
from cryptography.x509.extensions import (
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\x509\extensions.py", line 25, in <module>
from cryptography.x509.general_name import (
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\x509\general_name.py", line 9, in <module>
from email.utils import parseaddr
File "C:\Users/edoua/Documents\email.py", line 18, in <module>
server.login(email_address, email_password)
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 739, in login
(code, resp) = self.auth(
File "C:\Users\edoua\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 641, in auth
response = encode_base64(initial_response.encode('ascii'), eol='')
NameError: name 'encode_base64' is not defined
`
It is coming from the smtplib.py file. Also, I precise that the module works perfectly, I can use all the functions of the module base64. So the problem is coming from the word 'encode_base64'. Please help !
I tried to launch my program, and that error occures, I expected that it was going to work.
You have a local module C:\Users/edoua/Documents\email.py that is shadowing the email module from the Python standard library. Rename email.py to something else, or move it to somewhere that isn't on the Python path.

Unable to use moviepy library

I am writing a program to convert video to audio using moviepy library. Problem is that I have installed this library import moviepy.editor as mp
but when I run my program I am facing an error as follows:
PS D:\MPhil CS\Semester 2\Multimedia Retrieval Techniques\Assignment1\MRT_Assignment1> &
'C:\Users\User\AppData\Local\Programs\Python\Python39\python.exe'
'c:\Users\User\.vscode\extensions\ms-python.python-
2021.3.658691958\pythonFiles\lib\python\debugpy\launcher' '61484' '--' 'd:\MPhil CS\Semester
2\Multimedia Retrieval Techniques\Assignment1\MRT_Assignment1\mrt_retreival.py'
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\IPython\utils\timing.py", line 27, in <module>
import resource
ModuleNotFoundError: No module named 'resource'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\MPhil CS\Semester 2\Multimedia Retrieval
Techniques\Assignment1\MRT_Assignment1\mrt_retreival.py", line 5, in <module>
import moviepy.editor as mp
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\editor.py", line
56, in <module>
from .video.io.html_tools import ipython_display
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\moviepy\video\io\html_tools.py", line 22, in <module>
from IPython.display import HTML
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\IPython\__init__.py",
line 49, in <module>
from .terminal.embed import embed
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\IPython\terminal\embed.py", line 19, in <module>
from IPython.terminal.ipapp import load_default_config
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\IPython\terminal\ipapp.py", line 30, in <module>
from IPython.core.magics import ScriptMagics
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\IPython\core\magics\__init__.py", line 21, in <module>
from .execution import ExecutionMagics
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\IPython\core\magics\execution.py", line 46, in <module>
from IPython.utils.timing import clock, clock2
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-
packages\IPython\utils\timing.py", line 64, in <module>
clocku = clocks = clock = time.clock
AttributeError: module 'time' has no attribute 'clock'
I am using VScode and my python version is 3.9.0. My code is as follows:
from pytube import YouTube
from youtube_transcript_api import YouTubeTranscriptApi
import ffmpeg
import moviepy.editor as mp
# where to save
SAVE_PATH = "D:\\MPhil CS\\Semester 2\\Multimedia Retrieval
Techniques\\Assignment1\\MRT_Assignment1\\Video" #to_do
# link of the video to be downloaded
link="https://www.youtube.com/watch?v=2DFzxsy3PaM"
try:
# object creation using YouTube
# which was imported in the beginning
yt = YouTube(link)
except:
print("Connection Error") #to handle exception
try:
print("*********************** Video Title ***************************")
print(yt.title)
print("\n")
print("*********************** Video Subtitles
***************************")
srt = YouTubeTranscriptApi.get_transcript("2DFzxsy3PaM")
with open("subtitles_file.txt", 'w') as f:
f.writelines('%s\n' % i for i in srt)
print("Subtitles written!!")
print("\n")
print("Downloading start...")
yt.streams.filter(file_extension= 'mp4').first().download(SAVE_PATH)
print("Video Downloaded!!")
my_clip = mp.VideoFileClip(r"D:\\MPhil CS\\Semester 2\\Multimedia
Retrieval Techniques\\Assignment1\\MRT_Assignment1\\Video\\A fight
against the norm - 1 minute spoken word poetry for introverts.mp4")
my_clip.audio.write_audiofile(r"D:\\MPhil CS\\Semester 2\\Multimedia
Retrieval Techniques\\Assignment1\\MRT_Assignment1\\Video\\abc.mp3")
print("Audio Downloaded!!")
except:
print("Some Error!")
print('Task Completed!')
I am new to Python and I am unable to resolve this. Thanks in advance for help.
AttributeError: module 'time' has no attribute 'clock'
If you are using Python 38 and above, time. clock() is deprecated from the version (https://bugs.python.org/issue31803)
So you can use time.perf_counter() instead of time.clock()
The error is happening inside import moviepy.editor as mp so the rest of your code is irrelevant.
It seems to be a bug in the IPython package being called by moviepy.editor. Check that it is fully up-to-date.
Alternatively you can avoid moviepy.editor entirely and do from moviepy.video.io.VideoFileClip import VideoFileClip.

No module named QtOpenGL

I have the following error while running a python package cvu(connectome visualization utility):
Traceback (most recent call last):
File "/usr/local/bin/cvu", line 4, in <module>
__import__('pkg_resources').run_script('cvu==0.5.2', 'cvu')
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 657, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 1437, in run_script
exec(code, namespace, namespace)
File "/usr/local/lib/python2.7/dist-packages/cvu-0.5.2-py2.7.egg/EGG-INFO/scripts/cvu", line 33, in <module>
from cvu.main import main
File "/usr/local/lib/python2.7/dist-packages/cvu-0.5.2-py2.7.egg/cvu/__init__.py", line 20, in <module>
from .main import load_adj, load_parc, usage, main
File "/usr/local/lib/python2.7/dist-packages/cvu-0.5.2-py2.7.egg/cvu/main.py", line 20, in <module>
from gui import CvuGUI,ErrorHandler
File "/usr/local/lib/python2.7/dist-packages/cvu-0.5.2-py2.7.egg/cvu/gui.py", line 24, in <module>
from dataset import Dataset
File "/usr/local/lib/python2.7/dist-packages/cvu-0.5.2-py2.7.egg/cvu/dataset.py", line 26, in <module>
from dataview import (DataView,DVMayavi,DVMatrix,DVCircle)
File "/usr/local/lib/python2.7/dist-packages/cvu-0.5.2-py2.7.egg/cvu/dataview.py", line 28, in <module>
from chaco.api import (Plot,ArrayPlotData,ColorMapper,PlotGraphicsContext)
File "/usr/lib/python2.7/dist-packages/chaco/api.py", line 37, in <module>
from abstract_plot_renderer import AbstractPlotRenderer
File "/usr/lib/python2.7/dist-packages/chaco/abstract_plot_renderer.py", line 7, in <module>
from plot_component import PlotComponent
File "/usr/lib/python2.7/dist-packages/chaco/plot_component.py", line 4, in <module>
from enable.api import Component
File "/usr/lib/python2.7/dist-packages/enable/api.py", line 21, in <module>
from markers import MarkerTrait, marker_trait, MarkerNameDict, marker_names, \
File "/usr/lib/python2.7/dist-packages/enable/markers.py", line 18, in <module>
from compiled_path import CompiledPath
File "/usr/lib/python2.7/dist-packages/enable/compiled_path.py", line 17, in <module>
from toolkit import toolkit_object
File "/usr/lib/python2.7/dist-packages/enable/toolkit.py", line 47, in <module>
_init_toolkit()
File "/usr/lib/python2.7/dist-packages/enable/toolkit.py", line 40, in _init_toolkit
format_exception_only(t, v))
ImportError: Unable to import the image backend for the qt4 toolkit (reason: ['ImportError: No module named QtOpenGL\n']).
I have installed this
apt-get install python-qt4-gl
but stil I have the same error.
I don't know what is wrong.
Thanks for any guide.
edit
this is the script that run the pakcage:
run.py
#!/usr/bin/env python
import os
import sys
#pyside can cause problems with old versions of enable
#however, forcing traits to use pyqt causes problems in anaconda
#We will let the user set this optionally, it is good for debugging to not set.
#os.environ['QT_API']='pyqt'
#os.environ['QT_API']='pyside'
#One or the other should usually be set so that matplotlib and pyface can
#communicate and use the right Qt backend
#pyside is a better default choice
scriptdir=os.getcwd()
python_cmd = 'pythonw' if sys.platform=='darwin' else 'python'
#arguments = '%s %s'%(scriptdir, " ".join(sys.argv[1:]))
#exec_cmd = "%s main.py %s" % (python_cmd, arguments)
arguments = '%s main.py %s %s' % (python_cmd, scriptdir, " ".join(sys.argv[1:]))
try:
#see if cvu extracted locally
os.chdir(os.path.join(os.path.dirname(os.path.dirname(
os.path.realpath(__file__))),'cvu'))
os.system(arguments)
except OSError:
#see if cvu installed to system python
from cvu.main import main
sys.argv = arguments.split()
main()

py2exe with wxpython and wxpython DLL load failed

I want to use py2exe to pack a python file.
from distutils.core import setup
from glob import glob
import scipy
import numpy
import py2exe
import matplotlib
import wx
import sys
setup(
windows=["practice.py"],
options={"py2exe":
{"dll_excludes":["libiomp5md.dll",
"OLEAUT32.dll",
"USER32.dll",
"IMM32.dll",
"ole32.dll",
"MSVCP90.dll",
"SHELL32.dll",
"RPCRT4.dll",
"COMDLG32.dll",
"WSOCK32.dll",
"COMCTL32.dll",
"ADVAPI32.dll",
"WS2_32.dll",
"WINSPOOL.DRV",
"GDI32.dll",
"WINMM.dll",
"KERNEL32.dll",
"msvcm90.dll",
"msvcp90.dll",
"msvcr90.dll",
'libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll'],
"includes" : [ "matplotlib.backends",
"matplotlib.backends.backend_qt4agg",
"pylab",
"numpy",
"wx",
"scipy",
"matplotlib.backends.backend_tkagg"],
'excludes':['_gtkagg',
'_tkagg',
'_agg2',
'_cairo',
'_cocoaagg',
'_fltkagg',
'_gtk',
'_gtkcairo']
}
},
data_files=matplotlib.get_py2exe_datafiles()
)
However,when I open the .exe, it reported as following
Traceback (most recent call last):
File "practice.py", line 1, in <module>
File "matplotlib\__init__.pyc", line 180, in <module>
File "matplotlib\cbook.pyc", line 33, in <module>
File "numpy\__init__.pyc", line 168, in <module>
File "numpy\add_newdocs.pyc", line 13, in <module>
File "numpy\lib\__init__.pyc", line 18, in <module>
File "numpy\lib\polynomial.pyc", line 19, in <module>
File "numpy\linalg\__init__.pyc", line 50, in <module>
File "numpy\linalg\linalg.pyc", line 29, in <module>
File "numpy\linalg\lapack_lite.pyc", line 12, in <module>
File "numpy\linalg\lapack_lite.pyc", line 10, in __load
ImportError: DLL load failed: can't find specific modulus.
64bit windows8 python2.7.9
I have searched in Stackoverflow and find some answers, however, it doesn't work.
Is your numpy installation ok?
"numpy\linalg\linalg.pyc", line 29 is trying to load
from numpy.linalg import lapack_lite, _umath_linalg
In my installation, I have lapack_lite.pyd and _umath_linalg.pyd there.
But it looks like your numpy is somehow expecting lapack_lite.pyc. I do not know why they are different but perhaps re-installing the numpy will help?
Numpy usually behaves very well with py2exe.

cx_freeze fails to bundle django.utils.six.moves

I am trying to bundle django with cx_freeze. With my setup.py I am able to bundle it together but when I invoke the generated executable I get the below import error. I have tried various ways to fix this but unable to get a way around.
Versions used:
Django==1.6.4
cx-Freeze==4.3.3
./build/exe.linux-x86_64-2.7/script
Traceback (most recent call last):
File "lib/python2.7/site-packages/cx_Freeze/initscripts/Console.py", line 27, in <module>
exec(code, m.__dict__)
File "script.py", line 7, in <module>
from models import Project
File "/remote/vgrnd77/pritam/tryout/package/models.py", line 6, in <module>
from django.db.models import CharField, Model, \
File "lib/python2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
from django.db.models.query import Q
File "lib/python2.7/site-packages/django/db/models/query.py", line 14, in <module>
from django.db.models.fields import AutoField
File "lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 15, in <module>
from django import forms
File "lib/python2.7/site-packages/django/forms/__init__.py", line 8, in <module>
from django.forms.fields import *
File "lib/python2.7/site-packages/django/forms/fields.py", line 17, in <module>
from django.forms.util import ErrorList, from_current_timezone, to_current_timezone
File "lib/python2.7/site-packages/django/forms/util.py", line 4, in <module>
from django.utils.html import format_html, format_html_join
File "lib/python2.7/site-packages/django/utils/html.py", line 12, in <module>
from django.utils.text import normalize_newlines
File "lib/python2.7/site-packages/django/utils/text.py", line 11, in <module>
from django.utils.six.moves import html_entities
ImportError: cannot import name html_entities
Here is my sample script:
#!/usr/bin/env python
import os
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from models import Project
if __name__ == '__main__':
print "My first exe"
p = Project.objects.all()
for project in p:
print project.name
Here is how the setup.py looks like:
main_python_file = "script.py"
import sys
from cx_Freeze import setup, Executable
# html_entities is missing
base = 'Console'
buildOptions = dict(
create_shared_zip = False,
append_script_to_exe = False,
packages = [],
includes = ['django'],
excludes = ['tkinter']
)
setup(
name = "my_exe",
version = "0.1",
description = "My first exe",
options = dict(build_exe = buildOptions),
executables = [Executable(main_python_file, base = base)])
Here is how the project table looks like:
class Project(Model):
"""
Project table
"""
name = CharField(max_length=255)
Has anyone faced and solved this ?
Freeze Output shows it was not able to figure out django.utils.six.moves. It shows 'six' in missing modules:
Missing modules:
? django.utils.six.moves imported from django.db.backends, django.db.models.base, django.db.models.sql.where, django.dispatch.dispatcher, django.forms.formsets, django.http.cookie, django.http.response, django.utils.crypto, django.utils.functional, django.utils.html_parser, django.utils.ipv6, django.utils.regex_helper, django.utils.text
? django.utils.six.moves.urllib.parse imported from django.core.files.storage, django.core.validators, django.forms.fields, django.forms.widgets, django.http.request, django.http.response, django.utils.encoding, django.utils.html, django.utils.http
If you look in the django.utils.six file you will find the following code:
...
MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
MovedModule("http_cookies", "Cookie", "http.cookies"),
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
MovedModule("html_parser", "HTMLParser", "html.parser"),
...
What django does dynamically is build up the django.utils.six.moves module out of thin air. The parameters to MovedModule are . So the solution in python33 (the one I used) is to import html.entities in my Executable module. I tried to add it to the includes for the executable, but that didn't seem to work. I assume you could use the import htmlenditydefs for the python2 version.

Categories

Resources