Ads

Thursday, February 19, 2015

The importance of underscore: Learning Flask and Python Windows Installs

Along with the broad, and highly compelling legal analysis provided with this blog, I want to also use this space to provide some insights into software coding.

In developing a legalTech App for Baselex , I decided to use the Flask micro framework. I chose this over Django principally because Django seamed a bit overkill for the simple back-end I wanted, and I wanted to get some hands on experience writing Python based server code without 10 hours of just reading documents first.

I found an excellent Tutorial for Flask here (the official one leaves a bit to be desired). Working on a Windows machine, you are always at a disadvantage since most developers are running OSX or Linux machines and have a more knowledge of the command line, and most tutorials assume that windows users do too. (FYI, you want to develop seriously, then know the command line - it makes you a ninja, use powershell)

After about an hour of setting up, googling, re-setting, and re-googling. I finally had Python installed on my system, along with pip and virtualenv. Setting up Python on Windows is not an uncomplicated process. While doing so, I learned the following

Windows Python users Pro-Tip taken from the excellent "Learn Python the Hard Way" 


  1. Configure the path correctly. Make sure you enter [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") inPowerShell to configure it correctly. You also have to either restart PowerShell or your whole computer to get it to really be fixed.
  2. I would add to this [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\Scripts", "User")

When I finally installed Python, I followed the Flask tutorial instructions for a simple test app that would start a local development server. I made my folder structure, and executed my virtualenv script and......

 this happened:

Traceback (most recent call last): File "run.py", line 2, in from app import app ImportError: No module named app

I was crestfallen. All that work and I immediately get an error that prevented even the simplest of things from working. I tried everything, I looked at the code again, and copied it exactly. I restarted power-shell, and went to the help forums where others were having similar, but not exactly the same problem. I will spare you the amount of time spent trying to figure out the solution, but here it is.

One of the files I created was an  __init__.py file that contained the app assignment. Something was wrong here. I looked and looked and looked. The only thing I could find was that I had named my file _init_.py and not __init__.py (double underscores vs. single).

With trepidation, I changed the file name and Bango! my server started, and I was off to the races.

The lesson here about coding is that, as is law, precision counts. It is not enough to have the same code, but the name of the file has to be exact or else the whole atomic clockwork of computing can't tick over.


No comments:

Post a Comment