【问题标题】:Cant install custom package in Google Colab无法在 Google Colab 中安装自定义包
【发布时间】:2020-06-09 17:31:45
【问题描述】:

我有一个自己编写的自定义包,叫做 deblurrer,它是一堆用于训练神经网络的脚本。

在 Google Colab 中,我成功克隆了我的 repo,我拥有执行 setup.py 模块和安装 deblurrer 1.0.0 所需的所有东西。当我在我的电脑上本地安装 deblurrer 时,一切正常,但是当我尝试在 Colab 中运行 !python setup.py install 时,没有安装任何东西,事实上,输出显示一切都很好,但我无法导入包。在两个单独的 Colab 单元中执行下一个代码以重现问题:

# Cell 01
# Executes the cell in bash mode
%%bash

git clone https://github.com/ElPapi42/deep-deblurring
python deep-deblurring/setup.py install
# Cell 02
import deblurrer

如您所见,安装运行正常,但导入时:ModuleNotFoundError: No module named 'deblurrer'

有什么问题?

【问题讨论】:

  • 也许你必须使用不同的 python - 即。 python3python3.7 ?你可以检查python -V 看看它是否不是 Python 2
  • 我仔细检查了一下,实际上 Google Colab 使用的是 Python 3.6
  • 系统可能已经安装了许多不同版本的 Python,您可以为一个版本安装模块,但 Colab 可能使用不同的版本来运行代码。每个版本都使用自己的模块。尝试使用python3.6 - !python3.6 setup.py install进行安装
  • 你好!我使用python3.6 deep-deblurring/setup.py install 安装并安装了软件包,但是导入时仍然无法正常工作,说找不到模块
  • 检查 import sys print( sys.executable ) - 也许它不使用 3.63.53.7 或其他东西。

标签: python jupyter-notebook google-colaboratory python-packaging


【解决方案1】:

您必须对 Colab 采取稍微不同的方法。

# 1. Download the repo and set it as the current directory
!git clone https://github.com/ElPapi42/deep-deblurring
%cd deep-deblurring

# 2. install the project/module
!python setup.py install

# 3. Add the project directory to the path
import os, sys
sys.path.append(os.getcwd())

#4. Run your code
# ....

如此处所述https://*.com/a/53747334/2466781

【讨论】: