I've been trying to pack my app with py2exe. The application works fine but it keeps failing to find/use pywinauto. I been googling but I get nothing, I'm now I'm totally lost...
Here's the packing script:
from distutils.core import setup
setup(
windows = ["mainForm.py"],
data_files=[
('', ['mainForm.ui']),
('', ['osk.sqlite'])
],
options = {
"py2exe":{
"optimize": 2,
"includes": [
'sip', 'pyttsx.drivers.sapi5', 'win32com', 'xml.etree.ElementTree', 'sqlite3',
'pywinauto', 'pywinauto.application', 'pywinauto.controls', 'pywinauto.tests', 'SendKeys'
],
"typelibs": [('{C866CA3A-32F7-11D2-9602-00C04F8EE628}', 0, 5, 0)]
}
}
)
And here's the ouput when running the exe
Traceback (most recent call last):
File "mainForm.py", line 129, in changeState
File "mainForm.py", line 230, in setWriteMode
File "mainForm.py", line 105, in FillApps
File "WindowHandler.pyo", line 26, in getWindowList
NameError: global name 'pywinauto' is not defined
I hope anyone could point me into the right direct.
Thanks in advance
From my experience, py2exe handles imports in a weird way. Sometimes it has trouble finding linked-imports (like you import WindowHandler, which imports pywinauto).
I would start with this in mainForm.py:
import sys
import WordOps
import Voice
import WindowHandler
from PyQt import QtCore, QtGui, uic
And in setup.py, start with this:
options={'py2exe':{
'includes': ['sip'],
'bundle_files': 1
}
}
Make sure your program works before compiling to an exe, then try running setup.py.
When you start getting errors when running setup.py (like the one you posted), add more imports to mainForm.py. So for that error, your new header will look like:
import sys
import WordOps
import Voice
import WindowHandler
from PyQt import QtCore, QtGui, uic
# imports for py2exe
import pywinauto
It will not break your code because it will just be an "unused" import.
Keep doing that until setup.py works.
Related
I have this
folder_tree
and i want to import class from runner.py in crawlers.py
from scraper.runner import Runner
runner = Runner([{'name': 'my_name', 'urls': ["https://www.exaple.com/"]}])
runner.crawl()
but i got error
Traceback (most recent call last):
File "./scraper/crawlers/actor_crawler.py", line 3, in <module>
from scraper.runner import Runner
ModuleNotFoundError: No module named 'scraper'
Also, i tried relative import:
from ..runner import Runner
And got that:
ValueError: attempted relative import beyond top-level package
I've found the following link to be incredibly helpful in understanding how Python does imports.
The surefire way to ensure your module can be imported though is adding it to os.syspath. Try adding the following to the beginning of your script:
import os
import sys
sys.path.append(/path/to/scraper/scraper/runner.py)
from scraper.runner import Runner
runner = Runner([{'name': 'my_name', 'urls': ["https://www.exaple.com/"]}])
runner.crawl()
I am using Py2exe to create a executable app for my GUI and this is my setup code:
import matplotlib
from distutils.core import setup
import FileDialog
import zmq.libzmq
import py2exe
setup(
data_files=[matplotlib.get_py2exe_datafiles(),(zmq.libzmq.__file__,)],
console = [{'script': 'SVS-virtual-lib2.py'}],
options={
'py2exe': {
'packages': ['FileDialog'],
'includes': ['zmq.backend.cython'],
'excludes': ['zmq.libzmq'],
'dll_excludes': ['libzmq.pyd']
}
}
)
But i get the following error:
File "C:\Users\nzarinabad\AppData\Local\Continuum\Anaconda\lib\distutils\util.py", line 128, in convert_path
paths = string.split(pathname, '/')
File "C:\Users\nzarinabad\AppData\Local\Continuum\Anaconda\lib\string.py", line 294, in split
return s.split(sep, maxsplit)
AttributeError: 'tuple' object has no attribute 'split
Dose anyone who why i get the error and how to fix it?
Thank you
Please see the documentation, if you want to combine matplotlib.get_py2exe_datafiles() with other files, you have to do some manual work:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import os
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
matplotlibdata_files = []
for f in matplotlibdata:
dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
matplotlibdata_files.append((os.path.split(dirname)[0], [f]))
matplotlibdata_files.append(zmq.libzmq.__file__)
# ...
setup(
data_files=matplotlibdata_files,
# rest of your code
I know there are many posts about this problem (i've read them all).
But i still have a problem with my exe, still cannot be opened.
I've tried to put the qwindows.dll (i tried with 3 different qwindows.dll) in the folder dist with my exe but doesn't change anyhting.
I've tried with libEGL.dll, nothing.
Any suggestions ? Is there a way to avoid having this problem ?
I've had this issue aswell, after a lot of digging I found the following solution:
Copy the following file next to you main .exe:
libEGL.dll
Copy the following file in a folder "platforms" next to you main .exe:
qwindows.dll
Putting the qwindows.dll in the subfolder is the important part I think, hope this helps
Try:
from setuptools import setup
import platform
from glob import glob
from main import __version__, __appname__, __author__, __author_email__
SETUP_DICT = {
'name': __appname__,
'version': __version__,
'description': 'description',
'author': __author__,
'author_email': __author_email__,
'data_files': (
('', glob(r'C:\Windows\SYSTEM32\msvcp100.dll')),
('', glob(r'C:\Windows\SYSTEM32\msvcr100.dll')),
('platforms', glob(r'C:\Python34\Lib\site-packages\PyQt5\plugins\platforms\qwindows.dll')),
('images', ['images\logo.png']),
('images', ['images\shannon.png']),
),
'options': {
'py2exe': {
'bundle_files': 1,
'includes': ['sip', 'PyQt5.QtCore'],
},
}
}
if platform.system() == 'Windows':
import py2exe
SETUP_DICT['windows'] = [{
'script': 'main.py',
'icon_resources': [(0, r'images\logo.ico')]
}]
SETUP_DICT['zipfile'] = None
setup(**SETUP_DICT)
copy the dependency manually is a bad way to do, because py2exe take care of it. With pyqt5, this setup works, BUT if I try in other computer without pyqt install the exe crashes. I migrated to pyqt4 and run in all computers.
For me it was enough to copy qwindows.dll to platforms folder, like #Inktvisje wrote.
And don't repeat my mistake: don't download this dll from Internet! Copy it from your Python libs folder: YourPythonFolder\Lib\site-packages\PyQt5\plugins\platforms.
I am encountering an import error
Traceback (most recent call last):
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
ImportError: cannot import name 'SlotMap
This should be a straightforward issue, but I can’t seem to find the problem. If I place the SlotMapper.py file in the same directory as the GUI I am using the import of SlotMap occurs without error. If I move the file to a directory under the current working directory and add - sys.path.append(os.path.join(os.getcwd(), 'appLib')) I receive the error above. See import statements and modification of PYTHONPATH below. I know the PYTHONPATH has been modified after I checked it from the debugger. I also know since there are other files under appLib required for the GUI to operate. Finally, I have checked all of the imported files for a circular reference and find none… So stuck. Any suggestions welcome
import os
import sys
sys.path.append(os.path.join(os.getcwd(), 'appLib', 'KB-GUI'))
sys.path.append(os.path.join(os.getcwd(), 'appLib'))
from tkinter import *
from SlotMapper import SlotMap
from ShelfTypeSelection import ShelfTypeSelector
from PackTypeSelection import PackTypeSlotMappingSelector
from EntryWidgets import EntryBase, ShelfSlotEntry
The reason this is not working is because your file is named SlotMapper.pyw. The line
from SlotMapper import SlotMap
is trying to import SlotMap from your current file, hence the error. Try renaming your file to slotmapper_test.pyw or something like that, and everything should work as expected. You don't want your code files to have the same names as any modules you're trying to import, as the import mechanism will try to find the classes/functions there first, instead of searching your modules first.
I am trying to make a small script to remotely manage windows computers (currently only shutdown). The method I am using involves a webapp2 server. i would like to compile my first attempt into a .exe. The problem I am having is that after successfully compiling it I go to run it and it returns the error:
Traceback (most recent call last):
File "web2.py", line 2, in <module>
File "webapp2.pyc", line 25, in <module>
File "webob\__init__.pyc", line 1, in <module>
File "webob\datetime_utils.pyc", line 10, in <module>
ImportError: No module named email.utils
I have also tried this with cx_Freeze which had similar results. I have followed the advice given at import error while bundling using py2exe to no avail.
In case it is any use here is my code:
import cgi
import webapp2
import os
import socket
def ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
return s.getsockname()[0]
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action="/shutdown" method="link">
<div><input type="submit" value="Shutdown"></div>
</form>
</body>
</html>""")
class shutdown(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>Shutting down...<pre>')
self.response.out.write('</pre></body></html>')
os.system("shutdown -p -f")
app = webapp2.WSGIApplication([('/', MainPage),
('/shutdown', shutdown)],
debug=True)
def main():
from paste import httpserver
httpserver.serve(app, host=ip(), port='80')
if __name__ == '__main__':
main()
Thank you in advance.
EDIT:
I have found out using modulefinder that there are a lot of modules not being imported. I don't however know if this is happening when ran normally or only when imported or something like that.
http://pastebin.com/s0U9WHJ6
I have discovered that the problem was that I was presuming that py2exe would import webob as the interpreter does. In fact I needed to put the webob folder in the folder that I was building in.
I am not sure, but you could try specifically including email.utils in the setup.py by adding the following argument to the setup function call in the script that imports py2exe:
options={"py2exe": {'includes': ["email.utils"]}}
That, or you could try specificly importing it before you import webapp2, like on line 1:
import email.utils
import cgi
import webapp2
If this says it can't find a diferent module, try adding the module in the includes list:
options={"py2exe": {'includes': ["email.utils", "othermodulename"]}}
or specificly importing it again.
Hope this helps! :-)