Posts Tagged ‘Python’

Python IDLE performs a SaveAs with every file save

Saturday, July 23rd, 2011

This is really just a note to myself and others who may encounter the Python IDLE performing a saveAs every time command/ctrl S shortcut is performed. This happened to me while working on some Python code. After looking around for what could be the issue I found that if CapsLock is turned on the save shortcut becomes a SaveAs function. Seems pretty strange to me as I see no real reason for this. Nonetheless, simple enough to get around, just mildly frustrating in the moment. Turn off your CapsLock.

Just Finished Reading: Head First Python

Sunday, June 5th, 2011

I’m usually a huge fan of the Head First series of books. They are, in general, written in a way that is geared towards efficient learning. I almost always take great joy in some of their puzzles and subtle humor. This book, was no exception up to the chapter on mobile development with Python.

At some point in this chapter nothing seemed to work. I ended up doing a lot of research trying to understand how to do now what the book was instructing me how to do then. The problem was essentially that the Android API for Python had been updated and changed fairly substantially it seems since the book was written. While this is natural for APIs and programs in general it can really ruin the teaching / learning experience for someone really new to the topic.

The resulting issue was that each of the following chapters (which I believe was three) were very difficult to follow since they were building on the mobile app development chapter that had fallen to pieces.

In the end I learned a lot about Python outside of the Android API, but there may be better general python references out there to check out.

Django Error: ‘CSRF verification failed. Request aborted.’

Thursday, June 2nd, 2011

I just got this error when trying to execute a Django application locally. A quick search on the web revealed that I needed to include the following two lines:

'django.middleware.csrf.CsrfViewMiddleware'
'django.middleware.csrf.CsrfResponseMiddleware'

in the ‘MIDDLEWARE_CLASSES’ setting in the Settings.py of the application.

In my setup ‘django.middleware.csrf.CsrfViewMiddleware’ class path was already there, but ‘django.middleware.csrf.CsrfResponseMiddleware’ path was not. I added the latter and it seemed to have resolved the error.

I’m not completely sure what these classes do in detail, but it appears to revolve around security related to the local developing environment and obviously cross-site forgery. I also noticed in the ‘django.middleware.csrf.CsrfResponseMiddleware’ class there are notes that the class was deprecated as of Django 1.4. Once I saw this I removed the path from my Settings.py file, re-ran the application, and it appears to still work. I can’t really explain it, but adding it (at least temporarily) resolved the issue so I thought I would spread the wealth.

Just Finished Reading: Django – Visual QuickPro Guide

Wednesday, June 1st, 2011

I am pretty familiar with the MVC structure and have been writing Python for a while now mostly within the context of unit and integration testing. I started to think that maybe it was time to begin looking into web development with Python. In the past I’ve done a little work with python and CGI, but decided to look into Django as a framework for building Python sites.

I read a number of reviews on this book before picking it up and many of the complaints seemed to revolve around the fact that the book does not consistently build an application throughout the chapters. While true, I believe it adds to the learning experience in that it drills the basics of creating a Django application into your head. The book has 10 chapters and each chapter is a new application. While many of the applications are simply expanded versions of the application built in the previous chapter the author to go through again the process of creating the application. While probably annoying for the advanced Django programmer, the beginner leaves the book with the ability to set up a Django application in his or her sleep. Ultimately repetition is the best way to learn, and this book is full of repetition.

The writing style was clean and concise. No real issues with versioning. All of the code seemed to work as illustrated. Overall, I was looking to get a broad idea of what Django was all about, and on a high level what I could get out of it. If you’re at all advanced with the Django framework this book will probably bore you out of your mind, but if your a Django newbie I think you’ll find this book very informative.

If I had one criticism of the book, it would be that on a very basic level the book continued to be very instructive in how to accomplish certain tasks. I like the idea that I have to create a new application and set up a new database for each chapter. But I also think that it is good to force the user to begin doing it themselves without instructions. For example after chapter 6 or so the instructions should have indicated to the user that they should create an application named ‘thisApplication’ with a database named ‘thisDB’ instead of going through detailed instructions each time.

Overall good book, and well worth the time to read.

Using Selenium 2.0 with Python 3.2 doesn’t work well yet

Wednesday, May 4th, 2011

So I recently set up a testing environment using the Selenium webdriver and Python 3.2. I had some initial issues with getting it to just generally run which I resolved and blogged here. Nonetheless, I’ve since had a chance to dig a little deeper and it turns out that there are still things in the code that did not convert well from Python version 2 to 3 using the 2to3.py utility.

As such, I’ve uninstalled Python 3.2 and reinstalled version 2.7. I’ve run some of the tests that were failing while I had 3.2 installed and everything seems to be passing. So that’s good news. I’ll keep an eye out on and updated Python binding for Selenium 2 and give it another shot once there is an update.

Using Selenium 2.0 with Python 3.2

Wednesday, May 4th, 2011

Currently the Python Bindings for Selenium are not completely compatible with Python 3.2. I’ll show here how to make the python files compatible with version 3.2 using the python 2to3 utility. I am assuming here that you have Python installed and running, you have downloaded and installed Selenium 2.0, and that you have downloaded the Python Bindings and dropped the ‘selenium’ directory into your python ‘Lib’ directory to be used as a module.

So if you have all of this installed and you run the following line in the Python IDLE

from selenium import webdriver

you will most likely (considering that the Binding code has not been updated between now and then) get an error indicating a syntax error on line 193 or thereabouts. This syntax error is occurring because the Binding code is written for python version 2.7.

Good news though. Python 3.2 comes with a utility in the ‘Tools’ directory that will convert any .py file or files from version 2 to version 3.

If you want to find the actual file you can find it in the Python32/Tools/Scripts directory. It is named 2to3.py.

So we’ll execute this utility from the command line. So if on windows do Start/Run – type cmd to open the prompt. On mac use Applications/Utilities/Terminal.

(more…)

Python permission denied error when executing a CGI script

Friday, January 21st, 2011

I’ve just started playing with python with the intent of building android apps. As such this may be obvious to the seasoned python programmer, but nonetheless was not too terribly obvious to me at first.

After creating a CGI script to run I noticed that when the browser would execute the script I would get a 200 OK response from the browser, but the page was empty. Upon checking the terminal I received the following error:

Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/http/server.py”, line 1059, in run_cgi
os.execve(scriptfile, args, env)
OSError: [Errno 13] Permission denied

The solution here is to give the script permission to be executed. To do this run the following line in the terminal (obviously I’m on a mac here):

chmod +x [name of you CGI script]

So just to keep things simple, assume you have a script in your Documents directory named ‘myScript.py’. You would execute the following in the terminal:

Documents myComputer$ chmod +x myScript.py

Essentially, this changes the mode of the specified script to be executable.

One thing to keep in mind, that dumbly wasted a few minutes of my time, is that if you have a terminal window open running the python server you cannot perform the above execution in that window. Make sure you open a new terminal window and execute on the command line.