'NameError: name 'fcntl' is not defined' when using urwid on windows - python

So I just installed Urwid and as a test tried running the Urwid equivalent of a basic print command, as given as an example on the Urwid website tutorial. I received an error message.
I tried running a different one of the examples and received a similar error message. The code looks like this:
import urwid
txt = urwid.Text(u"Hello World")
fill = urwid.Filler(txt, 'top')
loop = urwid.MainLoop(fill)
loop.run()
It should print 'Hello World' in the top left corner of the screen and then run until instructed to quit. Instead I get this error message:
Traceback (most recent call last):
File "C:\Users\Rory Kranz\AppData\Local\atom\app-1.34.0\testingg", line 5, in <module>
loop = urwid.MainLoop(fill)
File "C:\Users\Rory Kranz\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urwid\main_loop.py", line 114, in __init__
screen = raw_display.Screen()
File "C:\Users\Rory Kranz\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urwid\raw_display.py", line 92, in __init__
fcntl.fcntl(self._resize_pipe_rd, fcntl.F_SETFL, os.O_NONBLOCK)
NameError: name 'fcntl' is not defined
Did something go wrong with the installation, or is there something else I need to install in order to get Urwid to work?

If you want to use urwid, you will need to use it with cygwin (not sure of mingw supports it but it might be worth a try if that's your preference).
Windows does not natively support the fcntl interface and apparently it is not trivial to simulate it using win32 functions.
If you want to remain on windows without a unix emulation environment, you might try curses or python prompt toolkit. I can't be sure these are the best choices as I'm not clear on what exactly you are trying to do.
Some relevant links
Problem over windows platform
NameError: name 'fcntl' is not defined

The previous answer is outdated. Windows does not support the interface however installing the new Linux bash console will suffice.

Related

Can't change sys.ps1 in IDLE

I've recently heard that you can change the prompt in python by changing variable sys.ps1. So I've decided to open IDLE, and write something like that:
>>> import sys
>>> sys.ps1 = ":::"
However, that created a new variable and nothing changed (prompt was still ">>>") - I rebooted IDLE and checked is this variable read by python... Nope:
>>> sys.ps1
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
sys.ps1
AttributeError: module 'sys' has no attribute 'ps1'
So, how do I change the prompt in python?
NOTE: The other functions/variables of sys module were read properly.
See issue #13657, "IDLE doesn't recognize resetting sys.ps1", on the Python bug tracker. It was opened in 2011 and is still unresolved (as of this writing). So it is not possible to change the prompt from within IDLE's Python shell simply because IDLE does not support that.
Update (less than 24 hours later): The issue is now closed. In the upcoming version of IDLE, to be released with Python 3.10, prompts will be displayed differently. See comment by IDLE developer Terry Jan Reedy above. A setting to change the prompt may be added in the future.
In the current and earlier Python/IDLE releases, the prompt can be customized before starting IDLE, but not while running it. You'd need a little start-up script that does this:
import sys
sys.ps1 = '::: '
import idlelib.idle
The reason you get that error message (module 'sys' has no attribute 'ps1') is because the Python shell that IDLE presents to you is not actually in "interactive" mode. And only then is sys.ps1 defined. You'd see the same error message if you tried to access sys.ps1 in any other Python program that is directly executed. In this case, that Python program is IDLE itself.
The prompt strings specifying the primary and secondary prompt (their initial values in this case are >>> and ... ) of the interpreter are only defined if the interpreter is in interactive mode and IDLE is more or less an integrated development environment for Python
Python interactive mode:
sys.ps1 doc

How to use websockify with python on windows or compiling websockify as windows executable (for more multiple client connection at a time)

I'm trying to get the websockify 0.6.0 running on windows but without any luck,
I have tried python websockify.py 1501 10.0.0.141:1501 but its not good, getting errors , like that:
Traceback (most recent call last):
File "websockify.py", line 1, in <module>
run
NameError: name 'run' is not defined
I also tried Compiling Websockify as Windows Executable , but this also didn't work
I use the following command run.exe 1501 10.0.0.141:1501 and it looks promising at the beginning, outputting the following to the console:
WARNING: no 'resource' module, daemonizing is disabled
WebSocket server settings:
- Listen on :1501
- Flash security policy server
- No SSL/TLS support (no cert file)
- proxying from :1501 to 10.0.0.141:1501
but then after trying to connect using the following from the browser ws://localhost:1501
**it outputs the following error
Traceback (most recent call last):
File "run", line 5, in <module>
File "websockify\websocketproxy.pyc", line 419, in websockify_init
File "websockify\websocket.pyc", line 1018, in start_server
UnboundLocalError: local variable 'exc' referenced before assignment
Any idea on how to use the websockify on windows / or how to use the compiled websockify as windows executable ?
The easiest way to get websockify working on Windows is to use the Node.js version of websockify (in the other/js directory). It works perfectly out of the box, with no shenanigans required.
To address this, use the modified following commands for your example source, start from the beginning of each step and see if it helps:
Firstly, install Portable Python 2.7
You then need to modify the setup.py (It looks like this is why you are getting your first error, as you may not have defined run):
from setuptools import setup, find_packages
# add (line 2):
import py2exe
setup(name=name,
# add (line 10):
console=['run'],
Ensure the above has executed correctly by inspecting setup.py and ensure it includes run.
In your local code, import the resources module to allow you to monitor, measure and control system resources utilized by your program
import resource
Inspect your local variable exc and ensure you have assigned a value to it before calling it (I'm guessing you may have attributed a system variable to it, but python was unable to do so as you did not have resources imported, and as such it was not assigned). If you like, put up an example of your code in the comment to this response and I'll take a closer look at this part.
Back to your source guide, navigate to the websockify folder in command prompt, then execute the following to compile websockify:
[Your path to Portable Python install]\App\python.exe setup.py py2exe --includes numpy
You will now see in the websockify directory a new dir 'dist' which contains the compiled exe. An example provided is:
run.exe 5901 localhost:5900
There is also a guide here to run websockify as a Windows Service if this suits (again mentioned in your source).
----Further edit for more detail----
Open up the two files that seem to be giving you issues (websockify\websocketproxy.pyc and websockify\websocket.pyc and ensure that any reference to a variable called "exc" is referenced after it has been assigned a value (small chance of an issue if you have not yet modified these files.
I believe that your code is relying upon making and monitoring changes to the system resources (such as ports etc) and you are not allowing your code to have these permissions, so it needs the resources module. If you are calling run.exe from a program (what I called your local code) then you need to import resources at the top. If you are just straight up calling the run.exe program from a command line, then try making this new program and see if this helps. If not, send me the contents of your websockify folder and run.exe and I will take a look
# Program Name: goAndRun.py
# Code:
import sys, string, os, arcgisscripting, resource
os.chdir( 'C:\\[Path_to_the_dir_containing_run.exe]' )
os.system( '"C:\\[Path_to_the_dir_containing_run.exe]\run.exe, 5901 localhost:5900"' )
And then use the command:
python goAndRun.py
Not being in your environment, I cannot tell if this will execute exactly as I have written it. The last line may also be:
os.system( '"C:\\[Path_to_the_dir_containing_run.exe]\run.exe"', '5901 localhost:5900' )

idle-python3.2 not starting: complains of NameError: name 'Tk' is not defined

running linux mint 13
idle-python3.2 did work from the terminal before, now it does not.
Calling idle-python3.2 from the terminal throws the error below BUT I can run Idle by double clicking on the script!
Here's the very simple script that broke it all(?)
http://pastebin.com/pP9An3UU
FWIW, the script does not appear to work either (nothing happens when I run it).
Here's the error in the terminal when trying to run Idle3.2 from the terminal:
File "tkinter.py", line 2, in <module>
tk = Tk()
NameError: name 'Tk' is not defined
here's the full error
http://pastebin.com/J5AfAQyV
I know tkinter is not installed by default... but it IS installed
python3-tk is already the newest version.
python3-tk set to manually installed.
I've checked the system monitor and killed all obvious python instances running in the background.
But, still no joy.
Here's a video of the error:
http://youtu.be/oCLOJR2rktI
Your help for a fix is appreciated.
It sounds like you either a) named your little test program "tkinter.py", or b) you have another file in the same directory named "tkinter.py". If that is the case, when you do from tkinter import *, you are not importing the real tkinter but rather importing the local file.
If you named your file "tkinter.py", rename it. Also, if there is a file named "tkinter.pyc", remove it.

Python ImportError with Sage

Okay I am newer to python and have been researching this problem but I can't find anything like it so I am not sure what is going on.
I am creating a program that involves sage and it has a message cue. We have this set up on a development machine, so I know it works but I was wanting to set it up on my own computer so I could get a better understanding of how it all works and make it easier to develop for myself.
To start up sage, we run a script that calls sages main binary file and passes it an executable .py file. (./sage/sage ./sage_server.py) This creates an error in the sage_server.py file:
Traceback (most recent call last):
File "./sage_server.py", line 23, in <module>
from carrot.messaging import Publisher
ImportError: No module named carrot.messaging
But whenever I run that file just in the terminal (./sage_server) the import works fine and isn't until line 27 that there is an error when it tries to import something from sage.
Does anyone know what would cause the error when it is being called by something else? I am very lost as to what would be causing this.
Sage has its own python, separate from the system libraries. This "carrot" module, whatever it is, must be installed in whatever python ./sage_server.py uses, but not in Sage.
You should be able to use either
[your-sage] -sh
to start up a Sage shell and use easy_install, or you could get whatever carroty package you're using, find its setup.py file, and then run
[your-sage] -python setup.py install
where obviously your-sage is the path to your sage.
Things get a little trickier if the install process isn't setup.py-based.

Is the default configuration of re incorrect on macbooks? Or have I simply misunderstood something?

Python came pre-installed on my macbook and I have been slowly getting acquainted with the langauge. However, it seems that my configuration of the re library is incorrect, or I simply misunderstand something and things are amiss. Whenever I run a python script with "import re", I recieve the following error:
Traceback (most recent call last):
File "regex.py", line 2, in <module>
import re
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.py", line 4, in <module>
# re-compatible interface for the sre matching engine
AttributeError: 'module' object has no attribute 'compile'
What gives!
Pretty mysterious problem, given that line 4 in that file (and many other lines around that line number) is a comment (indeed the error msg itself shows that comment line!-) so even with the worst misconfiguration I'd be hard put to reproduce the problem as given.
Let's try to simplify things and check how they may (or may not) break. Please open a Terminal, mkdir a new empty directory somewhere and cd into it (so we know there's no filename conflict wrt modules etc), at the bash prompt unset PYTHONPATH (so we know for sure that isn't interfering), unset PYTHONSTARTUP (ditto); then type the command:
$ python -c'import re; print re.__file__'
It should emit the line:
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/re.pyc
does it? If so, then we can keep rooting around to understand what name clash (or whatever) caused your original problem. If the problem persists under such "clean" conditions then your system is jinxed and I would reinstal Mac OS X Leopard if I were in your shoes!

Categories

Resources