So You've Fucked up your Python Path

A timeless hazing ritual for new Python devs, and how to fix it

I remember back to when I first learned Python. It was a strange decision for a happily employed post-graduate to make, especially for a time when many were screaming for the death of the language with Guido's (outrageous?) grand reveal of Python 3.  The Ruby on Rails guys seemed to be doing just fine. Those were the days.

After weeks of sweating over a keyboard in the basement of an illegal BedStuy hostel, I had finally set out what I had hoped to achieve. It was the greatest credential any programmer could possibly strive for: yes ladies and gentlemen, none other than yours truly became an officially recognized licensed professional: I had just completed the last Python course in Codecademy.

Congrats! You know nothing.

Armed with this new unfathomable knowledge, I was ready to take on the world. I did have a few gaps in my knowledge, such as:

  • Experience with Linux
  • General idea of what a terminal is and why anybody would use one
  • Basic understanding of the internet
  • Motor skills needed to survive

That aside, I was determined. Nothing could stop me, which turns out to be a really bad attitude when you're SSHed into a VPS with root access, and zero hesitation to wreak havoc upon any and all system files. You see where this is going.

Dear Stack Overflow: I Think I Deleted Python

Disabling your system's native Python version, whether via deletion or a misconfigured PATH, is a coming-of-age cliché as timeless as losing one's virginity on prom night. Young developers flock to Stack Overflow pleading for help, while those senior enough to reply sigh a gasp of nostalgia before reassuring them that their efforts are pointless. "Ah yes," they reminisce, "I remember my first devastating life-altering failure. To be young again."

If you're lucky enough to be unaware, UNIX based systems depend on their natively installed version of Python to run, well, almost everything. If Python is unavailable for just a moment, the developer loses the ability to use:

  • vi
  • vim
  • nano
  • grep
  • source
  • wait, SOURCE?

Yep, the command you'd normally use to relaunch a corrupted startup file (such as the one that holds your PATH variable) is totally unusable. Not that it matters, what are you going to do, edit it? Oh right, you just ruined every bash text editor. I'd actually be more interested in seeing what DOES work without Python configuring on Mac/Linux, other than moving up and down your file structure helplessly, trying to quantify the damage you just wrecked upon your work, life, and career.

So, Should I Kill Myself?

Calm yourself and stick with me here; I have no interest in writing long-winded posts without providing any sort of solution. That said, I have no problem making you sit down and think about what you've done while I bother getting to the point. Nobody else has fixed this for you yet, and I need to improve my site's metrics, so it seems like we need each other on this one.

Besides, don't be so hard on yourself. Not even a week ago, I sat with a Senior Developer to review my development stack, and how that would fit into the current ecosystem of the department. Naturally I explained that I prefer running Flask on Python 3.7, as I adjusted my motorcycle jacket and sat forward to casually   extinguish a cigarette on his desk. Even through my vision was obstructed by the timelessly classic aviators resting on my face, I could see the letters roll across the terminal window before him as he typed. The poor bastard running a 4-year old Linux distribution had just checked if Python 3 was installed on his live production instance, when his fingertips unleashed the unmistakable export PATH=..., seconds away from linking CentOS' native Python from Python 2.7 to Python 3.7. My exact reaction was something along the lines of "OH GOD NO DON'T DO THAT STOP HOLY MOTHER OF JESUS FUCK!"

He stopped typing. The day was saved, and I was able to explain the importance of leaving native Python versions intact on the systems they come installed on. Then, I myself fucked up the Python path maybe an hour later. On production.  

We all do dumb things sometimes. That doesn't mean you're dumb. Well, except in my case, having done this countless times before. Some of us simply like to live dangerously. Fortune favors the bold, and so on.

Getting out of this Mess

Now that we've hit rock bottom, where do we go from here? Well, two things to keep in mind:

  1. Not everything is broken without Python. Extreme foreshadowing.
  2. Restarting a terminal will automatically run all startup scripts.

By now you know that .bash_profile and .bashrc are critically important to your system, considering how badly you've fucked things up just now. These files set important variables for your system every time you open your terminal; more specifically, .bash_profile kicks in on any sort of user log in (such as SSH) where .bashrc fires every time a new additional terminal window is opened. Even if your startup files are corrupted, the system will always love them and turn to them regardless of how horribly disfigured you left them. Talk about a spark of hope.

On Mac OSX

I'll admit I'm a bit embarrassed at how long it took me to realize this: even though you ruined every text editor known to man, there's one resilient enough to hold strong. The name? TextEdit.

In your root directory, a combo of  Shift + Cmd + . displays all hidden files. Check out bash_profile. Fix it, save it, reopen terminal. Cancel the suicide party. Pop the Champagne.

On Linux

No GUI can save us now, but what can? Perhaps a command so stupid, so simple, that it couldn't possibly need Python to work:

echo 'export PATH="/YOUR/ORIGINAL/PATH/2.7/bin:${PATH}"' >> ~/.bash_profile

This appends the text you provide to the end of an existing file. .bash_profile and .bashrc both only pay attention to the last exported PATH in the document, which means the rest of your file will work, and the only PATH which is respected is the one which you've presumably entered correctly this time around. Restart your terminal. Get back in the game son: it ain't about how hard you hit, but how hard you can get hit and keep moving forward.

Sidenote

We're not exactly conducting rocket science here (data science is a close second perhaps? Just kidding. We barely know what's going on most of the time). I felt compelled to write this post for two reasons: one being how common and destructive this pitfall can be for most people, but more importantly, the knee-jerk reaction veterans have in response to this problem is "good luck, you're fucked."

I'm not here to comment on the integrity of our fine anonymous internet communities, but the discrepancy between how devastating losing a server can be doesn't seem to met with much urgency by anybody with insight. Nobody taught me how to work around these issues. Had I listened to anonymous internet advice, I probably wouldn't let entire servers of sensitive data for dead, including my personal machines. I worked through it, and quite frankly, I'm kind of a fucking idiot, as is our mission statement. If an idiot who majored in nonsense and learned a programming language before learning Linux can work through this, I would expect the same of those with much more intelligence than I to, at the very least, attempt the same.

Author image
New York City Website
Product manager turned engineer with an ongoing identity crisis. Breaks everything before learning best practices. Completely normal and emotionally stable.

Product manager turned engineer with an ongoing identity crisis. Breaks everything before learning best practices. Completely normal and emotionally stable.