[FAQTS] Python Knowledge Base Update -- May 28th, 2000

Fiona Czuczman fiona at sitegnome.com
Sun May 28 07:08:07 EDT 2000


Hi Guys,

Below are the entries I've entered into http://python.faqts.com today.

The knowledge base is growing really well, over 160 questions and 
answers! Thanks to everyone that has contributed and provided me with 
suggestions and feedback.

cheers,

Fiona Czuczman

[In advance: I apologise if this mail comes into the list twice, I'm 
having trouble with my mail...]


## New Entries #################################################


-------------------------------------------------------------
How do I set environment variables in a Python script that is to be run on an Apache server?
After setting environment variables in a script will they be changed after running the script?
http://www.faqts.com/knowledge-base/view.phtml/aid/3298
-------------------------------------------------------------
Fiona Czuczman
Cliff Crawford

Problem:

Say, I want to set ORACLE_HOME = /opt/app/ora/

I wrote the following code:

        import os
        os.system("ORACLE_HOME=/opt/app/ora/")
        os.system("export ORACLE_HOME")
        ....

I executed the script under my user account but
when I checked my enviroment variables with "env"
there is no ORACLE_ENV added.



Solution:

That's because os.system spawns a new process (with a copy of the
original environment of the parent process) each time it's called.  You
want to use os.environ instead:

    os.environ["ORACLE_HOME"]="/opt/app/ora/"

This changes the environment for all future os.system calls.

Once the process dies, the changed environment dies with it.


-------------------------------------------------------------
How can I embed/port Python to Vxworks?
http://www.faqts.com/knowledge-base/view.phtml/aid/3301
-------------------------------------------------------------
Fiona Czuczman
Richard Wolff

What follows are some of the details.  This same information and the 
rest of the story can be found at
  http://daikon.tuc.noao.edu/python

As usual, no warranty for fitness nor merchantability nor freedom from
un-wanted side-effects of creeping featurism.

---------------------------------------------------------------------

Here's how I set up Python as an embedded task in VxWorks.  Note that
building the system for VxWorks is not going to be as nicely set up as
doing so for most systems.  We can't run configure for one thing.  So
there are a number of changes that you have to consider/make by hand.
Before getting to the details, a thank you to Paul Fernhout for some
helpful advice in a recent comp.lang.python posting.

Perhaps the best way to get started is to build Python on your 
workstation and use that as the start of the port.  This way, many of 
the files you need will be present.  For instance, the parser generator 
program will be built and run, so it isn't needed for the cross-compile. 
 You'll also have  config.h  file that might well serve as a useful 
starting point.

The files from the distribution that you need to modify are in Include,
Modules, Objects, Parser, and Python.  At the top level, you'll need to
change Makefile and config.h.  The diff file (pydiff) will make a lot
of changes for you, but you should probably take a look at what's going
on first.  There may well be better ways of doing things.  By the way,
the diff is against Python 1.5.2 and is for VxWorks 5.2 (pre-Tornado).

You'll need (or want) to do the following:
1) Edit the top level Makefile to reflect the correct definitions for
CC, RANLIB, LD, and AR.  Make the same edits in the Makefiles in
Objects, Parser, and Python directories.  Make the same edits in
Makefile.pre in Modules.

2) Edit config.h

3) Get a version of getopt.c and put it in the top level directory, 
along with a suitable getopt.h, which can go in the Include directory.  
Or put getopt.c somewhere else and modify Makefiles suitably.

4) Edit Setup (and Setup.local, Setup.thread).

5) If you include modules that I didn't, you'll probably want to edit
that module's ".c" file.  If you have a different version of VxWorks,
look for the VXWORKS define and modify as needed.

6) Modify Modules/python.c as you will.  You probably will want to
modify the arg0 value.

7) Remove the buildno file if you want.

8) In Modules, run makesetup to build the Makefile and config.c.  If you
have used both Setup and Setup.local, run   makesetup Setup Setup.local

9) At the top level, run make.  The output will be "python.o" in this
directory.

10) Load that into your VxWorks system.  On my system, Python took too
much stack space to run under the shell, so I spawn the task, which is
what "sppy" ("spawn python") does.  If Python comes alive, but you get
lots of xxx:yyy: No such file or directory    and  xxx:yyy: File exists,
don't worry.  Those messages are from 'stat' as it probes the file
system on xxx looking for library files.  I don't know how to stop
this; it's both annoying and harmless.

10a) You should 'cd "python-top-level-directory"' before you load the
python.o file, or you should be sure that you have the Lib directory
in a standard place (or you have modified the Makefiles to reflect
suitable prefix and exec-prefix directories).  Or you might set
the PYTHONHOME environment variable in Modules/python.c before Python
goes through its initialization steps.

11) When the >>> prompt appears, it's likely that a carriage return will
bring back the shell prompt.  That's because the shell runs at much
higher priority than Python and it grabs all the characters.  It needs
to be demoted, which is what the "demoteShell" code in Modules/python.c
does (with a vengeance...there's probably no need to suspend the shell).
So run that routine from the shell, and then run Python as you will.

12) To exit, the usual command sequence (or sys.exit) will get you
back to the shell prompt, at which time, you should raise the shell
priority back to 1 (use "restoreShell").

13) You can make things more interesting by having Python code that
will allow you to run VxWorks routines.  You need a slight extension of
Python, to be found in my "Modules/vxmodule.c".  Modify Setup (or
Setup.local, rerun makesetup, and then the top level make).

14) A simple VxWorks class makes use of the vxmodule code.  See
VxWorks.py Thus, i = VxWorks.Vxw("i","") builds an 'i' such that 'i()'
gives the same task information table from the Python prompt as it does
from the VxWorks shell.  Or,  routeAdd = VxWorks.Vxw("routeAdd", "ss")
builds a routeAdd function that can be used to add a route to the
VxWorks routing table  (routeAdd("90.0.0.0, "gateway")), and so forth.

15) If you have the VxWorks module in place, you can then define
        rs = VxWorks.Vxw("restoreShell","")
and when you run rs(), you have, for interactive use, effectively
suspended Python, as the shell will get all the input characters.
"demoteShell" and 'rs' thus allows you to switch back and forth between
Python and the VxWorks shell.


Richard Wolff
May, 2000

P.S.  I did this work because I want to run various routines in the
VxWorks environment under the control of a "scripting" language.  There
are three approaches here...embed Python in VxWorks, which is what this
note is about, connect Python running on a workstation to the VxWorks
shell via telnet, or connect Python on the workstation to a process on
the VxWorks box via a socket connection.  Which way to go is nice
question.  The other question one ought to answer is, "why aren't you
using rt-linux?".

P.P.S.  All this code is released under the same terms and conditions
as the Python license.


-------------------------------------------------------------
How can I create a Tkinter MessageBox with the MessageBox - option "YESNOCANCEL"?
http://www.faqts.com/knowledge-base/view.phtml/aid/3297
-------------------------------------------------------------
Fiona Czuczman
Robert Roy

Problem:

When I use -

import tkMessageBox
tkMessageBox.askquestion("equal","even better", icon=QESTION,
type=YESNOCANCEL)

The error message here is: NameError: QUESTION

Solution:

QUESTION is not in your namespace
try
tkMessageBox.askquestion("equal",
        "even better",
         icon=tkmessageBox.QUESTION, 
        type=tkMessageBox.YESNOCANCEL)

however this will bring up another a type error: 
        keyword parameter redefined.

so then instead of using askquestion use _show

tkMessageBox._show("equal",
        "even better",
         icon=tkmessageBox.QUESTION, 
        type=tkMessageBox.YESNOCANCEL)


If you look at the sources you will see that askquestion is just a
shortcut for

 _show(title, message, icon, type) where icon=QUESTION and type=YESNO
 
ref:
def askquestion(title=None, message=None, **options):
    "Ask a question"
    return apply(_show, (title, message, QUESTION, YESNO), options)


-------------------------------------------------------------
Is it possible to set the minimum size of the window?
http://www.faqts.com/knowledge-base/view.phtml/aid/3299
-------------------------------------------------------------
Fiona Czuczman
Robert Roy

Try this:

import Tkinter
root = Tkinter.Tk()
root.minsize(400,200)
Tkinter.Button(root, text='Hello").pack()
root.mainloop()


also see
maxsize and geometry


-------------------------------------------------------------
I'm trying to use the smtplib for sending mail, and I keep getting error: (10061, 'winsock error') when I try just to connect to the localhost.
http://www.faqts.com/knowledge-base/view.phtml/aid/3296
-------------------------------------------------------------
Fiona Czuczman
Andrew Henshaw

I'm guessing that your code looks something like:

  import smtplib

  server=smtplib.SMTP('localhost')
  server.connect()


If so, then your problem is that the server.connect() function was
already called when you instantiated "server".   The connection is 
automatically made when you provide the hostname as a parameter to SMTP.

I believe that the SMTP example in the library documentation is in
error, in this regard.







More information about the Python-list mailing list