Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3.9 malloc: can't allocate region error 3

I am using Python3.9 and I started writing a simple script to shape some data, I tested it after writing just a single line and I am getting this error:

 $ Python3.9 Shape_Response.py 
Python(2857,0x1061635c0) malloc: can't allocate region
*** mach_vm_map(size=18446744072478715904) failed (error code=3)
Python(2857,0x1061635c0) malloc: *** set a breakpoint in malloc_error_break to debug
init_dgelsd failed init
Traceback (most recent call last):
  File "/Users/dominik/Desktop/Google Analytics API/Shape_Response.py", line 1, in <module>
    import pandas as pd, numpy as np
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/__init__.py", line 11, in <module>
    __import__(dependency)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/__init__.py", line 286, in <module>
    raise RuntimeError(msg)
RuntimeError: Polyfit sanity test emitted a warning, most likely due to using a buggy Accelerate backend. If you compiled yourself, see site.cfg.example for information. Otherwise report this to the vendor that provided NumPy.
RankWarning: Polyfit may be poorly conditioned

This is the Python script:

import pandas as pd, numpy as np

df = pd.read_csv('test.txt', delimiter = "\n", header=None)

print(df)

The file I am reading is:

Hello
Hello
Hello

I am on MacOs.

Details of my Python installation are:

dominik at Dominiks-MacBook-Pro in Google Analytics API
$ which python
/usr/bin/python

dominik at Dominiks-MacBook-Pro in Google Analytics API
$ python --version
>>> Python 3.9.0

dominik at Dominiks-MacBook-Pro in Google Analytics API
$ pip --version
>>> pip 20.2.4 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)

dominik at Dominiks-MacBook-Pro in Google Analytics API
$ pip list
>>> Package         Version
>>> --------------- -------
>>> numpy           1.19.2
>>> pandas          1.1.3
>>> pip             20.2.4
>>> python-dateutil 2.8.1
>>> pytz            2020.1
>>> setuptools      49.2.1
>>> six             1.15.0
>>> wheel           0.35.1

I installed pandas using pip install wheel and pip install pandas.

I also have free memory, I reinstalled my Python and I am still running into this error.

I am a Python beginner and I really don't understand the error message, I tried searching but nothing worked...

Any ideas of what am I doing wrong?

Thank you very much

like image 286
Raymond_90 Avatar asked Oct 26 '20 00:10

Raymond_90


3 Answers

brew is not yet stable for MacOS BIG SUR as on date. I upgraded to Python 3.9 and faced same issue with numpy mem allocations, so i rolled back like this:

brew link --overwrite [email protected]
  Linking /usr/local/Cellar/[email protected]/3.8.6_2... 25 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

this way I am back on Python 3.8.6 which works fine

like image 148
Deepkamal Avatar answered Nov 15 '22 17:11

Deepkamal


Same issue was for me when I install python3.8/3.9 using brew. I got around this problem by deleting brew version of python and install python from python.org/mac-osx

like image 21
NAZ.42 Avatar answered Nov 15 '22 17:11

NAZ.42


I was also having issues with getting numpy to work on my machine.

Context

My setup is MacOS Big Sur running the zsh command line using the oh-my-zsh extension. I was having issues with importing numpy ever since I upgraded from Catalina to Big Sur.

I had a few versions of Python3 installed on my machine:

  • Python3.9 (brew)
  • Python3.8 (brew)
  • Python3.8 (from python.org I think)

with the python3 alias pointing to one of the brew versions of Python, at least according to the output of performing the command which python3. I believe that these brew versions were installed automatically as dependencies for another program.

Since I had multiple versions of Python installed, I believe that I had multiple paths pointing to the same python/python3 alias. To solve this issue, I first removed any old Python paths which could have been automatically created whenever I installed a new version of Python (either through brew or official release).

My Steps

I took the following steps to fix my supposed issue. If your issue sounds similar to mine, this may be helpful!

Part A. Clearing out .bashrc and .zshrc

  1. Type into the command line: vi ~/.bashrc and remove anything that contains something along the lines of export PATH=/usr/...<some more directories>...python:%PATH.
  2. Type into the command line: vi ~/.zshrc and follow the same process as before.

Part B. Removing brew-installed versions of Python

Next, I removed any versions of python which were installed by brew. Since I had both Python3.9 and Python3.8, I used the following commands to uninstall them via brew:

  1. brew uninstall [email protected]
  2. brew uninstall [email protected]

Part C. Installing a fresh version of Python

Once the brew versions of Python were uninstalled, running the command which python3 now pointed to the Python3.8 version I had installed from python.org. Since I wanted to run a different version of Python, I went over to the python downloads page for Mac OS X and downloaded the Mac OS X 64-bit installer.

After doing so and restarting my terminal, running the which python3 command now pointed to the latest version of Python which I installed.

/Library/Frameworks/Python.framework/Versions/3.9/bin/python3

You should also run which pip3 to confirm that pip3 is aliased to the proper version of pip to use for Python3.9, which should be the case if installing Python via the installer.

/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3

Part D. Installing numpy

Once python3 and pip3 are set up, you can run pip3 install numpy, which should install a fresh version of numpy for Python3.9. Then, running an import statement for numpy (such as import numpy as np) inside of a .py file should work with no issues.

Conclusion

I am sure that this is not the best way of handling different version of Python, but it worked for me and I hope it works for you as well. Overall though, this may be a good workaround for now until someone figures out a better way or if numpy fixes this in a future update.

I have heard that using pyenv and pipenv are good ways of dealing with multiple environments and versions of Python. This may be helpful for you. Good luck!

  • Edit: Added a section on pip3 aliasing and a section on the result after installing numpy.
  • Edit: Added information on where the multiple versions of Python I had on my machine may have originated from.
like image 28
lecheboludo Avatar answered Nov 15 '22 16:11

lecheboludo