Python3:ImportError:使用模块多处理中的值时,没有名为“_ctypes”的模块

问题描述 投票:73回答:6

我正在使用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中,所以它没有被覆盖。

python compiler-errors install python-3.4
6个回答
140
投票

安装libffi-dev并重新安装python3.7为我解决了这个问题。

要干净地建立py 3.7 libffi-dev是必需的,否则以后的东西会失败

如果使用RHEL / Fedora:

yum install libffi-devel

要么

sudo dnf install libffi-devel

如果使用Debian / Ubuntu:

sudo apt-get install libffi-dev

97
投票

在一个新的Debian图像上,克隆https://github.com/python/cpython并运行:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get install build-essential python-dev python-setuptools python-pip python-smbus
sudo apt-get install libncursesw5-dev libgdbm-dev libc6-dev
sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
sudo apt-get install libssl-dev openssl
sudo apt-get install libffi-dev

现在执行上面克隆的configure文件:

./configure
make # alternatively `make -j 4` will utilize 4 threads
sudo make altinstall

已安装3.7并为我工作。

SLIGHT UPDATE

看起来我说我会用更多的解释更新这个答案,两年后我没有太多补充。

  • this SO post解释了为什么像python-dev这样的某些图书馆可能是必要的。
  • this SO post解释了为什么人们可能会使用altinstall而不是install命令中的make参数。

除此之外,我想选择是通过cpython代码库来查找需要满足的#include指令,但我通常做的是继续尝试安装包并继续阅读输出安装所需的包直到它成功了。

让我想起Engineer, the Manager and the Programmer whose car rolls down a hill的故事。


16
投票

在CentOS或任何redhat linux机器上安装Python 3.7的详细步骤:

  1. https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz下载Python
  2. 提取新文件夹中的内容
  3. 在同一目录中打开终端
  4. 一步一步运行以下代码:
sudo yum -y install gcc gcc-c++ 
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel 
./configure
make
make install

11
投票

以为我会添加Centos安装:

sudo yum -y install gcc gcc-c++ 
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel 

检查python版本:

python3 -V

创建virtualenv:

virtualenv -p python3 venv


3
投票

当我尝试在Ubuntu 18.04中使用下一个命令安装Python 3.7.3时遇到此错误:$ pyenv install 3.7.3。运行$ sudo apt-get update && sudo apt-get install libffi-dev后安装成功(如建议的here)。问题解决了there


1
投票

请参阅this thread,对于libffi的自定义安装,Python3.7很难找到libffi的库位置。另一种方法是在Makefile中设置CONFIGURE_LDFLAGS变量,例如CONFIGURE_LDFLAGS="-L/path/to/libffi-3.2.1/lib64"

© www.soinside.com 2019 - 2024. All rights reserved.