Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, September 26, 2013

running RedNotebook journal 1.7.1 on XP and Win7


If you are trying to run the RedNotebook 1.7.1 journal / diary on a Windows desktop you may have hit the same problems which I have had to resolve on XP Sp3 and Win7 Home Premium with Sp1.

If your Win XP environment contains a PYTHONHOME variable then you must set it to something inocuous such as a dot for the current directory of the Rednotebook.

I am doing this with a call to X-RedNotebook.exe in a 3-line BAT file which I summon from a 2-line VBS script that has a shortcut on my desktop ( this keeps a daft black DOS shell window from hanging around.)

On Win7 Home Premium Sp1 I could not get 1.7.1 to run as the X-RedNotebook variant until I had dragged in copies of the DLL's of which it was complaining as missing.

If you need details, drop a note.

If your upgrade to 1.7.1 went without a hitch, I would love to know the secret !




Tuesday, December 7, 2010

The path to BFG: virtualenv bfgenv and _socket PYTHONHOME PYTHONPATH activate

The BFG python web framework folks pride themselves on their testing - but I can't say the same for virtualenv when installed under Windows.  And in this regard, the BFG web documentation is lacking (compared, say, to WebCore.)

First, the point of virtualenv is to be able to maintain a BFG server on a machine with python26 and python27 and python31  as we transition to python32 and beyond.  So they move a copy of some version of python.exe and pythonw.exe into a Scripts folder.  But they neglect to supply the respective python DLL. So copy that from your PYTHONHOME into that new bfgenv/Scripts directory.

The scripts will not work correctly if you do not first clean up your cmd environment by executing the following at that DOS shell prompt:
set PYTHONPATH=
but even then I have found that for my sanity I need to go into the new /bfgenv/Scripts and edit the activate.bat and deactivate.bat files to preserve and restore my %PYTHONPATH% as well %PYTHONHOME%

One symptom that there is a problem is having an install script complain about not finding a module which imports without problem in your 'normal' python environment.  In my case it was _socket

Sadly, I read advice on mail lists to reinstall Python.  "There must be something wrong with your Python install" as you will see by a web search on virtualenv _socket

Here is a set of steps which will work on Windows XP for Python 2.7 if \python27\Scripts is on your PATH
cd \python27
python ez_setup.py            (note: get this python script off the web for SetupTools)
easy_install virtualenv
set PYTHONPATH=
cd \
virtualenv --no-site-packages bfgenv
cd \bfgenv
Scripts\activate
Scripts\easy_install -i http://dist.repoze.org/bfg/current/simple repoze.bfg
cd Scripts
python test_bfg_port_28686.py   (note: this last is your test file - remember to choose a port)

Suppose PYTHONHOME had been conceived as a string such as
"27*c:\python27;31*i:\python312"
then this would not be an issue - so long as PYTHONPATH was not built by subst of %PYTHONHOME% or $PYTHONHOME

But that ship has sailed.

And take care when in \bfgenv to issue the command as
Scripts\activate.bat
because, like me, you may have more than one activate.bat in your path ... I mean, %PATH%.

Here is a 2009 blog post on using virtualenv: Chris Scott

The WebCore pages suggest using pip. And what could be simpler than
pip install CherryPy
PS
Of course, BFG on linux installed and ran like a charm ...
PPS
Check that %PYTHONHOME%\Scripts is on your PATH

Thursday, December 2, 2010

Curl web content on a CherryPy web server

Here are two snippets from a Python CherryPy web server test run:

    def index(self):
        cherrypy.response.headers['Content-Type'] = 'text/vnd.curl'
        return """{curl 7.0 applet}{br}{paragraph Test CherryPy}{br}"""
                   
    index.exposed = True
  
    def home(self):
        cherrypy.response.headers['Content-Type'] = 'text/vnd.curl'
        return """{curl 7.0 applet}{br}{paragraph Curl Test for CherryPy}{br}"""
  
    home.exposed = True

 It is also possible to specify a path to static Curl files and an index to that directory - in which cases files could appear with .CURL extensions.

In the two cases above, the working URL's are
http://localhost:18787/
http://localhost:18787/index
http://localhost:18787/home
and the Curl content displays as intended in linux (Ubuntu maverick) FireFox.

This test run was done using CherryPy 3.1.2 but it would only run on Python 2.6.6 as of today

The test daemon was launched from a local code folder using
sudo python /usr/local/lib/python2.6/dist-packages/cherrypy/cherryd -c ./curl/site.conf
That ./curl web app folder was modelled on the CherryPy3 scaffold folder in their python module distribution (which also includes a working ./tutorial for HTML content.)

Although I was setting a content-type globally for cherrypy.server I found that I still had to flip the MIME type in the individual 'page' methods as you see above in order to send a response with "text/vnd.curl".
Solution: tool decorator

@cherrypy.tools.response_headers([('Content-Type', 'text/vnd.curl')])
def curlHome(self): { // etcetc
//or
class Root:
  @mimetype("text/vnd.curl")
  def someView(self):
// using
def mimetype(type):
    def decorate(func):
        def wrapper(*args, **kwargs):
            cherrypy.response.headers['Content-Type'] = type
            return func(*args, **kwargs)
        return wrapper
    return decorate
// thanks to 
http://stackoverflow.com/users/11549/ddaa

 

Wednesday, December 1, 2010

with statement

Just as JavaScript 5 strict mode bars the with statement, Python 3.1 extends the reach of the with statement.

The two languages are poised to see more and more use as Python extends its reach in linux and as new frameworks revitalize JS on the server-side.

Perhaps the next JS will give a role to the reserved word goto ? No. The ECMAScript spec has dropped goto as a word which may not be used to name an identifier ( ECMA-262 5th Ed.)

Tuesday, November 30, 2010

configure & make node.js under Cygwin

Just one oddity with building node.js on cygwin (it was fine Ubuntu) and that is that I could not get ./configure to complete until it occurred to me to run
unset PYTHONHOME
unset PYTHONPATH
After that the
./configure
make
make install
ran without a hitch and node/js behaving much as it does on linux. 

The python module path issue was not becuase cygwin was using my Windows values: a simple bash check with
echo $PYTHONPATH
echo $PYTHONHOME
cleared that up.
The symptom could be seem by just running a verbose python REPL at thecommand prompt
python -v
and noting the "site" package error.  Yet loading sys and doing
print sys.path
showed no obvious problem.  I have seen this gift once when a linux version upgrade required a python version upgrade.  I checked in the registery under LOCAL_MACHINE | SOFTWARE but the three values there for python25, python26 and python31 look fine (although the latter appears truncated ...)

I tried various export statements and looked for a doubtful resource rc file or other config in /etc but the answer (porbably obvious enough) eludes me.

When set and export to reasonable values and to no value fail and there is nothing else odd reported by env then unset to the rescue.

Now back to looking at axiom and sproutcore as server-side JavaScript options.

Monday, September 21, 2009

Python + Tkinter and Python + Gtk

Remembering that Python was intended as a scripting language, compare these two HelloWorld from Python.

First, Tkinter for Tk as in Tcl/Tk

from Tkinter import *
root = Tk()

class HelloWorld:

def __init__(self):
  widget = Button(root, text = 'Hello World', command = self.quit)
  widget.pack()

def quit(self):
  print "Hello World"
  import sys
  sys.exit()

HelloWorld()
root.mainloop()

Now to compare the same minimal HelloWorld with Gtk.  First, install Gtk+  ( and on Windows XP  that means get all of the dependencies - the DLL's - on the path.)

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:
def hello(self, widget, data=None):
print "Hello World"

def delete_event(self, widget, event, data=None):
print "delete event occurred"
return False

def destroy(self, widget, data=None):
   gtk.main_quit()

def __init__(self):
   self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   self.window.connect("delete_event", self.delete_event)
   self.window.connect("destroy", self.destroy)
   self.window.set_border_width(10)
   self.button = gtk.Button("Hello World")
   self.button.connect("clicked", self.hello, None)
   self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
   self.window.add(self.button)
   self.button.show()
   self.window.show()

def main(self):
   gtk.main()

if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

Whatever the merits of Gtk+ as a cross-platform C GUI framework for general purpose languages, something looks to have gone wrong with the Python library PyGtk.

Even with Tk itself, something comparable occurs when using Tk from Haskell versus using Tk from OCaml.  Probably  expect was the Tcl "killer app" but Tk is what most developers know of Tcl today.  Tk was also an early mainstay of Ruby.  Here is the HelloWorld demo using Gtk+ from the Ruby-Gnome team:

# Copyright (c) 2002,2003 Ruby-GNOME2 Project Team
require 'gtk2'

button = Gtk::Button.new("Hello World")
button.signal_connect("clicked") {
    puts "Hello World"
}

window = Gtk::Window.new
window.signal_connect("delete_event") {
    puts "delete event occurred"
#true
   false
}

window.signal_connect("destroy") {
   puts "destroy event occurred"
   Gtk.main_quit
}

window.border_width = 10
window.add(button)
window.show_all

Gtk.main

Now this is a little better, but compared to Ruby + Tk we are at almost double the lines of code.

To see a really elegant use of Tk, see the QTk framework for Oz, the language.  I'm fiddling with a QCurl variant for Oz just now.  I added some notes on OCaml and labltk over at wikia.com

Thursday, September 10, 2009

Converge programming language 1.1 "alpha" binaries available

Laurie Tratt's Converge, an ICON for pythonistas has gone to 1.1 over at convergepl.org

This comes just as Robert Parlett's ObjectIcon has popped up to 1.4 at code.google.com

I am still waiting for news to clarify the status of ICON 9.5 ... which Carl Sturtivant of UMN, for one, has been working on over the past year or more.

I have started a sketch for a Converge docs site-specific browser at converge.aule-browser.com

At the moment it's only note-taking link is to cl1p.net/convergepl

A converge DSL on the server-side would be excellent for scripting a dynamic GUI in Curl code.