Getting into Python: A Guide for Networkers Part 2

This is a very subjective piece about what worked for me when I first tried my hand at getting into Python as a networking chap. It comes with the disclaimer that your experience may be wildly different.

Commit

Firstly, and most importantly, commit to learning Python, keep focused on that, block out the noise.
The event that prompted me to initially write this blog was a discussion I hear about whether networkers need to, or at least should, learn to code. This was 2017. I found the conversation frustrating because I could imagine if I hadn't started, or I was struggling with my study, then I might find some excuse here to ease up or stop, happy in the knowledge that other networkers were not learning so why should I sweat it?
My advice is stop listening to the pontificating and get on with learning. There are undeniable benefits1 from learning to code and Python is the de facto language for networkers. Do not waste time with the 'should I, shouldn't I?'.
I've heard first hand from people that hated programming in college and got into networking to avoid it. That's fine, it isn't for everyone. However if you're interested in getting involved in all this Python chat then commit and get going.

"Stay on target"

Secondly, once you've committed to Python do not get distracted by the 'learn go', 'learn ruby', 'learn xyz' that you'll hear on twitter. Starting on a second language before you've reached a confident level with the first will just lead to more frustration. Keep focused on Python until the grind of learning all the new concepts of the language becomes less arduous and you start to enjoy it, connecting the dots between the different concepts you are learning.
Don't pick up another language because you want a change or are bored with the grind. If you are in that mindset you are not ready yet to change direction and will probably just hit the wall with another language once the initial fillip of learning some new and shiny has worn off.

Python3 or Python2

"Python3, next question!"

Longer answer, Python3 is the future, most tutorials I've seen in the last couple of years use Python3.
The most important thing is to not waste time debating this or getting confused over one or the other. Do not let this put you off. There's a good break down of py2.7 versus Python3 here.
If you are going to be working in an environment that already uses a lot of Python in their work practices, you may find that code is written in Python2 and so would need to be aware of it. However, for the majority of networkers learning afresh there isn't this concern and so can just jump in at Python3.

Learn Python from Python tutors

You may be thinking to yourself, 'I only need Python for networking tasks' and so seek out Python tutorials from network vendors or bloggers. Scotch those thoughts. Learn from the experts with years worth of coding experience. Do not look for shortcuts to get to the stuff that interests you as a networker. You need to grasp the basics of the language first and look to experienced tutors of the language proper to help you.
Once you start finding your way then by all means look to consolidate what you know with relevant examples from the world of networking (netmiko as mentioned in the previous post is a great way to start). While difficult and time-consuming, building a strong foundation in the Python language itself is vital before you look to apply these skills elsewhere. I'm going to build a recommended Python reading list as a separate post so I can easily go back and edit it as my recommendations evolve.

Build a Dev Machine

This section could also be called 'Build a Linux Dev Machine' You are going to need an environment in which to play. Python runs in on Mac, Wins and Linux. If you like to spend money that could be on a Mac. Using windows is probably feasible but I've little experience.
Python and Linux work elegantly together and when you are building a dev box you'll find that there are a whole raft of tools that you'll want running on the machine such as an editor and IDE, or processes that you come to rely on, such a git for version control. For me, Linux is the best option.
Now some would call me a charlatan, I run a Linux VM on a Windows PC and do all my Python in the VM. The reason is that all the 'worky-type-stuff' (official title) runs on Wins so I have my email and powerpoint covered without having to figure out integrating them into Linux. Plus the commonly used versions of Linux use Python as part of the operation of the OS itself. As a beginner you're probably going to screw something up. Working in a VM provides the ability to consign a broken machine to the darkness of the pit and rejoice in a fresh new victim. Delete from disk and respawn with minimum effort. Please note I understand the real answer here is to use virtual environments for playing Python, but that's not exactly for absolute beginners.

Side point: Invest time in git

But if you are just nuking VMs what about all those saved scripts and notes? Ok so I understand this sounds like yet another thing to learn but if you combo your Python study with a sprinkling of git you can adopt a light-weight workflow for backing up your work off-machine. Trust me, git is horrible to start with but in time it feels like gamifying the code writing process. There's something satisfying about that push confirm message. Plus it's versioning control that actually engaging, which is amazing in itself. But I'm getting ahead of myself, I'll do a git blog soon, for now, back to Python.

Pick an IDE

Once you have your machine of choice and Python installed you can just dive right in using a basic text editor to write code and Python's built-in shell or REPL. But that is dark ages stuff, you might think that when learning a new language it is best to endure the purgatory that is typing out everything in full. However, if you value your time, auto-completion and PEP8 prompts are heaven-sent. For this, and many other benefits, you need an editor or IDE (Integrated Development Environment).

There are a number of different options, here's a selection:

  • Pycharm - released by jetbrains as a pro edition that requires payment and a free community edition, which is the one I use and my go-to IDE. Pycharm is a dedicated Python IDE and, as you would expect contains, a huge amount of features to make writing Python that much easier, all from first install. I've seen complaints about the Windows-like menu structure (loads of layers and tick boxes), which is true but I think investing time in this product is worth the effort and once I found my way around it is an invaluable tool. I've also seen criticism for its bloated feel, it is partly written in java but my impression is that pycharm has improved greatly over the last few years and I enjoy using it.

  • Sublime Text 3 - slick and good-looking this text editor is popular with full-time developers. It isn't dedicated to Python and must be customized to come anywhere near to pycharm's basic feature set. It is one of those weird products that is not free to use, although it is free to evaluate, and there is no time limit to the evaluation period. So it isn't free. But you can use it for an unlimited time. So is it free? No!

  • atom.io - In development from the github team this one used to be pretty flaky in my experience, I sometimes fire up the latest build to check how it is getting on. It is one of those products that looks super-cool from the blog, it has the attractive dark themes similar to Sublime Text and is free without caveats. Again, it is a multi-language editor so it needs a host of plugins to match pycharm's Python shortcuts. As to be expected from a github product, the latest version appears to be making a fanfare about its git integration.

Stay Focused

Thus you've got options. Pick your dev machine, pick your IDE and get into the study. A word of caution here, don't be the busy fool. There are a huge amount of options here, which OS to use, if Linux, which distro of Linux, ubuntu, fedora, debian, centos? Which desktop? gnome, KDE, unity? Which editor, which theme, which font? On and on it goes. I find that with so many OS, colour schemes and tweaks to choose from I sometimes waste valuable time tinkering with my OS & IDE rather than getting on with my real aim, learning Python.
Sure, playing around is fun but if you want to make progress you need to resist the urge, pick a Python dev setup and stick with it for a week or so before going back through those 'Preferences'.

Little and often gets results

I've already mentioned that I think of learning Python in a similar vein to exercise. To make progress I need to keep dipping in and doing some on a regular basis. For the novice runner attempting a 5k four times a week is going to be a lot more enjoyable than one big 20k on a Sunday. The same goes for coding. Your brain can only absorb so much in one sitting but you'll be surprised how quickly it recharges and is ready for some more the next day. I suggest trying to do 30 mins most days, including weekends if possible. I found that worked for me, just keep on typing out those exercises, even if you do not understand the concepts. Keep banging your head against it and one day you'll get that satisfy feeling of 'oh yeah, THAT'S how it works'.
If you keep at it, you'll find that you naturally become better at each new task presented to you, you're interest levels rise in the language as a whole and that 30 mins of grind becomes more something you want to do, than have to do. Once you get into a virtuous circle you'll find yourself doing more than the 30 mins. Admittedly, one heavy session of Friday night drinks after work means you'll wake up feeling like you've forgotten it all but keep at it and the results will come.

That's all I've got, if you've read all this and you still don't feel like learning Python, I've got nothing to tell you.

For those that do, good luck.

Comments: @joeneville_

  1. When I said there are 'undeniable benefits' from learning Python in a conversation recently I was rightly challenged on this. I'm adding this in because I don't want this post to be another one extolling the virtues. At the most basic level for a network engineer is you know a bit of Python and you've got a large config to build that you need to say dish out /30s ip addresses to each of the 48 ports. A simple script means you can do this in a few minutes. Python makes those mind-numbing repetitive tasks easy to perform and repeat. Everything else from there is an additional boon.