简体   繁体   English

通过 github 将模块导入笔记本时,如何让子模块也导入?

[英]When importing a module via github into a notebook, how can I get the sub modules to import also?

I'm playing with a library and am doing this in my colab notebook:我正在玩图书馆,并在我的 colab 笔记本中这样做:

!git clone https://github.com/ProjectDrawdown/solutions

this creates a folder called solutions in my root folder, then I do this -这会在我的根文件夹中创建一个名为 solutions 的文件夹,然后我这样做 -

# Import modules we'll use later
import sys; sys.path.insert(0, '..')
from solutions.solution import factory

The root module imports but the sub ones do not.根模块导入但子模块不导入。

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-8-b5d7e6c5ec59> in <module>
      4 # Import modules we'll use later
      5 import sys; sys.path.insert(0, '..')
----> 6 from solutions.solution import factory
      7 import pandas as pd
      8 import numpy as np

/content/solutions/solution/factory.py in <module>
      4 from pathlib import Path
      5 from functools import lru_cache
----> 6 from model import advanced_controls as ac
      7 from model import scenario
      8 from model import vma

ModuleNotFoundError: No module named 'model'

Is there anything I can do to fix this?我能做些什么来解决这个问题吗? It would be nice to sometimes just import this via colab so I can quickly play with a git repo有时通过 colab 导入它会很好,这样我就可以快速使用 git 回购

You can simply import all with:您可以简单地导入所有内容:

from solutions.solution import *

Although do note that using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.尽管请注意,在 python 程序中使用 import * 被认为是一个坏习惯,因为这样会污染您的命名空间,但 import * 语句会将所有函数和类导入您自己的命名空间,这可能会与您定义的函数或函数发生冲突您导入的其他库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM