Announcement

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

  • pystata does not work in Mac M1

    Hi all,

    Has anyone with Mac M1 successfully used Stata in Python via the pystata library? I am having trouble using the library as it gives a "page failed" error message (see below). For context, the OS is Mac Monterey 12.0.1. Any help is appreciated. Many thanks!

    Python 3.8.9 (default, Jul 19 2021, 09:37:30)
    [Clang 13.0.0 (clang-1300.0.27.3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import stata_setup
    >>> stata_setup.config('/Applications/Stata', 'mp')
    [0.003s][warning][os,thread] Attempt to protect stack guard pages failed (0x000000016b080000-0x000000016b08c000).
    zsh: killed python3

  • #2
    Hi Sean Ma, is this message a warning or an error? Does the statement
    Code:
    stata_setup.config('/Applications/Stata', 'mp')
    still initialize Stata successfully or immediately error out and quit you Python environment?

    Comment


    • #3
      Hi @Zhao Xu (StataCorp) No,
      stata_setup.config('/Applications/Stata', 'mp') does not initialize Stata. My Python environment quits immediately after I run this line of command.

      Comment


      • #4
        Hi Sean Ma, we did some experiments on different macOS and found that on my Apple M1 machine with macOS 11.6,
        Code:
        stata_setup.config('/Applications/Stata', 'mp')
        outputs two warnings claiming "Attempt to protect stack guard pages failed", but it still successfully initialized Stata and there was no problem executing Stata commands. On my Apple M1 machine with macOS 12.2, it immediately quits Python like yours. I also tested my Intel Mac with macOS 12.2, it successfully initialized Stata without any warning. We are investigating this issue now.

        Comment


        • #5
          Thank you Zhao Xu (StataCorp)!
          Is there any update on this?

          Comment


          • #6
            Originally posted by Zhao Xu (StataCorp) View Post
            On my Apple M1 machine with macOS 12.2, it immediately quits Python like yours. [...] We are investigating this issue now.
            I'm still running Big Sur (OS X 11.6) on my M1, but the thread above suggests that PyStata is currently incompatible with Monterey (OS X 12.3) on M1. Is that correct, and if so, are there any workarounds? If not, can you provide any information on when this might be addressed? Thanks very much.

            Comment


            • #7
              Sean Ma and Phil Schumm:

              We have identified that there is some incompatibility on MacOSX 12.2, and possibly 12.3, with loading Java 11 from Python 3 on M1 Macs. This includes Python 3 loading Stata which then loads Java. In the current revision of Stata 17 (15 Feb 2022) and before there is a Java dependency for loading pystata itself. We are working to remove that dependency and plan to release that improvement in the next executable update. However, we are still investigating the larger issue with Python3/pystata and Java which can be triggered by other functionality in Stata.

              Comment


              • #8
                Thanks very much Zhao Xu.

                Comment


                • #9
                  Originally posted by Zhao Xu (StataCorp) View Post
                  Sean Ma and Phil Schumm:

                  We have identified that there is some incompatibility on MacOSX 12.2, and possibly 12.3, with loading Java 11 from Python 3 on M1 Macs. This includes Python 3 loading Stata which then loads Java. In the current revision of Stata 17 (15 Feb 2022) and before there is a Java dependency for loading pystata itself. We are working to remove that dependency and plan to release that improvement in the next executable update. However, we are still investigating the larger issue with Python3/pystata and Java which can be triggered by other functionality in Stata.
                  I also have a recent MacBook Pro with the M1 chip. When I run the code in Chuck Huber's blog (The Stata Blog » Stata/Python integration part 7: Machine ...) about using a Python SVM module, I get the following error message:

                  dlopen(/Users/anthonykilleen/opt/anaconda3/lib/lib
                  > python3.9.dylib, 0x0009): tried: '/Users/anthony
                  > killeen/opt/anaconda3/lib/libpython3.9.dylib' (m
                  > ach-o file, but is an incompatible architecture
                  > (have 'x86_64', need 'arm64e'))
                  failed to load the shared library /Users/anthonyki
                  > lleen/opt/anaconda3/lib/libpython3.9.dylib.
                  r(7100);

                  I have the python_exec set to "/Users/anthonykilleen/opt/anaconda3/bin/python", currently 3.9.7. Using the 6-apr-2022 update of Stata 17 MP.

                  On a different Mac with an Intel I5 processor and the same Stata level, Mr. Huber's code runs perfectly. Both machines are running macOS Monterey 12.3.1
                  Last edited by Anthony Killeen; 16 Apr 2022, 19:28.

                  Comment


                  • #10
                    Originally posted by Anthony Killeen View Post

                    I have the python_exec set to "/Users/anthonykilleen/opt/anaconda3/bin/python", currently 3.9.7.
                    Update: I switched the choice of Python installation to "/usr/local/bin/python3", (version 3.9.8), installed the necessary packages in that library (see Install python modules and libraries using IDLE on MAC 2019https://www.honchosearch.com › blog › seo › install-py...) and now Mr. Huber's code works well on my M1 MacBook. Maybe there was a problem with the Anaconda installation.

                    Comment


                    • #11
                      Hi Anthony Killeen,

                      If you're running an Apple Silicon native version of Stata on an M1 Mac, then all libraries or plugins you attempt to use with Stata must be Apple Silicon native as well. This is not just a Stata requirement, this is true for all macOS applications. So if you're trying to use a third party version of Python, that version of Python must be built for Apple Silicon as well as any libraries it'll use. If you attempt to use non Apple Silicon native versions of libraries, you'll get error messages that the libraries it's attempting to load are the wrong architecture or failed to load.

                      For your specific problem, you can try to force Stata to run under Rosetta 2 so that it will run the Intel version of Stata. From the Finder, select the Stata application then select the File > Get Info menu item. Check the Open Using Rosetta checkbox. Then Open Stata to set your Python executable to see whether the problem goes away.

                      Comment


                      • #12
                        Hi @ZhaoXu. Thank you! That was very helpful advice. I set Stata 17 to open with Roseta and set python_exec to use the Anaconda installation again, the error message from Anaconda no longer appeared, and the code ran.

                        Comment


                        • #13
                          Hi @ZhaoXu, I saw that Stata released a new update on April 06., 2022. For some reason pystata module still does not work. Calling stata_setup.config('/Applications/Stata', 'mp') inside Python no longer invokes the Python kernel being killed, but when I call the stata magic command, it shows " UsageError: Cell magic `%%stata` not found." As I look into the Jupyter notebook logs, below is what I found.

                          [IPKernelApp] ERROR | Error in message handler

                          Traceback (most recent call last):

                          File "/Users/Library/Python/3.8/lib/python/site-packages/ipykernel/kernelbase.py", line 461, in dispatch_queue

                          await self.process_one()

                          File "/Users/Library/Python/3.8/lib/python/site-packages/ipykernel/kernelbase.py", line 450, in process_one

                          await dispatch(*args)

                          TypeError: object NoneType can't be used in 'await' expression

                          [IPKernelApp] ERROR | Error in message handler

                          Traceback (most recent call last):

                          File "/Users/Library/Python/3.8/lib/python/site-packages/ipykernel/kernelbase.py", line 461, in dispatch_queue

                          await self.process_one()

                          File "/Users/Library/Python/3.8/lib/python/site-packages/ipykernel/kernelbase.py", line 450, in process_one

                          await dispatch(*args)

                          TypeError: object NoneType can't be used in 'await' expression

                          Comment


                          • #14
                            Hi Sean Ma,

                            I could not reproduce it on my macOS. Can you tell which masOS version you are using? Also, can you type -update all, force- in Stata to make sure you have a full update?

                            Comment


                            • #15
                              Hi Zhao Xu, Thank you! I am using Mac OS Monterey 12.3.1. I tried updating Stata again, but the error still persists.

                              Comment

                              Working...
                              X