Importing modules in python: What the hell, guys?

Posted by wanchan on September 2nd, 2016 filed in Programming

So, my team has elected to write our test automation in Python. Fine, that’s a good excuse for me to finally bother to learn Python.

Good lord have I been spoiled by Ruby all these years. I never really appreciated it, since it’s been my go-to language for the last 8ish years.

First off, I discovered that my practice scripts were using an outdated version – you have to invoke the latest version by calling “python3” on the commandline, rather than just “python”. What the hell?

Anyway, I want to do this Python thing right, so the first thing I do is go learn how one properly organizes python code. Okay, so my unit test code lives in a sibling directory to my client library I’m writing.

In ruby, I would just tell the test’s ‘require’ command the path to the library to import. In python, I have to manually add the library root to the load path, then invoke ‘import’. What the hell? I guess I know why the ruby devs added ‘require_relative’ – so you wouldn’t have to encode knowledge about the directory structure of the project, or knowledge of where the script is invoked from, into the application. Ruby has had this feature since around 2011, and it’s been standard practice to use it for nearly that long.

What the hell, guys?

Leave a Comment