Maya Python: IOError: [Errno 9] Bad file descriptor - python

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("")

Related

Baffling FileNotFoundError on Windows

The problem that I'm having is frankly baffling. I was doing some python coding, come back the next day, and without changing anything, the code stopped working, giving a FileNotFoundError. I've whittled it down to a laughably simple minimum code to produce the error.
f = open('positions.tsv','w')
Which produces the error
Traceback (most recent call last):
File "c:/Users/mrhel/Documents/GitHub/gas-collisions/test.py", line 1, in <module>
f = open('positions.tsv','w')
FileNotFoundError: [Errno 2] No such file or directory: 'positions.tsv'
I have been scouring the internet for hours for a solution but everything I come across is someone trying to create a file in a folder that doesn't exist or forgetting to set the mode to 'w'. I copied the exact code that this is happening with onto a Linux machine and it runs just fine, it is specifically not working on my Windows 10 machine.
I have tried rebooting the computer, which doesn't work either. Why would this simple line of code not work specifically on Windows?
Edit:
How I run the code is either python test.py or C:/Users/mrhel/AppData/Local/Microsoft/WindowsApps/python3.8.exe c:/Users/mrhel/Documents/GitHub/gas-collisions/test.py
In any case, the expected behavior according to the open() documentation
is that positions.tsv should be created upon calling this function with the 'w' mode.
When I do create the file in advance, I get an error when I try to write to it.
f = open('positions.tsv','w')
f.write('testing')
f.close()
Error:
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/mrhel/Documents/GitHub/gas-collisions/test.py", line 3, in <module>
f.close()
OSError: [Errno 9] Bad file descriptor
Edit 2:
I tried creating a file and reading it, and that works as expected. It's specifically writing that isn't working
Edit 3:
So since it runs in another folder, I decided to check for write permissions, and this is what I got. When I check properties of the folder with windows, all users have allow ticked.
>>> os.access('my_folder', os.W_OK)
False
This is clearly a permissions issue, but I'm not sure how to rectify it.
This turned out to be a result of "Controlled Folder Access" in Windows Defender Ransomware Protection. Turning off this feature allows it to function as expected. It's not clear how Ransomware Protection decided that my code is not to be trusted, but for posterity's sake, that is how this was fixed.

GDB printing STL data

After following the instructions given on this site: https://sourceware.org/gdb/wiki/STLSupport
GDB is still unable to print the contents of stl containers like vectors, other than printing out a huge amount of useless information. When GDB loads, I also get the following errors, which I think are related to the Python that I put into ~/.gdbinit
Traceback (most recent call last):
File "<string>", line 4, in <module>
File "/Users/mayankp/gdb_printers/python/libstdcxx/v6/printers.py", line 1247, in register_libstdcxx_printers
gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
File "/usr/local/share/gdb/python/gdb/printing.py", line 146, in register_pretty_printer
printer.name)
RuntimeError: pretty-printer already registered: libstdc++-v6
/Users/mayankp/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
When GDB loads, I also get the following errors...
It looks like instructions you followed on https://sourceware.org/gdb/wiki/STLSupport are invalid now. If you look at svn log you will see that registering of pretty printers was added in __init__.py recently:
------------------------------------------------------------------------
r215726 | redi | 2014-09-30 18:33:27 +0300 (Вт., 30 сент. 2014) | 4 lines
2014-09-30 Siva Chandra Reddy <sivachandra#google.com>
* python/hook.in: Only import libstdcxx.v6.
* python/libstdcxx/v6/__init__.py: Load printers and xmethods.
------------------------------------------------------------------------
And therefore second registration throws error. You can remove it or comment out:
#register_libstdcxx_printers (None)
GDB is still unable to print the contents of stl containers
You have probably mismatched pretty printers with your gcc. See https://stackoverflow.com/a/9108404/72178 for details.
From your traceback it seems that the register_libstdcxx_printers() call is failing because there already is such a pretty printer registered. To avoid that, you can wrap it in a try..except to make sure instructions in .gdbinit don't interfere with the launch of GDB if they fail:
python
import sys
sys.path.insert(0, '/home/user/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
try:
register_libstdcxx_printers(None)
except:
pass
end
(Note: You should usually never use a bare except statement without qualifying the type of exceptions you want to catch. This is a special case though, in startup configuration files like .gdbinit, .pdbrc or your PYTHONSTARTUP file you'll probably want to write defensive code like that).
But chances are this will only get rid of the ugly traceback for you, and printing of STL vectors still wont work. Because it seems there is already a pretty printer registered from somewhere else.
Make sure the path /home/user/gdb_printers/python actually matches the path where you checked out the module mentioned in the STLSupport docs.

Make _tkinter.createfilehandler work again (or provide a workaround)

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.

second python execution fails

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#

Running fselect using Python

I am trying to run this python script called fselect in windows 7. It can be downloaded from this website: http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/ under this name called Feature selection tool. I am running it on Python 2.7.2.Facing a bit of problem running it..
Typed this first in IDLE:
>>> import pprint
>>> import sys
>>> print pprint.pprint(sys.path)
>>> sys.path.append("C:\Users\HP\Documents\MATLAB\libsvm-3.11\tools")
>>> import fselect
Usage: training_file [testing_file]
Then the problem is when i type the next part:
Tried this:
>>> ./fselect.py TrainVec
SyntaxError: invalid syntax
Next tried this:
>>> fselect.py TrainVec
SyntaxError: invalid syntax
Next tried this:
>>> TrainVec
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
TrainVec
NameError: name 'TrainVec' is not defined
Tried this also:
>>> TrainVec.mat
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
TrainVec.mat
NameError: name 'TrainVec' is not defined
What is the correct way of typing it? Need some guidance on it...
tried running using cmd but there is an error...
If you are trying to run the fselect.py directly from the command prompt, make sure that python is set into the path variable. For guidance with that, please read http://people.cis.ksu.edu/~schmidt/200f07/setpath.html.
The script will also invoke grid.py. grid.py requires gnuplot to be there. So ensure that grid.py is running properly and if necessary check the paths of the svm_train, svm_test in the script along with that of grid.py.
Hope it will work now.
If it is a tool, you should run it, not import it. And of course you should not try to enter random commands, even if they are valid shell commands, in your Python prompt.
Assuming TrainVec is your data (since you use it in the context of TrainVec.mat it must be a Matlab data file) then run it on the Command Prompt like this:
python fselect.py TrainVec.mat
The example of ./fselect.py is intended for Unix systems. Make sure you run the above command in what ever directory you have saved fselect.py in.
If you need to write your own scripts to leverage this .py file then I refer you here for an example of how to do this.
Like the previous answer said, it looks like you are (incorrectly) trying to run the script from inside the Python interpreter. According to the documentation on the page you link to, it is not a module but a free-standing script and should be run as such:
Usage: ./fselect.py training_file [testing_file]

Categories