问题:Python3:ImportError:使用模块多处理中的值时,没有名为“ _ctypes”的模块
我正在使用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中,因此它没有被覆盖。
I am using Ubuntu and have installed Python 2.7.5 and 3.4.0. In Python 2.7.5 I am able to successfully assign a variable x = Value('i', 2)
, but not in 3.4.0. I am getting:
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'
I just updated to 3.3.2 through installing the source of 3.4.0. It installed in /usr/local/lib/python3.4.
Did I update to Python 3.4 correctly?
One thing I noticed that Python 3.4 is installed in usr/local/lib, while Python 3.3.2 is still installed in usr/lib, so it was not overwritten.
回答 0
安装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
Installing libffi-dev
and re-installing python3.7 fixed the problem for me.
to cleanly build py 3.7 libffi-dev
is required or else later stuff will fail
If using RHEL/Fedora:
yum install libffi-devel
or
sudo dnf install libffi-devel
If using Debian/Ubuntu:
sudo apt-get install libffi-dev
回答 1
在新的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版并为我工作。
轻微更新
看来我说过我会用一些更多的解释来更新此答案,两年后,我没有太多补充。
除此之外,我猜测选择是通读cpython代码库以查找#include
需要满足的指令,但是我通常要做的是继续尝试安装软件包,并继续通读安装所需软件包的输出,直到找到为止。成功。
让我想起了工程师,经理和程序员的故事,他们的汽车从山上滚下来。
On a fresh Debian image, cloning https://github.com/python/cpython and running:
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
Now execute the configure
file cloned above:
./configure
make # alternatively `make -j 4` will utilize 4 threads
sudo make altinstall
Got 3.7 installed and working for me.
SLIGHT UPDATE
Looks like I said I would update this answer with some more explanation and two years later I don’t have much to add.
- this SO post explains why certain libraries like
python-dev
might be necessary.
- this SO post explains why one might use the
altinstall
as opposed to install
argument in the make command.
Aside from that I guess the choice would be to either read through the cpython codebase looking for #include
directives that need to be met, but what I usually do is keep trying to install the package and just keep reading through the output installing the required packages until it succeeds.
Reminds me of the story of the Engineer, the Manager and the Programmer whose car rolls down a hill.
回答 2
如果您使用pyenv并在Debian / Raspbian / Ubuntu上收到错误“没有名为’_ctypes’的模块”(例如我是),则需要运行以下命令:
sudo apt-get install libffi-dev
pyenv uninstall 3.7.6
pyenv install 3.7.6
将您的python版本而不是3.7.6
If you use pyenv and get error “No module named ‘_ctypes'” (like i am) on Debian/Raspbian/Ubuntu you need to run this commands:
sudo apt-get install libffi-dev
pyenv uninstall 3.7.6
pyenv install 3.7.6
Put your version of python instead of 3.7.6
回答 3
Detailed steps to install Python 3.7 in CentOS or any redhat linux machine:
- Download Python from https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
- Extract the content in new folder
- Open Terminal in the same directory
- Run below code step by step :
sudo yum -y install gcc gcc-c++
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel
./configure
make
make install
回答 4
以为我会添加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
Thought I’d add the Centos installs:
sudo yum -y install gcc gcc-c++
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel
Check python version:
python3 -V
Create virtualenv:
virtualenv -p python3 venv
回答 5
尝试使用下一个命令在Ubuntu 18.04中安装Python 3.7.3时遇到此错误。运行后安装成功(如建议在这里)。这个问题在那里解决了。$ pyenv install 3.7.3
$ sudo apt-get update && sudo apt-get install libffi-dev
I run into this error when I tried to install Python 3.7.3 in Ubuntu 18.04 with next command: $ pyenv install 3.7.3
.
Installation succeeded after running $ sudo apt-get update && sudo apt-get install libffi-dev
(as suggested here).
The issue was solved there.
回答 6
回答 7
请参阅此线程,以进行libffi的自定义安装,Python3.7很难找到libffi的库位置。另一种方法是CONFIGURE_LDFLAGS
在Makefile中设置变量,例如CONFIGURE_LDFLAGS="-L/path/to/libffi-3.2.1/lib64"
。
Refer to this thread or this thread, for customized installation of libffi, it is difficult for Python3.7 to find the library location of libffi. An alternative method is to set the CONFIGURE_LDFLAGS
variable in the Makefile, for example CONFIGURE_LDFLAGS="-L/path/to/libffi-3.2.1/lib64"
.
回答 8
我的解决方案:使用apt-get安装libffi-dev并没有帮助。但这有帮助:从源代码安装libffi,然后从源代码安装Python 3.8。
我的配置:Ubuntu 16.04 LTS Python 3.8.2
一步步:
从Visual Studio Code启动调试器并运行时,出现错误消息“ ModuleNotFoundError:没有名为’_ctypes’的模块” python3 -c "import sklearn; sklearn.show_versions()"
。
./configure --disable-docs
make check
sudo make install
之后,我的python安装程序可以找到_ctypes。
My solution:
Installing libffi-dev with apt-get didn’t help.
But this helped: Installing libffi from source and then installing Python 3.8 from source.
My configuration:
Ubuntu 16.04 LTS
Python 3.8.2
Step by step:
I got the error message “ModuleNotFoundError: No module named ‘_ctypes'” when starting the debugger from Visual Studio Code, and when running python3 -c "import sklearn; sklearn.show_versions()"
.
- download libffi v3.3 from https://github.com/libffi/libffi/releases
- install libtool:
sudo apt-get install libtool
The file README.md from libffi mentions that autoconf and automake are also necessary. They were already installed on my system.
- configure libffi without docs:
./configure --disable-docs
make check
sudo make install
After that my python installation could find _ctypes.
回答 9
这为我在Debian上解决了相同的错误:
sudo apt-get install libffi-dev
然后再次编译
参考:issue31652
This solved the same error for me on Debian:
sudo apt-get install libffi-dev
and compile again
Reference: issue31652
回答 10
如果您不介意使用Miniconda,则默认情况下会安装必需的外部库和_ctypes。它确实占用更多空间,并且可能需要使用中等版本的Python(例如,本文撰写时为3.7.6而不是3.8.2)。
If you don’t mind using Miniconda, the necessary external libraries and _ctypes are installed by default. It does take more space and may require using a moderately older version of Python (e.g. 3.7.6 instead of 3.8.2 as of this writing).
回答 11
您必须从包管理器中加载缺少的php3(Python3)模块。如果您有Ubuntu,我建议Synaptic Package Manager
:
sudo apt-get install synaptic
您可以在那里简单地搜索缺少的模块。搜索ctypes并安装所有软件包。然后转到您的Python目录并执行
./configure
make install.
这应该可以解决您的问题。
You have to load the missing php3 (Python3) modules from the package manager.
If you have Ubuntu I recommend the Synaptic Package Manager
:
sudo apt-get install synaptic
There you can simply search for the missing modules. search for ctypes and install all the packages. Then go to your Python dir and do
./configure
make install.
This should solve your problem.
回答 12
如果您正在做某事,这里没有人会听您说,因为“您做错了方法”,但是由于太精明的原因,您必须“做错了方法”(例如,在我的情况下,它很快会降级为犯规)关于Devops团队超重母亲的某人的话),您需要首先:
获取libffi并将其以常规方式安装到用户安装区域中。
git clone https://github.com/libffi/libffi.git
cd libffi
./configure --prefix=path/to/your/install/root
make
make install
然后返回您的Python 3源代码,并在python源代码目录的顶层setup.py中找到这部分代码。
ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
if not ffi_inc or ffi_inc[0] == '':
ffi_inc = find_file('ffi.h', [], inc_dirs)
if ffi_inc is not None:
ffi_h = ffi_inc[0] + '/ffi.h'
if not os.path.exists(ffi_h):
ffi_inc = None
print('Header file {} does not exist'.format(ffi_h))
ffi_lib = None
if ffi_inc is not None:
for lib_name in ('ffi', 'ffi_pic'):
if (self.compiler.find_library_file(lib_dirs, lib_name)):
ffi_lib = lib_name
break
ffi_lib="ffi" # --- AND INSERT THIS LINE HERE THAT DOES NOT APPEAR ---
if ffi_inc and ffi_lib:
ext.include_dirs.extend(ffi_inc)
ext.libraries.append(ffi_lib)
self.use_system_libffi = True
并添加我在上面用注释标记的行。为什么有必要,为什么没有办法在Linux平台上配置尊重’–without-system-ffi`的功能,也许我会找出为什么在接下来的几个小时内“不支持”的原因,但是一切都有从那以后一直工作。否则,祝您好运… YMMV。
它的作用:只是重写那里的逻辑,并使编译器链接命令添加“ -lffi”,这是它真正需要的全部。如果您是用户安装的库,则只要您PKG_CONFIG_PATH
包含include ,它就可以很好地检测标题path/to/your/install/root/lib/pkgconfig
。
If you are doing something nobody here will listen you about because “you’re doing it the wrong way”, but you have to do it “the wrong way” for reasons too asinine (for instance, in my case it quickly degrades into foul words about somebody on devops team overweight mother), you need to first:
Get libffi and install it into your user install area the usual way.
git clone https://github.com/libffi/libffi.git
cd libffi
./configure --prefix=path/to/your/install/root
make
make install
Then go back to your Python 3 source and find this part of the code in setup.py at the top level of the python source directory
ffi_inc = [sysconfig.get_config_var("LIBFFI_INCLUDEDIR")]
if not ffi_inc or ffi_inc[0] == '':
ffi_inc = find_file('ffi.h', [], inc_dirs)
if ffi_inc is not None:
ffi_h = ffi_inc[0] + '/ffi.h'
if not os.path.exists(ffi_h):
ffi_inc = None
print('Header file {} does not exist'.format(ffi_h))
ffi_lib = None
if ffi_inc is not None:
for lib_name in ('ffi', 'ffi_pic'):
if (self.compiler.find_library_file(lib_dirs, lib_name)):
ffi_lib = lib_name
break
ffi_lib="ffi" # --- AND INSERT THIS LINE HERE THAT DOES NOT APPEAR ---
if ffi_inc and ffi_lib:
ext.include_dirs.extend(ffi_inc)
ext.libraries.append(ffi_lib)
self.use_system_libffi = True
and add the line I have marked above with the comment. Why it is necessary, and why there is no way to get configure to respect ‘–without-system-ffi` on Linux platforms, perhaps I will find out why that is “unsupported” in the next couple of hours, but everything has worked ever since. Otherwise, best of luck… YMMV.
WHAT IT DOES: just overrides the logic there and causes the compiler linking command to add “-lffi” which is all that it really needs. If you have the library user-installed, it is probably detecting the headers fine as long as your PKG_CONFIG_PATH
includes path/to/your/install/root/lib/pkgconfig
.