Is it possible to import my own modules into a google-colaboratory notebook?

前端 未结 2 623
执笔经年
执笔经年 2020-12-05 12:02

I am using google colaboratory for teaching Data Science with python and in order to leave the notebooks only with the specific lesson for the students I would like to impor

相关标签:
2条回答
  • 2020-12-05 12:51

    How can I import these methods without publishing them on pip?. Can google-colaborary pip install from github?

    Yes, you can do pip install from github by running bash commands (by appending ! to the commands) in collab. For example:

    !pip install git+<github_link>
    

    The best option for us would be to be able to have the code in Drive and an upload the module to colab space like we do with the csv files, and use standard import. Is that possible?

    This is a bit tricky but can be done by mounting your google-drive on your google collab instance using [google-drive-ocamlfuse][1].

    You will need to install ocamlfuse and get permissions for your google account using:

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    

    and then mount google drive using:

    !mkdir -p drive
    !google-drive-ocamlfuse drive
    

    After that you can check if the mount was successful using:

    !ls drive
    

    which should show all the files in your google drive.

    0 讨论(0)
  • 2020-12-05 12:51

    I have a dirty trick that does precisely this. With all my code in one package I can do

    !wget mysite/mypackage.py
    from mypackage import *
    

    Similarly if mypackage has dependencies, I can wget a zipfile and !unzip it

    0 讨论(0)
提交回复
热议问题