MacOS: ModuleNotFoundError: No module named '_ctypes'
Asked Answered
M

1

6

I use my python and pip in the pyenv environment, but when I try to install numpy using pip:

pip install numpy

I got the following error, does anyone know where do I get wrong?

Collecting numpy Using cached numpy-1.21.0.zip (10.3 MB)
Installing build dependencies ... done Getting requirements to build wheel ... done ERROR: Exception: Traceback (most recent call last):
File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 188, in _main status = self.run(options, args) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 185, in wrapper return func(self, options, args) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 333, in run reqs, check_supported_wheels=not options.target_dir File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/resolution/legacy/resolver.py", line 179, in resolve discovered_reqs.extend(self._resolve_one(requirement_set, req)) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/resolution/legacy/resolver.py", line 362, in _resolve_one abstract_dist = self._get_abstract_dist_for(req_to_install) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/resolution/legacy/resolver.py", line 314, in _get_abstract_dist_for abstract_dist = self.preparer.prepare_linked_requirement(req) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 488, in prepare_linked_requirement req, self.req_tracker, self.finder, self.build_isolation, File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 91, in _get_prepared_distribution abstract_dist.prepare_distribution_metadata(finder, build_isolation) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/distributions/sdist.py", line 38, in prepare_distribution_metadata self._setup_isolation(finder) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_internal/distributions/sdist.py", line 96, in _setup_isolation reqs = backend.get_requires_for_build_wheel() File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_vendor/pep517/wrappers.py", line 161, in get_requires_for_build_wheel 'config_settings': config_settings File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_vendor/pep517/wrappers.py", line 265, in _call_hook raise BackendUnavailable(data.get('traceback', '')) pip._vendor.pep517.wrappers.BackendUnavailable: Traceback (most recent call last): File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py", line 86, in _build_backend obj = import_module(mod_path) File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 953, in _find_and_load_unlocked File "", line 219, in _call_with_frames_removed File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked File "", line 728, in exec_module File "", line 219, in _call_with_frames_removed File "/private/var/folders/nk/t9xyqhd94zzd6br4x5rg_jj80000gn/T/pip-build-env-9l_6shz6/overlay/lib/python3.7/site-packages/setuptools/init.py", line 23, in from setuptools.dist import Distribution File "/private/var/folders/nk/t9xyqhd94zzd6br4x5rg_jj80000gn/T/pip-build-env-9l_6shz6/overlay/lib/python3.7/site-packages/setuptools/dist.py", line 34, in from setuptools import windows_support File "/private/var/folders/nk/t9xyqhd94zzd6br4x5rg_jj80000gn/T/pip-build-env-9l_6shz6/overlay/lib/python3.7/site-packages/setuptools/windows_support.py", line 2, in import ctypes File "/Users/xxx/.pyenv/versions/3.7.11/lib/python3.7/ctypes/init.py", line 7, in from _ctypes import Union, Structure, Array ModuleNotFoundError: No module named '_ctypes'

Mathura answered 4/7, 2021 at 1:4 Comment(2)
What version of MacOS? You have a new M1 mac or an Intel Mac?Trenatrenail
MacBook Air(M1, 2020), mac OS Big Sur version 11.2.3, Chip Apple M1, Memory 16GBMathura
R
5

The following script works on my MacBook Pro (M1, 2020), MacOS: Big Sur 11.5.2. I hope this is helpful.

# Install x86 brew
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
alias ibrew=/usr/local/bin/brew

# Install Python 3.7
arch -x86_64 ibrew install [email protected]

# Add `python` executable (symlink to `python3`)
ln -s python3 "$(ibrew --prefix [email protected])"/bin/python

# Symlink x86 Python 3.7 into pyenv
ln -s "$(ibrew --prefix [email protected])" .pyenv/versions/3.7.10

# Check
pyenv local 3.7.10
python -V
# Python 3.7.10
python -c 'import _ctypes'. # works!

reference: https://github.com/pyenv/pyenv/issues/1768#issuecomment-871602950

Rita answered 15/9, 2021 at 5:58 Comment(3)
This installs Python under Rosetta, which may not be desirable for many users on M1 Macs.Amor
The suggested script failed for me with "Error: Cannot install in Homebrew on ARM processor in Intel default prefix (/usr/local)!", I had to use the commands in github.com/pyenv/pyenv/issues/1768#issuecomment-855523190 to get it to workCultivator
It is not installed via pyenv. It us making the symlink from globally installed to pynv folder.Ironworker

© 2022 - 2024 — McMap. All rights reserved.