How to import python files in google colaboratory?

后端 未结 4 2016
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 15:33

I am trying to run my program on Google Colab; where my code make use of .py files written seprately.

In normal system I have all files inside one folder and it wor

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-02 15:51

    If you have just 2-3 files, you can try the solution I gave in another question here.

    Importing .py files in Google Colab

    But if you have something like 5-10 files, I would suggest you put your library on github, then !git clone it to Google Colab. Another solution is to zip all you library files, then modify the first solution by unzipping with !unzip mylib.zip

    If those library files are not in a folder structure, just a few files in the same folder. You can upload and save them then import them. Upload them with:

    def upload_files():
      from google.colab import files
      uploaded = files.upload()
      for k, v in uploaded.items():
        open(k, 'wb').write(v)
      return list(uploaded.keys())
    

提交回复
热议问题