我使用Ubuntu,并安装了Python 2.7.5和3.4.0。在Python 2.7.5中,我能够成功地分配一个变量x = Value(' I ', 2),但在3.4.0中不能。我得到:

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/local/lib/python3.4/multiprocessing/context.py", line 132, in Value
      from .sharedctypes import Value
   File "/usr/local/lib/python3.4/multiprocessing/sharedctypes.py", line 10, in <
module>
   import ctypes
   File "/usr/local/lib/python3.4/ctypes/__init__.py", line 7, in <module>
      from _ctypes import Union, Structure, Array
ImportError: No module named '_ctypes'

我刚刚通过安装3.4.0的源代码更新到了3.3.2。它安装在/usr/local/lib/python3.4.

我是否正确地更新到Python 3.4 ?

我注意到一件事,Python 3.4安装在usr/local/lib中,而Python 3.3.2仍然安装在usr/lib中,所以它没有被覆盖。


当前回答

如果您不介意使用Miniconda,默认情况下会安装必要的外部库和_ctypes。它会占用更多的空间,并且可能需要使用较旧的Python版本(例如,在撰写本文时,使用3.7.6而不是3.8.2)。

其他回答

如果你在Debian/Raspbian/Ubuntu上使用pyenv并得到错误“No module named '_ctypes'”(就像我一样),你需要运行以下命令:

sudo apt-get install libffi-dev
pyenv uninstall 3.7.6
pyenv install 3.7.6

使用你的python版本,而不是3.7.6

没有根的CentOS

安装libffi-3.2(不使用libffi-3.3) wget ftp://sourceware.org/pub/libffi/libffi-3.2.tar.gz libffi-3.2.tar.gz cd libffi - 3.2 / . / configure——prefix = $ YOUR_LIBFFI_DIR Make && Make安装 安装Python3 ./configure——prefix=$YOUR_PATH/python/3.7.10 LDFLAGS=-L${YOUR_LIBFFI_DIR}/lib64 PKG_CONFIG_PATH=${YOUR_LIBFFI_DIR}/lib/pkgconfig——enable-shared Make && Make安装

谢谢JohnWSteill

在我的Ubuntu 18.04机器上,我有一个常见的问题,python找不到_ctypes与pyenv安装的python。

在我的例子中,libffi-dev已经安装了。按照@MikeiLL的建议,从源代码安装cpython也没有帮助。

结果发现是自制的问题。

Ajkerrigans建议解决方案的pyenvs github问题为我解决了这个问题。

解决方案总结:告诉pyenv使用homebrew管理的GCC构建Python,使用如下命令:

CC="$(brew --prefix gcc)/bin/gcc-11" \
pyenv install --verbose 3.10.0

这假设任何构建依赖项都已通过pyenv wiki中指定的Homebrew安装。在写这篇文章时,Linux上的Homebrew看起来是这样的:

brew install bzip2 libffi libxml2 libxmlsec1 openssl readline sqlite xz zlib

当我试图在Ubuntu 18.04中使用next命令安装Python 3.7.3时,遇到了这个错误:$ pyenv install 3.7.3。 运行$ sudo apt-get update && sudo apt-get install libffi-dev(此处建议)后,安装成功。 问题在那里得到了解决。

在我的例子中,导致各种Python安装问题(包括与_ctypes和libffi有关的问题)的是Linux / Linuxbrew上的Homebrew。一旦brew不再在$PATH中,pyenv又高兴了。