I made an exe file by using py2exe (python2).
My partial code of hello.py
from PyQt4 import QtGui, QtCore
from new_ui import *
.....
global dir_name
dir_name = dir_name.encode(sys.stdout.encoding)
...
and new_ui.py has dir_name variable.
When I start my main.py, it works. But if I complie my code into .exe file, it doesn't work. Here's my setup.py files.
from distutils.core import setup
import py2exe, glob
setup(
console=[
{'script':'hello.py','icon_resources':[(1, 'cal.ico')]}
],
data_files=[ ('C:\\Users\\Ko\\Desktop\\python exe\\'+u'가계부\\'+'imageformats',glob.glob('C:\\Python27\\Lib\\site-packages\\PyQt4\\plugins\\imageformats\\*.*')) ],
options = {'py2exe': {
'bundle_files':3,
'dist_dir':'C:\\Users\\Ko\\Desktop\\python exe\\'+u'가계부',
'includes': ['sip','PyQt4.QtNetwork','new_ui'],
"dll_excludes": ["MSVCP90.dll","w9xpopen.exe"]
}},
zipfile = None
)
All I can see is this error message.
global name 'dir_name' is not defined
What should I do?
Related
Using VSCode v1.61.0
I have a python package, a single folder named "tfmodules" with a whole lotta modules, and in the init.py I'm doing this:
from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
In my code I import it like so:
from tfmodules import *
Then I call the modules with something like this:
def write(aci_topology_model, provider_source, provider_version):
with open('fabric-bringup.tf', 'w') as f:
x = fabricbringup.tf_write_fabric_bringup(aci_topology_model, provider_source, provider_version)
f.write(x)
fabricbringup is a module in the package.
The code works just fine, but Pylance is throwing 36 reportUndefinedVariable instances, one for each module called.
The specific error (from one of the modules) is:
"fabricbringup" is not defined Pylance(reportUndefinedVariable)
I have selected the proper interpreter for the venv in vs code, and I tried adding this to the settings.json file:
"python.analysis.extraPaths": ["tfmodules", "/home/aj/projects/python/dcnet-aci-lld-convert/tfmodules"]
But I still get the errors from Pylance. What am I doing wrong? Thanks!
Considering you will not be adding any extensions at runtime how about creating a file that will generate all references directly to "__init__.py"?
if __name__ == "__main__":
from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
print(join(dirname(__file__), "*.py"))
__all__ = ["\t\"" +basename(f)[:-3]+"\",\n" for f in modules if isfile(f) and not f.endswith('__init__.py') and not f.endswith('__add__manager.py')]
f = open(join(dirname(__file__), "__init__.py"), "a")
f.write("__all__ = [\n")
f.writelines(__all__)
f.write("]\n")
f.close()
Result will look something like this:
#(previous contents of __init__.py)
__all__ = [
"file1",
"file2",
]
I'm having troubles with praw, cx_freeze, pyside and requests, before freezing everything works fine, but when i freeze this happens, something with requests goes wrong i think:
http://pastie.org/10614254
This is the project that i'm working with: https://github.com/MrCappuccino/WallDit-QT
This is my setup.py: https://gist.github.com/MrCappuccino/0f1b0571d29d47a95895
import sys
import cx_Freeze
import PySide
import praw
import requests.certs
from cx_Freeze import setup, Executable
exe = Executable(
script="WallDit_QT.py",
base="Win32GUI",
targetName="WallDit_QT.exe"
)
#includefiles = ['README.txt', 'CHANGELOG.txt', 'helpers\uncompress\unRAR.exe', , 'helpers\uncompress\unzip.exe']
#build_exe_options = {"packages": ["os"], "includefiles": ['README.txt', 'CHANGELOG.txt']}
setup(name = 'WallDit_QT',
version = '1.0',
author = 'Disco Dolan',
description ='Set your wallpaper interactively!',
executables = [exe],
options = {'build.exe': {"include_files":['cacert.pem', 'praw.ini', 'README.md']}},
requires = ['PySide', 'cx_Freeze', 'praw', 'shutil', 'requests']
)
could anybody help out?
I have tried adding cacert.pem, to no avail, at this point i have no more ideas
For some frozen applications, you have to set the the cacert (or external data in general) path inside the frozen applications.
Setup.py Section
You first need to include it in your build options and manually specify the installation directory. This is the only part that goes inside the setup.py:
# notice how I say the folder the certificate is installed
{"include_files":[(requests.certs.where(),'cacert.pem')]}
In your case, this produces the following setup file:
import requests
import sys
# more imports
setup(name = 'WallDit_QT',
version = '1.0',
author = 'Disco Dolan',
description ='Set your wallpaper interactively!',
executables = [exe],
options = {
'build.exe': {
"include_files": [
(requests.certs.where(),'cacert.pem'),
'praw.ini',
'README.md'
]
}
},
requires = ['PySide', 'cx_Freeze', 'praw', 'shutil', 'requests']
)
Application Section
You then need to get the certificate path at runtime inside the frozen application.
For PyInstaller, a path is defined at runtime to the data directory called _MEIPASS (which can be gotten from sys._MEIPASS), allowing you to access all the data required for the application. In the case of cacert.pem, the path would be determined as follows:
cacertpath = os.path.join(sys._MEIPASS, "cacert.pem")
For cx_Freeze, the path can be determined from the path of the installation and joining it with the data desired. Here, we get the path as follows:
cacertpath = os.path.join(datadir, 'cacert.pem')
You can get the data directory easily for frozen applications with the following:
datadir = os.path.dirname(sys.executable)
(Please note that this won't work with a non-frozen application, so to ensure it works for both frozen and non-frozen applications, Cx_Freeze recommends you code it as follows):
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
You then include this path all your requests module GET and POST requests as follows:
request.get(url, headers=headers, verify=cacertpath)
Example 1
An example code snippet would be as follows:
# load modules
import os
import sys
import requests
# define our path finder
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
# get our cacert path and post our GET request
cacertpath = find_data_file('cacert.pem')
r = requests.get('https://api.github.com/events', verify=cacertpath)
# print the text from the request
print(r.text)
Example 2
You can also tell requests where to find the certificate in the future by doing the following:
os.environ["REQUESTS_CA_BUNDLE"] = cacertpath
In this case, we would do the following. The advantage here is that the cacertpath does not to be explicitly defined in every module (or imported from another module) and can be defined in the environment.
import os
import sys
import requests
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
cacertpath = find_data_file('cacert.pem')
os.environ["REQUESTS_CA_BUNDLE"] = cacertpath
r = requests.get('https://api.github.com/events')
r.text
I have problem with playing .wav sound with QSound.play() after compiling to exe (I'm using Python 3.4.3, PyQt 5.4.1 and py2exe 0.9.2.0).
setup.py code:
from distutils.core import setup
import py2exe
setup(
windows=[
{
"script": "main_app.py",
"icon_resources": [(0, "favicon163248.ico")]
}
],
data_files=[
(
'sounds', ['sounds\Siren.wav']
)
],
options={"py2exe": {"includes": ["sip"], "dist_dir": "MyProject"}}
)
What have i tried:
Relative path
sound = QSound("sounds/Siren.wav")
sound.play() #works when simply running, doesn't work when compiling to exe
Path to executable file (main_app.exe)
sound = QSound(os.path.dirname(sys.executable) + "\sounds\Siren.wav")
sound.play() #doesn't work when compiling to exe
Absolute path
sound = QSound("C:\\path\\to\\project\\MyProject\\sounds\\Siren.wav")
sound.play() #works when simply running, doesn't work when compiling to exe
Resources
resources_qrc.qrc code:
<RCC>
<qresource prefix="media">
<file>Siren.wav</file>
<file>favicon163248.ico</file>
</qresource>
</RCC>
then converted to resources.py with pyrcc5
from resources import *
...
sound = QSound(':/media/Siren.wav')
sound.play() #works when simply running, doesn't work when compiling to exe
Copy form resource to hard drive on the fly
QFile.copy(":/media/Siren.wav", "sounds/Siren.wav")
sound = QSound("sounds/Siren.wav")
sound.play() #still doesn't work after making exe!
After spending pretty much time on it, I gave up.
Any help would be appreciated.
I use python 2.7 and cx_Freeze 4.3.1 and PyQt4
#-*- coding: utf-8-*-
__author__ = 'Aaron'
from cx_Freeze import setup, Executable
import sys
if sys.platform == "win32":
base = "Win32GUI"
includefiles= ['icons','Sound','imageformats']
includes = ['sip', 'PyQt4.QtCore']
setup(
name = u"Your Programe",
version = "1.0",
description = u"XXXX",
options = {'build_exe': {'include_files':includefiles}},
executables = [Executable("Your programe name.py" ,base = base, icon = "XXX.ico")])
I had the same problem too, with Python 3.4.3, PyQt 5.5.1, py2exe 0.9.2.2.
Problem is not in wrong file path.
If you call:
QAudioDeviceInfo.availableDevices(QAudio.AudioOutput)
from .exe, the returned list will be empty.
You should add foder: "\audio" from "site-packages\PyQt5\plugins" to the directory with your output .exe file, and sound will work.
Here is my setup.py file:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from distutils.core import setup
import py2exe
import sys
import os, os.path
import zipfile
import shutil
def get_exe_name(f):
dirname, filename = os.path.split(os.path.abspath(f))
return os.path.split(dirname)[-1].lower()
def get_name(name):
idx = name.find('.')
if idx != -1:
return name[:idx]
return name
def build(__version__, __appname__, main_module = 'main.py', dest_base='main', icon='images\\main.ico'):
#exec('from ' + get_name(main_module) + ' import __version__, __appname__')
try:
shutil.rmtree('dist')
except:
print ('Cann\'t remove "dist" directory: {0:s}'.format(str(sys.exc_info())))
if len(sys.argv) == 1:
sys.argv.append('py2exe')
options = {'optimize': 2,
# 'bundle_files': 0, # create singlefile exe 0
'compressed': 1, # compress the library archive
'excludes': ['pywin', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'pywin.dialogs.list', 'os2emxpath', 'optparse', 'macpath', 'tkinter'],
'dll_excludes': ['w9xpopen.exe', 'mapi32.dll', 'mswsock.dll', 'powrprof.dll', 'MSVCP90.dll', 'HID.DLL'], # we don't need this
'includes': ['sip', 'locale', 'calendar', 'logging', 'logging.handlers', 'PyQt5', 'PyQt5.QtCore', 'PyQt5.QtGui', 'PyQt5.QtMultimedia', 'PyQt5.QtNetwork', 'PyQt5.QtPrintSupport'],
}
#datafiles = [('platforms', ['C:\\Python34\\Lib\\site-packages\\PyQt5\\plugins\\platforms\\qwindows.dll']),]
import PyQt5
datafiles = [('platforms', [PyQt5.__path__[0] + '\\plugins\\platforms\\qwindows.dll']), ('audio', [PyQt5.__path__[0] + '\\plugins\\audio\\qtaudio_windows.dll']),]
dirs = ['images', 'docs', 'ssl', 'sounds', 'p2ini', 'lib', 'config']
for d in dirs:
try:
for f in os.listdir(d):
f1 = d + '\\' + f
if os.path.isfile(f1): # skip directories
datafiles.append((d, [f1]))
except:
print ('Cann\'t find some files in directory "{0:s}": {1:s}'.format(d, str(sys.exc_info())))
setup(version = __version__,
description = __appname__,
name = '{0:s} {1:s} application'.format(__appname__, __version__),
options = {'py2exe': options},
zipfile = None,
data_files = datafiles,
windows = [{'icon_resources':[(1,'images\\main.ico')], 'script':main_module, 'dest_base':dest_base}] if icon else [{'script':main_module, 'dest_base':dest_base}],
scripts = [main_module],
#console = [{'icon_resources':[(1,'images\\main.ico')], 'script':main_module, 'dest_base':dest_base}] if icon else [{'script':main_module, 'dest_base':dest_base}],
)
And example code for call setup.py functions:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from setup import get_name, get_exe_name, build
MAIN_MODULE = 'main.py'
exec('from ' + get_name(MAIN_MODULE) + ' import __version__, __appname__')
build(__version__, __appname__, MAIN_MODULE, dest_base=get_exe_name(__file__))
In mainwindow.py file i have use this line,
ctypes.windll.LoadLibrary("ptzdll/PTZ.dll")
from PTZ import PTZDevice, PTZType
When i execute mainwindow.py file with terminal(python mainwindow.py), it works, but when i double click on exe file made by py2exe, it gives error:
**No module named PTZ.**
My setup.py file code:
from distutils.core import setup
from glob import glob
import py2exe
from distutils.filelist import findall
import matplotlib
import cv2
import numpy
import tkMessageBox
import easygui
import os
datafiles = [
("resources", glob('resources/*.*'))
]
datafiles.append(('ptzdll', [
'PTZ.dll',
'DirectShowLib-2005.dll'
]))
setup(
console=['mainwindow.py'],
#'file_resources': [bitmap_string[:-1]],
options = {
'py2exe': {
'packages' : ['matplotlib','ctypes','_ctypes'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll'],
r'includes': [r'scipy.sparse.csgraph._validation',
r'scipy.special._ufuncs_cxx']
}
},
zipfile = None,
windows = [{
"script":"mainwindow.py",
"icon_resources":[(1, "tfr.ico")],
}],
data_files = datafiles,
#data_files = datafiles
)
The PTZ.dll is not in your *\dist\ptzdll folder, but in *\dist\library.zip\ptzdll\PTZ.dll.
It is the first time I'm using cx_freeze and I really need help on this, I've been looking everywhere but i can't find an answer.
I don't have any error during the compiling process, but the executable file just doesn't do anything. Could someone please explain me why?
Here is my project structure:
Application
setup.py
application.py (Tkinter app that import 2 functions from cell.py)
logo.jpg
favicon.ico
responsible.xls
modules:
cell.py (all the work is done in this file)
I don't know where to include the file cell.py in setup, for now it's in a file Modules. And i'm not sure if Tkinter should be include or exclude.
Here is the file setup.py:
import sys
from cx_Freeze import setup, Executable
executables = [
Executable("application.py")
]
buildOptions = dict(
compressed = True,
includes = ["sys","re","PIL","ttk","xlrd","xlutils","datetime","string","Tkinter"],
include_files = ["responsible.xls","favicon.ico","logo.jpg"],
excludes = []
path = sys.path + ["modules"]
)
setup(
name = "test",
version = "1.1.1.0",
description = "test",
options = dict(build_exe = buildOptions),
executables = executables
)
You add the directories you want to include with the include_files option.
So this part of your code should be like this:
buildOptions = dict(
compressed = True,
includes = ["sys","re","PIL","ttk","xlrd","xlutils","datetime","string","Tkinter"],
include_files = ["responsible.xls","favicon.ico","logo.jpg", "modules"],
excludes = []
path = sys.path + ["modules"]
)