I've got some old Python scripts which used a different version of tkinter than the current systems are equipped with. Back in the old days, a method named _tkinter.createfilehandler() existed and worked. With the current versions this returns with a RuntimeError exception:
Traceback (most recent call last):
File "src/text.py", line 513, in <module>
tkinter.createfilehandler(pipe_r, READABLE, drain_pipe)
RuntimeError: _tkinter.createfilehandler not supported for threaded Tcl
The script I'm trying to get to run is this (shortened version of course):
#!/usr/bin/env python
import os
from Tkinter import *
(pipe_r, pipe_w) = os.pipe()
# ...
def drain_pipe(a, b):
# handle data from pipe_r
# ...
tkinter.createfilehandler(pipe_r, READABLE, drain_pipe)
tk.mainloop()
Is there a simple way to get this running again? I'm sure there is a way to convert the scripts (or maybe write them anew based on a different paradigm), but I'd be happy with a hack to not have to change very much (maybe there's a switch to enable this again somehow) because I've got several such scripts and would prefer not to have to patch a lot.
If tk is a Tk() object, then use tk.createfilehandler instead.
Related
I have a traffic simulation that is running in the simulation tool known as SUMO and I am trying to have the simulation run repeatedly multiple times (about 50 times at the most), but for the sake of this example I will use 10 times. My main code is in another Python file and it requires that arguments be passed into it prior to running. This code is quite lengthy in nature and has multiple Python functions in it so I will not post it in this forum, but for sake of simplification lets call it performSIM.py.
My other Python code that is responsible for repeating the main code looks something like this:
import traci
from sumolib import checkBinary
import argparse
import performSIM
for i in range(10):
pythonFile = str(performSIM)
exec(pythonFile)
I was wondering what is the right way to loop my main code so that runs the simulation 10 times repeatedly? When I run the lines of code above I get the following error:
Traceback (most recent call last):
File "C:\Users\#####\Sumo\USA Road Network SUMO_2021\running_Python.py", line 8, in <module>
exec(pythonFile)
File "<string>", line 1
<module 'performSIM' from 'C:\\Users\\#####\\Sumo\\USA Road Network
SUMO_2021\\performSIM.py'>
**SyntaxError: invalid syntax**
How exactly can I fix this issue so this error does keep showing up?
Thank you in advance for the help
Your performSIM should have one main function, if it does not have one yet you should restructure it in such a way that it does. A common pattern is to pass the command line arguments to the main function as a parameter. So performSIM.py could look like this:
import sys
def main(args):
# parse the args
# do other useful stuff
if __name__ == "__main__":
main(sys.argv)
Then your other script can simply do
import sys
import performSIM
for i in range(10):
performSIM.main(sys.argv)
I have an application that is time sensitive to the time on a remote server - it needs to be run at a particular instant. Looping and retrieving the server time continuously does not get me as close as i'd like. I am trying to retrieve the server time and update my system's time (Python 3.7, windows 10) in advance so the module can restart as close as possible to the server's time. I have found the SetSysemTime function in win32api.
Here is the two line module i created to test various approaches:
import win32api
win32api.SetSystemTime(2020,9,1,21,9,10,10,0)
When i run this i get the following error:
Traceback (most recent call last):
File "C:\Users\pinev\AppData\Local\Programs\Python\Python37\Projects\Threading Tests\settime.py", line 4, in
win32api.SetSystemTime(2020,9,1,21,9,10,10,0)
pywintypes.error: (1314, 'SetSystemTime', 'A required privilege is not held by the client.')
So it seems i have the right solution, but somehow the module's privilege needs to be changed in order for Win10 to process the request. I have researched ways to set the privilege in a Python module and can't seem to find anything that works. If anyone could could provide a solution or a reference it would be greatly appreciated.
Thanks in advance.
If you just need to elevate privileges to run Python programs, you can launch the program as an administrator.
This is also the simplest and most direct method I think.
Test code:
import win32api
import ctypes, sys
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
win32api.SetSystemTime(2020,9,1,21,9,10,10,0)
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
I created a maya python toolchain for my team. All works well, just on one machine i seem to have problems. I narrowed it down to the print command. Like this test library called "temp.py":
import os
# from pymel.core import *
print "Hello"
after importing it with
import temp
it produces this output (only on that one computer!):
// Error: 9
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# File "C:\maya_scripts\temp.py", line 4, in <module>
# print "Hello"
# IOError: [Errno 9] Bad file descriptor //
I've tried Maya Version 2016, 2016.5 and 2017. All the same result. Python 2.5 as standalone hasn't got that problem. To me that sounds like some kind of configuration problem, but then again it behaves the same over 3 different maya installations, so deleting the prefs didn't help either.
It's hard to know what's really happening here. But try this
import sys
sys.__stdout__.write("hello")
sys.__stdout__.write(str(sys.stdout))
Then check your output window (not the listener). In a vanilla maya you'd expect to see something like
<maya.Output object at 0x00000217E827FC10>
after "hello". If you see something else, some script has tried to hijack sys.stdout in this installation. You can probably work around it by creating an environment variable called MAYA_SKIP_USERSETUP_PY, setting it to 1, and restarting -- that should stop whatever script is being naughty from autoloading.
This ought to reset it to what you're looking for:
import maya.utils as utils
sys.stdout = utils.Output()
However you need to comb through the scripts on that machine and find out who is messing with sys.stdout behind your back
The error is from your module, you may overwrite the print function
maya 2016 is python 2.7.6 and maya 2017 is in python 3.x
on 2017 you must use print("")
How can I use the navigation-policy-decision-requested (or equivalent) in association with webkit_web_policy_decision_ignore()?
The following is a small outtake form my code (the rest is just a wrapper and settings etc):
def check(view, frame, req, nav, policy):
webkit_web_policy_decision_ignore(TRUE)
...
view.connect("navigation-policy-decision-requested", check)
When I load a new page this error is returned:
Traceback (most recent call last):
File "browser.py", line 17, in check_exec
webkit_web_policy_decision_ignore(TRUE)
NameError: global name 'webkit_web_policy_decision_ignore' is not defined
What I'm trying to achieve is that if a certain address have been given, actions will be taken to prevent it from loading via Python. Any suggestions are welcome in the comments and any additional information you may need will be provided upon request.
Now I'm new to Python so please be specific in your answer, criticism and suggestions.
If you are using pygtk, try policy.ignore().
The object names are mapped slightly differently in pygtk. In python shell you can try after executing from gi.repository import WebKit
print dir(WebKit)
to find corresponding object and in your case
help(WebKit.WebPolicyDecision)
I'm having a problem embedding the python 3 engine for an app that need to run custom scripts in python. Since the scripts might be completely different, and sometimes user provided, I am trying to make each execution isolated and there is not need to preserve any data between execution of the different scripts.
So, my solution is to wrap each execution between Py_Initialize and Py_Finalize. It looks something like that:
void ExecuteScript(const char* script)
{
Py_Initialize();
PyRun_SimpleString( script );
Py_Finalize();
}
However, this fails for a particular python script the second time a script is executed with:
done!
Traceback (most recent call last):
File "<string>", line 8, in <module>
File "\Python33Test\Output\Debug\Python33\Lib\copy.py", line 89, in copy
rv = reductor(2)
TypeError: attribute of type 'NoneType' is not callable
The python script looks like this:
class Data:
value1 = 'hello'
value2 = 0
import copy
d = Data()
dd = copy.copy( d )
print ( 'done!' )
As you can see, the first time around the script was executed the 'done!' was printed out. But the second time it rises an exception inside the copy function.
It looks like the python engine was left in some weird state after the first initialize-finalize. Note, this is python 3.
Also, it is very interesting to note that Python 2.7 did not have this problem.
I guess there might be other examples that could reveal better what's going, but i haven't had the time to find yet.
Full sources of the test project can be found here:
https://docs.google.com/file/d/0B86-G0mwwxZvNGpoM1Jia3E2Wmc/edit?usp=sharing
Note, the file is 8MB because it includes the python distribution.
Any ideas of how to solve this are appreciated.
EDIT: I also put a copy of the project containing flag to switch between Python 3 and Python 2.7 (the file is 31 MB): https://docs.google.com/file/d/0B86-G0mwwxZvbWRldTd5b2NNMWM/edit?usp=sharing
EDIT: Well, I tested with Python3.2 and it worked fine. So it seems to be bug in Python3.3 only. Adding as an issue: http://bugs.python.org/issue17408#
Well, this was fast.
They actually found the issue being a problem in Python 3.3 code.
http://bugs.python.org/issue17408#