Search code examples
python-3.xmacosjupytermacportsjupyter-contrib-nbextensions

Manging python packages on Mac OS


I am on Mac using macports to install python3. I decided to follow macports path instead of direct Python3 download/install to keep it separate from Apple's native Python installs. I went with Python36 since it was compatible with a recently added LabView python node.

Since then I have added number of other packages via macports: numpy, scipy, Pillow, matplotlib, pandas, jupyter, spyder, ... All dependencies have been always taken care of, all worked as expected. I have even install PIP (py36-pip) although I have never used it.

Trouble began when I tried installing "jupyter_contrib_nbextensions". The package did not show as an available "macport". Reluctantly, I tried PIP ... all seemed well, but received slew of error messages during the build phase.

I am wondering. What is the proper protocol installing additional python packages when the primary python is installed via macports?

  1. Perhaps, install python 3.x and corresponding PIP and manage all other packages with PIP rather than macports? Of course, all python packages would have to updated via PIP (currently everything is updated via macports, which is easy and convenient).

  2. ... or, since jupyter_contrib_nbextensions is the only non-working package I could find so far, hack it and install "manually" somehow?

  3. Something else?

I appreciate your insights.

Cheers, Radek


(Late night) Update:

After reading extensive write-up on multiple python versions here: https://realpython.com/intro-to-pyenv/ I decided to remove all python packages from macports (a bit scary really, but the text on the mentioned web site was reasonably clear). Note, that one still needs macports or homebrew to install and build pyenv

After installing pyenv it took a while to build python3.6. I can now switch between python versions set either globally (any directory) or locally (only in that directory).

PIP was a bit frustrating because it was not installed on the "system" python (in /usr/bin) but was installed on 3.6. I am just installing jupyter in my local 3.6 versions .... extensions are coming next.

All worked out.

pyenv all the way! R>


Solution

  • The question is which PIP are using using? Often the trouble starts when one version of PIP (eg. system version) is somehow associated with multiple versions of Python. The best way to avoid it when using MacPorts version(s) of Python is to always use it's version(s) of PIP.

    Regarding management of multiple versions of Python; it's easy to manage if you utilize port select when switching between Python versions. I have a ~/.bash_profile function that makes it super easy for me to switch back-and-forth between several versions of Python along with it's version of PIP.

    For example:

    In ~/.bash_profile

    # MacPorts Python and PIP version selection
    py() { [[ "$1" = "2" ]] && sudo port select --set python python27 \
    && sudo port select --set pip pip27 && kill -INT $$ && alias pip=pip2; \
    [[ "$1" = "3" ]] && sudo port select --set python python36 \
    && sudo port select --set pip pip36 && kill -INT $$ && alias pip=pip3; \
    echo "Invalid Python version selected." ; }
    

    Use it like this:

    $ py 2 # switches to Python 2.7 and PIP 2.7
    $ py 3 # switches to Python 3.6 and PIP 3.6
    

    Checking version:

    $ python -V
    Python 3.6.9
    
    $ pip --version
    pip 19.3.1 from /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip (python 3.6)
    

    When everything is on the same page it makes the management aspect much better.