Where is Python 3?

A number of readers have been sending me e-mail about Beginning Programming with Python For Dummies and why I chose to use Python 3.3 instead of one of the Python 2.x versions. In general, I believe in using the most up-to-date version of a language product available because that’s the future of programming for that language. So, it wasn’t too surprising to me that I noted in a recent InfoWorld article that Fedora 22 will have Python 3 installed by default. I’ve started noticing that Python 3 will be the default with other products and in other environments too. Choosing Python 3.3 for this particular book looks like a really good choice because anyone reading it will be equipped to work with the latest version as it becomes adopted in a wider range of environments.

I do talk about standard Python in Professional IronPython. Of course, this book is targeted toward IronPython users, not Python users, but talking about standard Python and how you can use both libraries and utilities from it seemed like a good idea when I wrote the book. You need to remember that a solid version of Python 3 wasn’t available at the time I wrote this book and that Python 2 was really popular at the time. If there are readers of this book who would like me to create a series of posts that discuss using Python 3 libraries and tools with IronPython (assuming it’s possible), you need to let me know at John@JohnMuellerBooks.com. I try to accommodate reader needs whenever I can, as long as there is an interest in my doing so. At this point, I haven’t had a single reader request for such support, which is why I’m making a direct request for your input.

This leaves my current book project, Python for Data Science for Dummies. It turns out that the Data Science community is heavily involved with Python 2. My coauthor, Luca, and I have discussed the issue in depth and have decided to use Python 2 for this particular book. The limitation is that the libraries used for Data Science haven’t been moved to Python 3 completely and the entire Data Science community still uses Python 2 exclusively. If it later turns out that things change, I can certainly post some updates for the book here so that it remains as current as possible.

Python is an exception to the rule when it comes to languages. There are currently two viable versions of the language, so I can understand that some readers are completely confused. I encourage you to contact me with your thoughts, ideas, and concerns regarding the use of specific Python versions in my books. I want you to feel comfortable with the decisions that I made in putting the books together. More importantly, your input helps me decide on content for future books, articles, and blog posts. Unless I know what you need, it’s really hard to write good content, so please keep those e-mails coming!


Checking the VBA Environment

Microsoft has made some significant changes in VBA 7.0 that aren’t discussed in VBA for Dummies because the book came out before Office 2010 was released.  All of them are good and necessary changes for the environment that Office 2010 supports. Of course, one of the biggest changes for Office 2010 users is that Office now supports both a 32-bit and a 64-bit environment, so you need a way to check for this condition. It turns out that all sorts of programming functionality changes in a 64-bit environment, including the use of Windows API pointers. Calls to the Windows API that worked just fine in previous versions of VBA may not work in VBA 7.0 when you’re working with a 64-bit version of Office.

Because users often install software that support doesn’t know about, you can’t assume that users will follow the rules and use only the 32-bit version of Office 2010. As a consequence, you must make some changes to your code to ensure it handles any environment thrown at it. Fortunately, Microsoft also makes it possible to check the environment using the following code pattern.

Sub CheckVBA()
    ' Check for VBA 7
    #If VBA7 Then
        ' Determine whether this is a 64-bit environment.
        #If Win64 Then
            MsgBox "VBA 7 running in 64-bit Office"
        ' This is a 32-bit environment.
            MsgBox "VBA 7 running in 32-bit Office"
        #End If
    ' This isn't VBA 7.
        MsgBox "Older version of VBA"
    #End If
End Sub

Notice the use of the specialized If…Then construction. The #If statement differs from the standard If statement. Because VBA7 isn’t defined in older versions of VBA, the #Else clause is executed. Likewise, only the 64-bit version of Office defines the Win64 constant, so the 32-bit version of Office 2010 will execute the #Else clause.

It’s essential to understand that the Win64 constant refers to the version of Office, not the version of Windows. You can run the 32-bit version of Office 2010 on a 64-bit Windows system without any problem. When you execute this macro on a 64-bit Windows system with 32-bit Office 2010 installed, you’ll still see the “VBA 7 running in 32-bit Office” message. When you think about it, this response makes sense because the version of Office is important, the version of Windows is less so.

Of course, there are times when you also need to know which version of Windows you’re using. The check is easy. Make sure you use an environment variable that only appears on the version of Windows you’re using. Checking for 64-bit Windows is easiest using this code.

Sub CheckWin()
    ' Check for a 64-bit-specific environment variable.
    If Not Environ("ProgramFiles(x86)") = "" Then
        MsgBox "Running on 64-bit Windows"
        MsgBox "Running on 32-bit Windows"
    End If
End Sub

The “ProgramFiles(x86)” only comes with 64-bit Windows. Now that you know how to check the various VBA 7.0 permutations, I’ll look at some VBA 7.0-specific programming in my next post. Let me know if you have any questions or suggestions at John@JohnMuellerBooks.com.