Getting Python under control on Mac OS X - setting up environment and libraries

Viewed 8976

After starting out with Python on Ubuntu Linux, I've now for a good while been doing most of my sustained work on the Mac, currently Mac OS X 10.6. Unfortunately I've neglected to give proper attention to how Python is installed there and ended up with:

  • Python 2.6.1 (Mac default version?) in /usr/bin (also, 2.5.4, which I'm not sure how it got there)
  • Python 2.6.5 installed via MacPorts in /opt/local/bin/. This is my default
  • I use pip to install libraries, which end up in some ungodly place (something like /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/ - SRSLY?)
  • Otherwise, mostly TextMate, and Git for version control. Django and Google App Engine, etc...

Now I'm preparing to set up 2.7 and 3.2, and I am unhappy with the haphazard state of things. So what are your favourite approaches to organising the code and libraries, and how have you wrestled the Mac into submission?

I'd like to continue using pip, but would like to have more control or at least understanding of what libraries for which version are getting installed and made accessible from where: I've had problems with installing py.test via pip and only being able to load it from the obsolete 2.6.1 Python, not my current 2.6.5 one. MacPorts has python_select, but it's not overly helpful:

reason: chris$ python_select -l
Available versions:
current none python26 python26-apple

Most Python people I've asked don't use MacPorts, which I don't much like, but the stock Mac Python from python.org. I've also heard the recommendation to use virtualenv systematically, so what is the link to a good practical introduction?

4 Answers

This is an old question, but I have struggled in the past with upgrading Python and virtual environments. I have figured out how to get this to work for the stuff I do. Hopefully this will help someone.

Setup:
Mac running 10.14
Python 3.7
Various packages: Pandas, IPython, Paramiko, etc.

Python:
Just use the installer they provide at Python.org. Click on the "Download" button for macOS, download the packages and click through the installer. Done. When it comes time to upgrade to the next Python 3 version, repeat the process.
To get the Python3 interpreter on the command-line instead of the Python2 one, use python3 instead of python.

Getting Rid of Older Python3 versions
If you don't like the idea of having old Python versions hanging around, open the Applications folder and drag-and-drop the old Python3 version you don't want to the trash. DO NOT do this to the Python2 versions unless you know what you a re doing. Some of these came with the OS and will likely cause trouble if they disappear.

Installing Packages:.
@Ned Deily's suggestion to use virtual environments is a good one if you want to make installing packages easier. The virtual environment tool for Python2 is 'virtualenv'. That tool is called 'venv' in Python3.

Step 1:
Create a directory to hold all of your virtual environments. This isn't necessary, but this is how I keep track of mine. I keep two separate dirs named 'virtenvs' and 'venvs' depending on whether I'm installing a package for Python2 or Python3. For this example, we'll use Python3:

cd ~
mkdir venvs
cd venvs

Now, I name my packages after my project or after the major package I've installed. For this example, let's just call it 'project1':
venv project1

Step 2:
"Activate" the virtual environment using the following command. You'll need to to this every time you start a new terminal or session.
source ~/venvs/project1/bin/activate

Pip changes a lot, so update it
pip install --upgrade pip

Step 3:
Now you're ready to start installing packages into your new virtual environment:
pip install pandas
etc.

When you're done or you want to switch to a different virtual environment, just type:
deactivate

What's Next:
Explore the web for more info on Python virtual environments. Using this approach, I've not run into a package that required MacPorts, Anaconda, etc. There are undoubtedly packages that do. YMMV. Hopefully this approach will work for you.

Related