SQLAlchemy with Python 64-bit on Windows

While hooking up SQLAlchemy for a web project, I kept running into attribute errors for ‘session_registry’. I figured I was doing something wrong, so I thought I’d just walk through the tutorial as a sanity check.

I was, apparently, insane.

>>> u = session.query(User).filter_by(name='ed').first()
Traceback (most recent call last):
File "", line 1, in
File "C:\dev\misc\SundanceAPI\sapi_env\lib\site-packages\sqlalchemy\orm\query.py", line 2282, in first
ret = list(self[0:1])
File "C:\dev\misc\SundanceAPI\sapi_env\lib\site-packages\sqlalchemy\orm\query.py", line 2149, in __getitem__
return list(res)
File "C:\dev\misc\SundanceAPI\sapi_env\lib\site-packages\sqlalchemy\orm\query.py", line 2349, in __iter__
context = self._compile_context()
File "C:\dev\misc\SundanceAPI\sapi_env\lib\site-packages\sqlalchemy\orm\query.py", line 2702, in _compile_context
context = QueryContext(self)
File "C:\dev\misc\SundanceAPI\sapi_env\lib\site-packages\sqlalchemy\orm\query.py", line 3247, in __init__
self.session = query.session_registry
AttributeError: 'Query' object has no attribute 'session_registry'

Tom Christensen, after joining me in some head scratching, noticed that I was on the 64-bit build of Python, and wondered if there could be anything strange with that.

Googling, we found:

TODO: Still valid? The ez_setup.py script currently(?) installs a broken version of setuptools on 64-bit Windows systems.

TODO: Still valid? To work around this you need to manually download SQLAlchemy and extract it. When extracted you need to copy the <top_dir>/lib/sqlalchemy directory into your Python<version>\Lib\site-packages directory.

Sure enough, following that work-around got rid of the errors. So thanks, openlp guys. In the end, I swapped my virtual environment out for the 32-bit variety. It feels safer there.

Getting pip into your Python 3 venv

Python 3.3 includes a built-in equivalent to the popular virtualenv tool for creating isolated Python environments. One difference with the built-in venv is that it doesn’t automatically install pip as virtualenv does. While I can understand the decision (given Python’s volatile packaging situation), it isn’t very convenient.

Here’s a Windows batch file that will remedy that, using the latest recommended way of getting pip. It depends on having curl in your path somewhere. Use it like you would virtualenv or venv, namely, by providing the path to where you’d like to create your new virtual environment.

@echo off

REM Python 3.3 pyvenv including pip.
REM See https://python-packaging-user-guide.readthedocs.org/en/latest/setup.html

if "%1" == "" goto :error

python -m venv %1
call %1\Scripts\activate

curl -O https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
python ez_setup.py

curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python get-pip.py

del ez_setup.py
del get-pip.py
del setuptools-*.tar.gz

goto :EOF

echo Specify the path to the virtual environment you want to create.
echo venv my_env

Coding Standards and Orthography

Sometimes when I bring up the subject of coding standards, I get an eye-rolling, aren’t-we-all-adults-here kind of reaction from my fellow programmers — or a fearful look anticipating endless debates about where the braces should go. Of course, “coding standards” can cover a gamut of subjects — from techniques to avoid shooting yourself in the foot to parenthesis placement — but even the little stuff matters, because writing code is largely about communicating with humans.

Continue reading

Python Packaging Demystified

IMPORTANT: Some of the information in this post is now obsolete. Please see here.

Randomly take a couple of words from the following list, put them together, and there’s a decent chance you’ll come up with something real involving Python packaging.

  • setup
  • install
  • dist
  • distribute
  • tools
  • utils
  • easy
  • py

The packaging situation is kind of confusing with all the alternate tools, libraries and methods, so I’m going to take a stab at clarifying things — for how it seems right now, anyway.

Continue reading

UI Horror: Untrimmed Validation

Hmm… this zip code looks pretty good to me.

This was on the Discount Tire mobile site. It’s awesome that you can schedule a service appointment on the web using your mobile phone. It’s not so awesome that the form validation doesn’t compensate for the trailing space that my phone’s keyboard automatically added. “Normal” people would give up when the site refused to accept such an obviously correct entry.