Creating a Python workspace in MacOS Sierra v.10.12.6

Making yourself a workspace on your Mac to start writing Python code doesn’t even seem necessary at first glance, since Python comes with MacOS X. It’s true that Python does come preinstalled on a Mac. But, that install turns out to be rather useless for coding in Python. Apple may not keep the runtime environment up to date, but when they do upgrade the OS, the upgrade may wipe out all the site packages you’ve added to the preinstalled version of Python. Also, adding third party libraries to the system version of Python is a repetitive pain of telling your Mac it’s OK every time and making sure all permissions end up in correct mode. Furthermore, at the current time (August, 2017) there are two versions of Python in heavy use, 2.7 and 3.6. Python v.3.6 is not backward compatible with code written for 2.7 – which is a lot of code! And Python v.2.7 will not run all code written for 3.6 – also a lot of code, and the current and growing version. As you dig into creating your Python coding environment properly for you Mac, you find out that you’ll need to have 3 (at minimum) versions of Python running. The system version, the 2.7 version, and the 3.6 version – and that these need to be kept isolated from one another so libraries and packages for one don’t mix in and corrupt those for another. The system version needs to be left alone, exactly as any apps or system software that might need it expects it to be. Libraries and packages and environment for 2.7 and for 3.6 are incompatible and require isolation from each other. System wide sharing of one Python environment is not possible.
The folks writing documentation and instruction for Python users have a fuzzy consensus for the basics of what a solution for this looks like. It involves installing Xcode, an Apple IDE that has some tools you need; Homebrew, a third party package manager; using that to install fresh new copies of Python v.2.7 and v.3.6; pip, a Python package manager that comes with Python 3.6 as pip3 but a different version, pip2 needs manual install for Python 2.7; virtualenv, a tool to create virtual environments that are like separate shells that can’t see inside each other for the two Python versions you’ll be coding in; virtualenvwrapper which is an ease-of-use wrapper around virtualenv; pyenv, which is like a new version of virtualenv that comes with Python3. Along the way of installing these different pieces of software you’ll be making changes to your .bash_profile with additions to the PATH environmental variable, add a new environmental variable called WORKON_HOME and you’ll initialize virtualenvwrapper and pyenv. You’ll make a directory to hold your virtual environments called ~/.virtualenvs and you’ll create and activate a couple of virtual environments. Once that’s done you’ll go inside each these virtual environments and add whatever tools and libraries and packages you want for your Python programming convenience and needs. And that brings up a flaw you will run into – you can’t really run IDLE, the sort of basic, no frills free IDE that many use for writing Python, from within these virtual environments. IDLE wants a different version of Tkint, the tcl/tk language and libraries than what comes included in the Python installation packages, where it’s hard-coded. Outside the virtual environments you can install the correct version, but IDLE can’t see outside the virtual shell and won’t see it. You can’t install it inside the virtual environment, for reasons I’m frankly not very clear on, I just know it didn’t work by any of the usual methods when I tried, and many others have brought up this issue. You can add a flag (—system-site-packages) to virtualenv to make outside packages visible when you make a new virtual environment, but I didn’t like that. I don’t like IDLE very much either, so rather than spend time figuring out why it doesn’t run without complaining about Tkint, I just downloaded a different, nicer IDE called PyCharm which runs just fine – you just give each project the path to the Python version inside the virtual environment you want for that project, and everything’s copacetic.

I did not go into great detail in this article, no step-by-step “now type this and press enter” type stuff. I feel that would be very redundant, since there are several such scripted installation how-to’s already published – I used some of them myself. But for the convenience of cst’s readers and website visitors, I bookmarked the sites I went to as I was figuring all this out – here they are:

Add Color to the Terminal in Mac OS X

Installing Python 2 And Python 3 Alongside Each Other On Apple Mac Os X With Virtualenv And Virtualenvwrapper | Joe Bergantine | Designer

add python 2.7 to path mac – Google Search

osx – How to setup python path for 2.7 on mac? – Stack Overflow

sitecustomize.py and usercustomize.py mechanisms – Google Search

site – Site-wide configuration – Python Module of the Week

Python Imports — Reference Pages 0.2 documentation

Installing Python 2 on Mac OS X — The Hitchhiker’s Guide to Python

osx – Add to python path mac os x – Stack Overflow

4. Using Python on a Macintosh — Python 2.7.13 documentation

mac os x software update – Google Search

Check and Install Mac OS X Software Updates from the Terminal

running python 2.7 and 3.6 on mac os x – Google Search

osx – Switch between python 2.7 and python 3.5 on Mac OS X – Stack Overflow

Homebrew — The missing package manager for macOS

Installation — Homebrew

osx – Installing Python 2.7 and 3.6 Side by Side on Mac – Stack Overflow

Installing Python 2 And Python 3 Alongside Each Other On Apple Mac Os X With Virtualenv And Virtualenvwrapper | Joe Bergantine | Designer

Installing Python 2 And Python 3 Alongside Each Other On Apple Mac Os X With Virtualenv And Virtualenvwrapper | Joe Bergantine | Designer

Install Python3 on OS X, virtualenv & virtualenvwrapper

Install Python 2.7, virtualenv and virtualenvwrapper on OS X

Command Reference — virtualenvwrapper 4.7.3.dev12 documentation

28.3. venv — Creation of virtual environments — Python 3.4.7rc1 documentation

How do I edit $PATH (.bash_profile) on OSX? – Stack Overflow

Hathaway

How to Add a New Path to PATH at Command Line the Right Way

Add Color to the Terminal in Mac OS X

bash – Setting PATH environmental variables in OSX permanently – Stack Overflow

Leave a Reply