【问题标题】:ModuleNotFoundError after successful pip install in Google Colaboratory在 Google Colaboratory 中成功安装 pip 后出现 ModuleNotFoundError
【发布时间】:2020-01-10 06:37:54
【问题描述】:

我正在尝试将我编写的包导入 Google Colaboratory 笔记本。我将包内容上传到我的云端硬盘,然后运行:

[ ] from google.colab import drive
[ ] drive.mount('content/gdrive/')
[ ] ! pip install --user /content/gdrive/My\ Drive/my-package
Processing ./gdrive/My Drive/my-package
Building wheels for collected packages: my-package
  Building wheel for my-package (setup.py) ... done
  Created wheel for my-package: 
  Stored in directory: 
Successfully built my-package
Installing collected packages: my-package
Successfully installed my-package-1.0.0.dev1

pip list 表示安装成功。但是,包的导入失败并显示 ModuleNotFoundError

我已经在本地机器上成功安装并导入了 my-package。我还使用pip install --user 通过同一个 Colab 笔记本成功安装并导入了另一个 Python 包。如here 所述,我也尝试过重启内核。

这可能与this 相关但未回答的问题有关。

【问题讨论】:

    标签: python pip google-colaboratory


    【解决方案1】:

    您链接到的关于重新启动运行时的 github 页面在 IMO 有点模棱两可,所以我只想澄清一下:

    您需要运行!pip install 单元格。 然后“重新启动运行时。” 然后运行你的import语句单元。

    我可能会建议您在执行这些步骤之前“重置所有运行时”,以确保您有一个干净的状态。

    -- 如果上述步骤你已经做过的: 您使用的是 Python 2 还是 3 笔记本? (不是 100% 确定这很重要,但更多信息会更好)

    您是否从 google 驱动器链接 pip install 到您的本地计算机? (如果没有,请尝试看看是否有效并报告)

    【讨论】:

    • 我使用的是 Python 3 笔记本。
    【解决方案2】:

    感谢@jojo 的建议,我在本地机器上卸载并重新安装了该软件包,并且能够诊断出问题。在我的本地机器和 Colab 中,只有在 pip 命令 (pip install --user -e /content/gdrive/My\ Drive/my-package) 中包含 -e(可编辑)标志并在安装后重新启动运行时,我才能成功安装和导入包。我不知道为什么包含-e 标志会有所作为;如果您有任何见解,请发表评论!

    【讨论】:

      【解决方案3】:

      可编辑安装(或setuptools development-mode)将模块路径附加到easy-install.pth 文件。 site 模块在 python 启动时处理这些文件并将路径附加到 sys.path。这就是为什么它只有在重新启动运行时才能工作。

      可以通过导入site 模块并(重新)运行site.main() 来避免重启colab notebook。

      %pip install -e pkg
      import site
      site.main()
      import pkg
      

      但是在下面的示例中,这具有从sys.path 中删除当前目录并将其替换为其绝对路径的效果。

      %pip install -e "git+https://github.com/jd/tenacity#egg=tenacity"
      
      print("\nTrying to import tenacity")
      try:
          import tenacity
      except ImportError as exc:
          print("ImportError")
          print(exc)
          print()
      
      import sys, site
      
      print("\n##### sys.path original #####")
      for p in sys.path:
          print(f"'{p}'")
      print()
      
      site.main()
      
      print("\n##### sys.path after site.main() #####")
      for p in sys.path:
          print(f"'{p}'")
      print()
      
      import tenacity
      print(f"\nImported tenacity from {tenacity.__file__}")
      

      打印

      Obtaining tenacity from git+https://github.com/jd/tenacity#egg=tenacity
        Cloning https://github.com/jd/tenacity to ./src/tenacity
        Running command git clone -q https://github.com/jd/tenacity /content/src/tenacity
      Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.6/dist-packages (from tenacity) (1.15.0)
      Installing collected packages: tenacity
        Running setup.py develop for tenacity
      Successfully installed tenacity
      
      Trying to import tenacity
      ImportError
      No module named 'tenacity'
      
      
      ##### sys.path original #####
      ''
      '/env/python'
      '/usr/lib/python36.zip'
      '/usr/lib/python3.6'
      '/usr/lib/python3.6/lib-dynload'
      '/usr/local/lib/python3.6/dist-packages'
      '/usr/lib/python3/dist-packages'
      '/usr/local/lib/python3.6/dist-packages/IPython/extensions'
      '/root/.ipython'
      
      
      ##### sys.path after site.main() #####
      '/content'
      '/env/python'
      '/usr/lib/python36.zip'
      '/usr/lib/python3.6'
      '/usr/lib/python3.6/lib-dynload'
      '/usr/local/lib/python3.6/dist-packages'
      '/usr/lib/python3/dist-packages'
      '/usr/local/lib/python3.6/dist-packages/IPython/extensions'
      '/root/.ipython'
      '/content/src/tenacity'
      
      
      Imported tenacity from /content/src/tenacity/tenacity/__init__.py
      

      示例 colab 笔记本:https://colab.research.google.com/drive/1S5EU-MirhaTWz1JJVdos3GcojmOSLC1C?usp=sharing

      通过博文偶然发现:https://yidatao.github.io/2016-05-10/Python-easyinstall-generates-pth-file/

      【讨论】:

        猜你喜欢
        • 2017-11-15
        • 2019-06-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 2021-05-08
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        相关资源
        最近更新 更多