Pywinauto app = Application.start() is not working and giving error - python

I get an error when I try to start an exe file with pywinauto. This is my code:
from pywinauto.application import Application
app = Application()
app.Start("D:\dragonboy160\dragonboy160.exe")
As far as I know that should work. app = Application().start("") does not work either. I also tried setting the imports like this: from pywinauto import Application I get this error when I run the python file in PyCharm:
"C:\Program Files (x86)\python.exe" D:/dragonboy160/screenview.py
Traceback (most recent call last):
File "D:/dragonboy160/screenview.py", line 2, in <module>
import pywinauto
File "C:\Program Files (x86)\lib\site-packages\pywinauto\__init__.py", line 40, in <module>
from . import findwindows
File "C:\Program Files (x86)\lib\site-packages\pywinauto\findwindows.py", line 42, in <module>
from . import controls
File "C:\Program Files (x86)\lib\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Program Files (x86)\lib\site-packages\pywinauto\controls\uiawrapper.py", line 44, in <module>
from ..uia_defines import IUIA
File "C:\Program Files (x86)\lib\site-packages\pywinauto\uia_defines.py", line 35, in <module>
import comtypes.client
File "C:\Program Files (x86)\lib\site-packages\comtypes\client\__init__.py", line 33, in <module>
gen_dir = _find_gen_dir()
File "C:\Program Files (x86)\lib\site-packages\comtypes\client\_code_cache.py", line 71, in _find_gen_dir
result = os.path.abspath(gen_path[-1])
IndexError: list index out of range
I searched and searched and it seems no other person has had this problem. I also tried opening other .exe files but got the same error. What is causing this error and how can I fix it?

This is an import problem (at the very first line).
This looks like comtypes can't write cached files to Program files without admin privileges. In theory it might be fixed on comtypes side (or by re-installing Python to another folder like C:\Python3x), but you will have to run the script as Administrator anyway because of other OS restrictions (pywinauto will warn about insufficient rights in nearest future, now it's silent).

I had the same problem and I managed to have it fixed by disabling UAC from Windows.

Related

Problem with creating an .exe out of my Python script via Pyinstaller

I'm trying to create an exe out of my Python script. After a long journey of problem solving, I finally made it with Pyinstaller. I got a build folder, a dist folder, and the needed exe file.
Now, when I try to run the program, it doesn't work, although it does work in my IDE, PyCharm. If I just click on it, the cmd just opens for a ms and closes right away. If I start it via the cmd, it shows the error you can see below.
Normally there should be a main_window generated by my script, and like I said, it works when I start it from pycharm directly, so the code shouldn't be the problem.
I need the program on another PC without Pycharm at all, so I definitely need this exe file. I hope you can help me. Thanks in advance!
This is the error:
C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_>main_.exe
Traceback (most recent call last):
File "main_.py", line 7, in <module>
import torch
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\__init__.py", line 643, in <module>
from .functional import * # noqa: F403
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\functional.py", line 6, in <module>
import torch.nn.functional as F
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\nn\__init__.py", line 1, in <module>
from .modules import * # noqa: F403
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\nn\modules\__init__.py", line 2, in <module>
from .linear import Identity, Linear, Bilinear, LazyLinear
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\nn\modules\linear.py", line 6, in <module>
from .. import functional as F
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\nn\functional.py", line 11, in <module>
from .._jit_internal import boolean_dispatch, _overload, BroadcastingList1, BroadcastingList2, BroadcastingList3
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\_jit_internal.py", line 28, in <module>
import torch.package._mangling as package_mangling
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\package\__init__.py", line 11, in <module>
from .package_exporter import EmptyMatchError, PackageExporter, PackagingError
File "C:\Users\messlaptop\PycharmProjects\Source_code\dist\main_\torch\package\package_exporter.py", line 5, in <module>
import pickletools
ModuleNotFoundError: No module named 'pickletools'
[8896] Failed to execute script 'main_' due to unhandled exception!

Pyinstaller having issues with import of librairies

I was working with python (which I rarely do) for a simple program that macros some functionalities with certain keyboard inputs. I decided to export the script to a .exe using pyinstaller so I go ahead and run command: pyinstaller --onefile code.py and it generates the code.exe as expected. Only for my .exe to crash instantly. I got a view of the error by running it with cmd and got this
Traceback (most recent call last):
File "code.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[1284] Failed to execute script 'code' due to unhandled exception!
I'm certain there is an explanation to this so here are my imports:
from pynput import keyboard
from pynput.keyboard import Key
from pywinauto import application
from pywinauto.findwindows
import WindowAmbiguousError, WindowNotFoundError
import os
I couldn't seem to find any similar problems on here. Thank you in advance for the help.
In your .spec file, you should add all of your modules as "hidden imports", this usually fixes all module not found errors.
hiddenimport=["pynput","pywinauto"]
Then run pyinstaller with the spec file
pyinstaller my.spec
I'd also ensure you're using a virtual environment during this entire process.

importing pywinauto.application throws error while trying to run using python 3.5.4

I was trying to execute the following command on windows 10 machine using python 3.5.4,
from pywinauto.application import Application
Following is the error which i was receiving
Traceback (most recent call last):
File "C:/Users/bizact/Desktop/PSAV/test.py", line 1, in <module>
from pywinauto.application import Application
File "C:\Program Files\Python35\lib\site-packages\pywinauto\__init__.py", line 72, in <module>
from . import findwindows
File "C:\Program Files\Python35\lib\site-packages\pywinauto\findwindows.py", line 42, in <module>
from . import controls
File "C:\Program Files\Python35\lib\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Program Files\Python35\lib\site-packages\pywinauto\controls\uiawrapper.py", line 46, in <module>
from ..uia_defines import IUIA
File "C:\Program Files\Python35\lib\site-packages\pywinauto\uia_defines.py", line 35, in <module>
import comtypes.client
File "C:\Program Files\Python35\lib\site-packages\comtypes\client\__init__.py", line 33, in <module>
gen_dir = _find_gen_dir()
File "C:\Program Files\Python35\lib\site-packages\comtypes\client\_code_cache.py", line 71, in _find_gen_dir
result = os.path.abspath(gen_path[-1])
IndexError: list index out of range
Is there a way i can fix this ?
Try running the script with administrative privileges. Just by looking at the traceback it looks like some cached files that pywinauto import tried to write but later on it wasn't found, and generally the script can't write the file due to permissions. If the above don't work, try installing python somewhere else (C:/Python with full privileges in order to isolate the issue)
This is comtypes issue. It will be fixed soon (EDIT: fixed in comtypes==1.1.7). You can downgrade to one of previous versions: pip install comtypes==1.1.2. Or run script as Administrator as Rodolfo suggested (disabling UAC may not help, need to say explicitly to "run as Administrator" by popup menu).

I can't make anaconda work on windows by importing pandas

I am new to Python and am trying to use anaconda to do some data analysis. I am using Python 3.6.2 along with the geany text editor.
First, I'm trying to execute a file call panda.py which only contains import pandas as dp.
When I configure geany with a working directory of C:\Users\Anaconda3\python, I get the following error:
Traceback (most recent call last):
File "panda.py", line 1, in <module>
import pandas as dp
File "C:\Users\Anaconda3\lib\site-packages\pandas\__init__.py", line 13, in <module>
__import__(dependency)
File "C:\Users\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Users\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Users\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Users\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Users\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 35, in <module>
from . import _internal # for freeze programs
File "C:\Users\Anaconda3\lib\site-packages\numpy\core\_internal.py", line 18, in <module>
from .numerictypes import object_
File "C:\Users\Anaconda3\lib\site-packages\numpy\core\numerictypes.py", line 962, in <module>
_register_types()
File "C:\Users\Anaconda3\lib\site-packages\numpy\core\numerictypes.py", line 958, in _register_types
numbers.Integral.register(integer)
AttributeError: module 'numbers' has no attribute 'Integral'
The same error also occurs when I enter the following at the command prompt:
C:\Users\Anaconda3\python.exe C:\Users\Documents\python_work\panda.py
However, if I execute C:\Users\David\Anaconda3\python.exe and then enter import pandas as dp in the REPL, it appears to load without error.
Any idea how I can fix this so I can use anaconda with geany?
[SOLVED] John below ask if I had a file called numbers.py in my directory which made me realize I named an exercise I previously did as numbers.py. Once I removed it, the error went away (I tested it further by adding a file with the name again and the error returned).
I'm new to the stack over flow community. Let me know if there's anything else I need to do to close out this question.
Also thanks to chb for the edits to my original question the format looks much easier to read.
Disclaimer: python on windows is not something I know about
You know that C:\Users\David\Anaconda3\python.exe when run standalone knows how to import pandas, so your setup is fine - you have everything installed that you need, and in the correct locations.
Given that, you can look at two issues: gearny setup, and general setup. I noticed that python and pandas is installed under C:\Users\Anaconda3, while your program is under C:\Users\Documents\python_work, and we also have C:\Users\David - could it be permissions issues?

program wont work after being compiled with py2exe

I wrote a program it works fine without any errors when I run it from the python interpreter, I then used py2exe to turn it in to an .exe but when I run it it does'nt work anymore... I get this error:
Traceback (most recent call last):
File "pass.py", line 1, in <module>
File "dropbox\__init__.pyc", line 3, in <module>
File "dropbox\client.pyc", line 22, in <module>
File "dropbox\rest.pyc", line 26, in <module>
File "pkg_resources.pyc", line 950, in resource_filename
File "pkg_resources.pyc", line 1638, in get_resource_filename
NotImplementedError: resource_filename() only supported for .egg, not .zip
Am I supposed to do something when using py2exe when I have downloaded modules imported into the program?
these are the modules imported :
import dropbox
import os
import getpass
from time import sleep
please help !
I fixed this problem using the solution found here
basically you modify ~line 26 in rest.py found in the dropbox library
to this:
try:
TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')
except:
# if current module is frozen, use library.zip path for trusted-certs.crt path
# trusted-certs.crt must exist in same directory as library.zip
if hasattr(sys,'frozen'):
resource_name = sys.prefix
resource_name.strip('/')
resource_name += '/trusted-certs.crt'
TRUSTED_CERT_FILE = resource_name
else:
raise
and then place the trusted-certs.crt file also found in the dropbox library in the same folder as your executable

Categories

Resources