Unable to see os.environ['COLUMNS']

Tim Chase python.list at tim.thechases.com
Mon Sep 15 14:26:06 EDT 2008


Thomas Bellman wrote:
> Tim Chase <python.list at tim.thechases.com> writes:
> 
>>> $ export COLUMNS
>>>
>>> $ python -c "import os; print os.environ['COLUMNS']"
>>> 80
> 
>> This works well, and also manages to keep up to date across runs
>> as window-size changes.
> 
> Now try this:
> 
>     $ export COLUMNS
>     $ python -c "import time, os; time.sleep(60); print os.environ['COLUMNS']"
> 
> and change the window width during that sleep.

Yes, I did try something similar in my experimenting:

   $ export COLUMNS
   $ python
   >>> import os
   >>> print os.environ['COLUMNS']
   80
   >>> # resize to some other width
   >>> print os.environ['COLUMNS']
   80

However, for my particular use-case, it's merely for output 
formatting of a short-running process (akin to "svn status" 
output).  If you resize it in the middle of the sub-second 
operation, you deserve what you get :)

It is disappointing that something so cross-platform as "what's 
my output width" isn't built-in, requiring jumping through hoops, 
in ways that aren't cross-platform.  The ioctl() method worked on 
my *nix-like boxes, as did the ncurses versions.  However on 
Win32, neither worked:

   C:\Temp>python
   Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
   [MSC v.1310 32 bit (Intel)] on win32
   Type "help", "copyright", "credits" or "license"
   for more information.
   >>> # try the Curses version:
   ...
   >>> import curses
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
     File "c:\Program Files\Python24\lib\curses\__init__.py",
     line 15, in ?
       from _curses import *
   ImportError: No module named _curses
   >>> # try the ioctl version:
   ...
   >>> import fcntl
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ImportError: No module named fcntl
   >>> import termios
   Traceback (most recent call last):
     File "<stdin>", line 1, in ?
   ImportError: No module named termios
   >>>

So for cross-platform'ness on Win32, you wander over here:

http://code.activestate.com/recipes/440694/

Abstracting all this mess in a cross-platform sort of way would 
be a nice "batteries included" tool to have.  Based on the other 
thread that Grant directed me to and the comments in the Win32 
page, I'm not the first to have hoped for such an built-in.

Fortunately, for now I'm mostly focused on the *nix side of 
things and have enough to get it working for now.  Thanks to 
those who gave their input.

-tkc










More information about the Python-list mailing list