Search code examples
pythonmacosmacportspython-3.5

Error installing python packages that require .so files after upgrading to macports python 3.5


I've seen similar questions, but none that have given answers to my problem. I recently upgraded to python v3.5 using Macports on Mac OS X. Installing python packages works fine if there is a valid Macport:

sudo port install py35-numpy

However, if I try to install python packages that require .so files using pip3, I get errors. For example, there is no port of pystan. First, I install pip

sudo port install py35-pip

Then I use pip to install pystan to my Python 3.5 directory

sudo pip3 install --target=/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/ pystan

Installing collected packages: Cython, numpy, pystan
Successfully installed Cython-0.24 numpy-1.11.1 pystan-2.9.0.0

Not that it didn't actually install Cython or numpy because they were already there. But then when I try to run pystan:

    Nate$ ipython
Python 3.5.2 (default, Jun 27 2016, 03:10:38) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pystan
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-7b354c723dbb> in <module>()
----> 1 import pystan

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/__init__.py in <module>()
      7 import logging
      8 
----> 9 from pystan.api import stanc, stan
     10 from pystan.misc import read_rdump, stan_rdump
     11 from pystan.model import StanModel

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/api.py in <module>()
     10 import hashlib
     11 
---> 12 import pystan._api  # stanc wrapper
     13 from pystan._compat import string_types
     14 from pystan.model import StanModel

ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/_api.so, 2): Symbol not found: _PyBaseString_Type
  Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/_api.so
  Expected in: flat namespace
 in /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pystan/_api.so

I saw the same error when I tried to install numpy via pip, and the same one with netCDF4.

In [2]: import netCDF4
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-f731da2de255> in <module>()
----> 1 import netCDF4

/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/__init__.py in <module>()
      1 # init for netCDF4. package
      2 # Docstring comes from extension module _netCDF4.
----> 3 from ._netCDF4 import *
      4 # Need explicit imports for names beginning with underscores
      5 from ._netCDF4 import __doc__, __pdoc__

ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/_netCDF4.so, 2): Symbol not found: _PyCObject_Type
  Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/_netCDF4.so
  Expected in: flat namespace
 in /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/netCDF4/_netCDF4.so

What's wrong with the .so files? Or, for that matter, my computer?


Solution

  • Are you sure pip3 belongs to a version of pip installed by MacPorts, preferably pip-3.5? My guess is that you are using a pip from a different Python installation, which will therefore link C extensions to the wrong Python library, and force installation into the package directory of the Python3.5 from MacPorts.

    To fix, use pip-3.5 instead (and then you can also leave out the --target option).

    EDIT: Or execute sudo port select --set pip pip35 and then use just pip.

    EDIT: I just checked, and indeed (current) MacPorts pip versions do not provide a pip3 command at all, so this clearly belongs to a different Python version. Do which -a pip3 to find out which one.