首页 > 解决方案 > 如何修复“导入错误:无法导入 cffi”

问题描述

我正在按照说明安装 OpenAI Universe。我在以 Windows 10 作为主机的虚拟机上使用 Ubuntu 18.04。我需要构建 docker 来完成这个过程,但是当我给出命令时:

docker build -t universe .

我收到一条错误消息:

ImportError:没有名为“cffi”的模块

我运行了python并导入了cffi,它成功导入了。我使用 pip 命令重新安装了它,仍然不行。

我才开始使用码头工人,我对他们知之甚少。即使在安装了 cffi 之后,我也不知道为什么会发生这种情况。

如何修复此错误?

以下是命令的完整输出:

docker build -t universe .

输出:

Sending build context to Docker daemon  2.209MB
Step 1/13 : FROM ubuntu:16.04
 ---> 13c9f1285025
Step 2/13 : RUN apt-get update     && apt-get install -y libav-tools     python3-numpy     python3-scipy     python3-setuptools     python3-pip     libpq-dev     libjpeg-dev     curl     cmake     swig     python3-opengl     libboost-all-dev     libsdl2-dev     wget     unzip     git     golang     net-tools     iptables     libvncserver-dev     software-properties-common     && apt-get clean     && rm -rf /var/lib/apt/lists/*
 ---> Using cache
 ---> c3c5dcdf750c
Step 3/13 : RUN ln -sf /usr/bin/pip3 /usr/local/bin/pip     && ln -sf /usr/bin/python3 /usr/local/bin/python     && pip install -U pip
 ---> Using cache
 ---> c653f4a9e438
Step 4/13 : RUN pip install gym[all]
 ---> Running in ded29a916c4b
Collecting gym[all]
  Downloading ""link""
  Ignoring enum34: markers 'python_version < "3.4"' don't match your environment
Requirement already satisfied: scipy in /usr/lib/python3/dist-packages (from gym[all]) (0.17.0)
Requirement already satisfied: numpy>=1.10.4 in /usr/lib/python3/dist-packages (from gym[all]) (1.11.0)
Collecting six (from gym[all])
  Downloading ""link""
Collecting pyglet<=1.3.2,>=1.2.0 (from gym[all])
  Downloading ""link""
Collecting cloudpickle~=1.2.0 (from gym[all])
  Downloading ""link""
Collecting mujoco_py<2.1,>=1.50 (from gym[all])
  Downloading ""link""
Collecting imageio (from gym[all])
  Downloading ""link""
Collecting box2d-py~=2.3.5 (from gym[all])
  Downloading ""link""
Collecting atari_py~=0.2.0 (from gym[all])
  Downloading ""link""
Collecting Pillow (from gym[all])
  Downloading ""link""
Collecting opencv-python (from gym[all])
  Downloading ""link""
Collecting future (from pyglet<=1.3.2,>=1.2.0->gym[all])
  Downloading ""link""
  Downloading ""link""
Collecting Cython>=0.27.2 (from mujoco_py<2.1,>=1.50->gym[all])
  Downloading ""link""
Collecting cffi>=1.10 (from mujoco_py<2.1,>=1.50->gym[all])
  Downloading ""link""
Collecting lockfile>=0.12.2 (from mujoco_py<2.1,>=1.50->gym[all])
  Downloading ""link""
Collecting pycparser (from cffi>=1.10->mujoco_py<2.1,>=1.50->gym[all])
  Downloading ""link""
Building wheels for collected packages: gym, mujoco-py, future, glfw, pycparser
  Building wheel for gym (setup.py): started
  Building wheel for gym (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/95/14/8e/b4f5c72600f654312b40c0844d4c23f146f291c48ac7a5df62
  Building wheel for mujoco-py (setup.py): started
  Building wheel for mujoco-py (setup.py): finished with status 'error'
  ERROR: Complete output from command /usr/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-y60v1wum/mujoco-py/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-4i3731kh --python-tag cp35:
  ERROR: running bdist_wheel
  running build
  Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-install-y60v1wum/mujoco-py/setup.py", line 45, in <module>
      tests_require=read_requirements_file('requirements.dev.txt'),
    File "/usr/lib/python3.5/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/usr/lib/python3.5/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/usr/lib/python3/dist-packages/wheel/bdist_wheel.py", line 179, in run
      self.run_command('build')
    File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/tmp/pip-install-y60v1wum/mujoco-py/setup.py", line 29, in run
      import mujoco_py  # noqa: force build
    File "/tmp/pip-install-y60v1wum/mujoco-py/mujoco_py/__init__.py", line 3, in <module>
      from mujoco_py.builder import cymj, ignore_mujoco_warnings, functions, MujocoException
    File "/tmp/pip-install-y60v1wum/mujoco-py/mujoco_py/builder.py", line 16, in <module>
      from cffi import FFI
  ImportError: No module named 'cffi'
  ----------------------------------------
  ERROR: Failed building wheel for mujoco-py
  Running setup.py clean for mujoco-py
  Building wheel for future (setup.py): started
  Building wheel for future (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/0c/61/d2/d6b7317325828fbb39ee6ad559dbe4664d0896da4721bf379e
  Building wheel for glfw (setup.py): started
  Building wheel for glfw (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/fa/26/dd/ff8199187d59bcd5bd4543f286fabfb20642d921a912aa3775
  Building wheel for pycparser (setup.py): started
  Building wheel for pycparser (setup.py): finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/f2/9a/90/de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511
Successfully built gym future glfw pycparser
Failed to build mujoco-py
ERROR: gym 0.13.1 requires enum34~=1.1.6, which is not installed.
ERROR: opencv-python 4.1.0.25 has requirement numpy>=1.11.1, but you'll have numpy 1.11.0 which is incompatible.
Installing collected packages: six, future, pyglet, cloudpickle, glfw, Cython, Pillow, imageio, pycparser, cffi, lockfile, mujoco-py, box2d-py, atari-py, opencv-python, gym
  Running setup.py install for mujoco-py: started
    Running setup.py install for mujoco-py: finished with status 'error'
    ERROR: Complete output from command /usr/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-y60v1wum/mujoco-py/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-2yrsd2ff/install-record.txt --single-version-externally-managed --compile:
    ERROR: running install
    running build
    
    You appear to be missing MuJoCo.  We expected to find the file here: /root/.mujoco/mujoco200
    
    This package only provides python bindings, the library must be installed separately.
    
    Please follow the instructions on the README to install MuJoCo
    
        https://github.com/openai/mujoco-py#install-mujoco
    
    Which can be downloaded from the website
    
        ""link""
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-y60v1wum/mujoco-py/setup.py", line 45, in <module>
        tests_require=read_requirements_file('requirements.dev.txt'),
      File "/usr/lib/python3.5/distutils/core.py", line 148, in setup
        dist.run_commands()
      File "/usr/lib/python3.5/distutils/dist.py", line 955, in run_commands
        self.run_command(cmd)
      File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/usr/lib/python3/dist-packages/setuptools/command/install.py", line 61, in run
        return orig.install.run(self)
      File "/usr/lib/python3.5/distutils/command/install.py", line 583, in run
        self.run_command('build')
      File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/tmp/pip-install-y60v1wum/mujoco-py/setup.py", line 29, in run
        import mujoco_py  # noqa: force build
      File "/tmp/pip-install-y60v1wum/mujoco-py/mujoco_py/__init__.py", line 3, in <module>
        from mujoco_py.builder import cymj, ignore_mujoco_warnings, functions, MujocoException
      File "/tmp/pip-install-y60v1wum/mujoco-py/mujoco_py/builder.py", line 502, in <module>
        mujoco_path, key_path = discover_mujoco()
      File "/tmp/pip-install-y60v1wum/mujoco-py/mujoco_py/utils.py", line 93, in discover_mujoco
        raise Exception(message)
    Exception:
    You appear to be missing MuJoCo.  We expected to find the file here: /root/.mujoco/mujoco200
    
    This package only provides python bindings, the library must be installed separately.
    
    Please follow the instructions on the README to install MuJoCo
    
        https://github.com/openai/mujoco-py#install-mujoco
    
    Which can be downloaded from the website
    
        ""link""
    
    ----------------------------------------
ERROR: Command "/usr/bin/python3 -u -c 'import setuptools, tokenize;__file__='"'"'/tmp/pip-install-y60v1wum/mujoco-py/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-2yrsd2ff/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-y60v1wum/mujoco-py/
The command '/bin/sh -c pip install gym[all]' returned a non-zero code: 1

注意:我已将输出中的下载链接替换为“链接”,因为堆栈不允许我包含超过 8 个链接。

Docker 版本:18.09.7,构建 2d0083d

Linux 版本:Ubuntu 18.04

蟒蛇版本:3.7.3

标签: pythonlinuxdockeropenai-gymcffi

解决方案


推荐阅读