Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Difficulties setting up Python in STATA16

    Dear Statalist,

    I am having difficulties in setting up Python in Stata16. I have read several threads from this forum (e.g. https://www.statalist.org/forums/for...le-not-working) but still cannot find a solution to my problem.

    I am running Stata16 on macOS 10.15.4. My current version of Python is 3.74 which works just fine with IDEs such as Jupyter Notebook and Spyder.

    Please find below the output of a few Stata command lines (in the example I am trying to import seaborn.) which I hope will provide you with enough information to help me.

    Many thanks in advance

    Best,
    PG

    Code:
    . python search
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     Python environments found:  
     /usr/bin/python
     /usr/local/bin/python3
     /usr/local/bin/python3.7m
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    . python query
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        Python Settings
          set python_exec      /opt/anaconda3/bin/python
          set python_userpath  /opt/anaconda3/lib/python3.7/site-packages
    
        Python system information
          initialized          yes
          version              3.7.4
          architecture         64-bit
          library path         /opt/anaconda3/lib/libpython3.7m.dylib
    
    . python
    ----------------------------------------------- python (type end to exit) -----------------------------------------------------------------------------------------------------------
    >>> import sys
    >>> print(sys.path)
    ['', '/Applications/Stata/ado/base/py', '', '/opt/anaconda3/lib/python37.zip', '/opt/anaconda3/lib/python3.7', '/opt/anaconda3/lib/python3.7/lib-dynload', '/opt/anaconda3/lib/python
    > 3.7/site-packages', '/opt/anaconda3/lib/python3.7/site-packages/aeosa', '.', '/Applications/Stata/', '/Applications/Stata/ado/base/', '/Applications/Stata/ado/base/py', '/Applicat
    > ions/Stata/ado/site/', '/Applications/Stata/ado/site/py', '/Users/peppegrass/Library/Application Support/Stata/ado/plus/', '/Users/peppegrass/Library/Application Support/Stata/ado
    > /plus/py', '/Users/peppegrass/Library/Application Support/Stata/ado/personal/', '/Users/peppegrass/Library/Application Support/Stata/ado/personal/py', '~/ado/', '/opt/anaconda3/li
    > b/python3.7/site-packages']
    >>> import seaborn as sns
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/__init__.py", line 6, in <module>
        from .rcmod import *
      File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/rcmod.py", line 5, in <module>
        from . import palettes, _orig_rc_params
      File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/palettes.py", line 12, in <module>
        from .utils import desaturate, set_hls_values, get_color_cycle
      File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/utils.py", line 7, in <module>
        from scipy import stats
      File "/opt/anaconda3/lib/python3.7/site-packages/scipy/stats/__init__.py", line 379, in <module>
        from .stats import *
      File "/opt/anaconda3/lib/python3.7/site-packages/scipy/stats/stats.py", line 180, in <module>
        import scipy.special as special
      File "/opt/anaconda3/lib/python3.7/site-packages/scipy/special/__init__.py", line 641, in <module>
        from ._ufuncs import *
    ImportError: dlopen(/opt/anaconda3/lib/python3.7/site-packages/scipy/special/_ufuncs.cpython-37m-darwin.so, 2): Symbol not found: _main
      Referenced from: /opt/anaconda3/lib/python3.7/site-packages/scipy/special/_ufuncs.cpython-37m-darwin.so
      Expected in: flat namespace
     in /opt/anaconda3/lib/python3.7/site-packages/scipy/special/_ufuncs.cpython-37m-darwin.so
    r(7102);
    >>> end
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    .

  • #2
    Hi Peppe,

    It looks like you have a few different versions of Python on your machine.

    Are you sure that the version of -seaborn- being called is "in" the same environment as the version of Python invoked in Stata?

    It appears as though it's having trouble with a C library (the _ufuncs.cpython-37m-darwin.so file it's referring to) and in looking over similar issues on StackExchange, a commonality seems to be that there are multiple versions of Python and the version of Python being used cannot load the C library as called.

    I'm not overly familiar with macOS file paths but it seems like -python query- yields different results from where -python search- finds Python executables. I would guess that the problem is somewhere in which version of Python is used vs the specs of the -seaborn- package.

    - joe
    Joseph Nicholas Luchman, Ph.D., PStatĀ® (American Statistical Association)
    ----
    Research Fellow
    Fors Marsh

    ----
    Version 18.0 MP

    Comment


    • #3
      Hi Joseph,

      Thanks for your answer

      I guess you are right...
      Originally posted by Joseph Luchman View Post
      I would guess that the problem is somewhere in which version of Python is used vs the specs of the -seaborn- package.
      ...but I do not know how to check this
      Originally posted by Joseph Luchman View Post
      Are you sure that the version of -seaborn- being called is "in" the same environment as the version of Python invoked in Stata?
      Consequently, I cannot figure out whether my current 'python_exec' and 'python_userpath' configuration is right.

      Last edited by Peppe Grasso; 15 Apr 2020, 12:38.

      Comment


      • #4
        I could ascertain that the executable python file that I installed as part of the anaconda distribution is located here "/opt/anaconda3/bin/python"; therefore I guess the first configuration (below) should be correct
        Code:
        python set exec "/opt/anaconda3/bin/python", permanently
        Moreover, from my mac terminal, after invoking python3, I call
        Code:
        import sys
        print(sys.path)
        and get
        Code:
        ['', '/opt/anaconda3/lib/python37.zip', '/opt/anaconda3/lib/python3.7', '/opt/anaconda3/lib/python3.7/lib-dynload', '/opt/anaconda3/lib/python3.7/site-packages', '/opt/anaconda3/lib/python3.7/site-packages/aeosa']
        All of which (excluding '/opt/anaconda3/lib/python37.zip', which gives me an error) I successfully manage to have Stata parse in
        Code:
        python set userpath "/opt/anaconda3/lib/python3.7" "/opt/anaconda3/lib/python3.7/lib-dynload" "/opt/anaconda3/lib/python3.7/site-packages" "/opt/anaconda3/lib/python3.7/site-packages/aeosa" "/opt/anaconda3/lib/python3.7/site-packages/IPython/extensions", permanently
        Notwithstanding, I still cannot import seaborn. However, I can import numpy and pandas.
        Code:
        . python
        ----------------------------------------------- python (type end to exit) -----------------------------------------------------------
        >>> import numpy as np
        >>> import pandas as pd
        >>> import seaborn as sns
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/__init__.py", line 6, in <module>
            from .rcmod import *
          File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/rcmod.py", line 5, in <module>
            from . import palettes, _orig_rc_params
          File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/palettes.py", line 12, in <module>
            from .utils import desaturate, set_hls_values, get_color_cycle
          File "/opt/anaconda3/lib/python3.7/site-packages/seaborn/utils.py", line 7, in <module>
            from scipy import stats
          File "/opt/anaconda3/lib/python3.7/site-packages/scipy/stats/__init__.py", line 379, in <module>
            from .stats import *
          File "/opt/anaconda3/lib/python3.7/site-packages/scipy/stats/stats.py", line 180, in <module>
            import scipy.special as special
          File "/opt/anaconda3/lib/python3.7/site-packages/scipy/special/__init__.py", line 641, in <module>
            from ._ufuncs import *
        ImportError: dlopen(/opt/anaconda3/lib/python3.7/site-packages/scipy/special/_ufuncs.cpython-37m-darwin.so, 2): Symbol not found: _ma
        > in
          Referenced from: /opt/anaconda3/lib/python3.7/site-packages/scipy/special/_ufuncs.cpython-37m-darwin.so
          Expected in: flat namespace
         in /opt/anaconda3/lib/python3.7/site-packages/scipy/special/_ufuncs.cpython-37m-darwin.so
        r(7102);
        >>> end
        -------------------------------------------------------------------------------------------------------------------------------------
        
        .
        If I look for the three of them (numpy, pandas, seaborn) in Stata using -ls- in the directory which I had originally indicated
        Code:
         
         /opt/anaconda3/lib/python3.7/site-packages
        I find them all. Then why can I not import seaborn? To be more precise this is not unique to seaborn. sklearn for instance is also found in the directory above, but fails to be imported.
        Any ideas would be greatly appreciated. Thanks!

        Comment

        Working...
        X