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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27022373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:20:42  来源:igfitidea点击:

Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing

pythoncompiler-errorsinstallpython-3.4failed-installation

提问by htc_m8

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:

我正在使用 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'

I just updated to 3.3.2 through installing the source of 3.4.0. It installed in /usr/local/lib/python3.4.

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

Did I update to Python 3.4 correctly?

我是否正确更新到 Python 3.4?

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.

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

回答by MikeiLL

On a fresh Debian image, cloning https://github.com/python/cpythonand running:

在新的 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

Now execute the configurefile cloned above:

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

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

Got 3.7 installed and working for me.

安装了 3.7 并为我工作。

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 postexplains why certain libraries like python-devmight be necessary.
  • this SO postexplains why one might use the altinstallas opposed to installargument in the makecommand.
  • 这篇 SO 帖子解释了为什么python-dev可能需要某些类似的库。
  • 这篇 SO post解释了为什么人们可能会在make命令中使用与参数altinstall相反的install参数。

Aside from that I guess the choice would be to either read through the cpython codebase looking for #includedirectives 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.

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

Reminds me of the story of the Engineer, the Manager and the Programmer whose car rolls down a hill.

让我想起工程师、经理和程序员的汽车从山上滚下来的故事。

回答by S.E Furgeson

Thought I'd add the Centos installs:

以为我会添加 Centos 安装:

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

Check python version:

检查python版本:

python3 -V

python3 -V

Create virtualenv:

创建虚拟环境:

virtualenv -p python3 venv

virtualenv -p python3 venv

回答by dotbit

Installing libffi-devand re-installing python3.7 fixed the problem for me.

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

to cleanly build py 3.7 libffi-devis required or else later stuff will fail

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

If using RHEL/Fedora:

如果使用 RHEL/Fedora:

yum install libffi-devel

or

或者

sudo dnf install libffi-devel

If using Debian/Ubuntu:

如果使用 Debian/Ubuntu:

sudo apt-get install libffi-dev

回答by Alok Deep

Detailed steps to install Python 3.7 in CentOS or any redhat linux machine:

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

  1. Download Python from https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
  2. Extract the content in new folder
  3. Open Terminal in the same directory
  4. Run below code step by step :
  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
sudo yum -y install gcc gcc-c++ 
sudo yum -y install zlib zlib-devel
sudo yum -y install libffi-devel 
./configure
make
make install

回答by pengchy

Refer to 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_LDFLAGSvariable in the Makefile, for example CONFIGURE_LDFLAGS="-L/path/to/libffi-3.2.1/lib64".

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

回答by akpp

I run into this error when I tried to install Python 3.7.3in Ubuntu 18.04with 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.

我遇到这个错误,当我试图安装的Python 3.7.3的Ubuntu 18.04与下一个命令:$ pyenv install 3.7.3。运行后安装成功$ sudo apt-get update && sudo apt-get install libffi-dev(如建议here)。问题在那里得到解决。

回答by Arash Mohammadi

None of the solution worked. You have to recompile your python again; once all the required packages were completely installed.

没有一个解决方案有效。你必须再次重新编译你的python;一旦完全安装了所有必需的软件包。

Follow this:

按照这个:

  1. Install required packages
  2. Run ./configure --enable-optimizations
  1. 安装所需的包
  2. ./configure --enable-optimizations

https://gist.github.com/jerblack/798718c1910ccdd4ede92481229043be

https://gist.github.com/jerblack/798718c1910ccdd4ede92481229043be

回答by Evgeniy

If you use pyenv and get error "No module named '_ctypes'" (like i am) on Debian/Raspbian/Ubuntu you need to run this commands:

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

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

把你的python版本而不是3.7.6

回答by slowkoni

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:

如果你在做某事,这里没有人会听你说,因为“你做错了”,但你必须“以错误的方式”做这件事,原因太愚蠢了(例如,在我的情况下,它很快就会变成犯规关于 devops 团队中某人超重的母亲的话),您首先需要:

Get libffi and install it into your user install area the usual way.

获取 libffi 并以通常的方式将其安装到您的用户安装区域。

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

然后回到你的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

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.

并添加我在上面用评论标记的行。为什么它是必要的,为什么在 Linux 平台上没有办法让配置尊重“--without-system-ffi”,也许我会在接下来的几个小时内找出为什么“不受支持”,但一切都有从那以后一直工作。否则,祝你好运...... 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_PATHincludes path/to/your/install/root/lib/pkgconfig.

它的作用:只是覆盖那里的逻辑并导致编译器链接命令添加它真正需要的“-lffi”。如果您安装了用户安装的库,那么只要您PKG_CONFIG_PATH包含path/to/your/install/root/lib/pkgconfig.

回答by Christopher Bottoms

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).

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