Importing .py files in Google Colab

前端 未结 10 2099
清酒与你
清酒与你 2020-11-28 22:38

Is there any way to upload my code in .py files and import them in colab code cells?

The other way I found is to create a local Jupyter notebook then upload it to C

相关标签:
10条回答
  • 2020-11-28 23:05

    Try this way:

    I have a package named plant_seedlings. The package is stored in google drive. What I should do is to copy this package in /usr/local/lib/python3.6/dist-packages/.

    !cp /content/drive/ai/plant_seedlings.tar.gz /usr/local/lib/python3.6/dist-packages/
    
    !cd /usr/local/lib/python3.6/dist-packages/ && tar -xzf plant_seedlings.tar.gz
    
    !cd /content
    
    !python -m plant_seedlings
    
    0 讨论(0)
  • 2020-11-28 23:08
    1. You can upload local files to google colab by using upload() function in google.colab.files
    2. If you have files on github, then clone the repo using !git clone https://github.com/username/repo_name.git. Then just like in jupyter notebook load it using the magic function %load %load filename.py.
    0 讨论(0)
  • 2020-11-28 23:12

    You can save it first, then import it.

    from google.colab import files
    src = list(files.upload().values())[0]
    open('mylib.py','wb').write(src)
    import mylib
    

    Update (nov 2018): Now you can upload easily by

    • click at [>] to open the left pane
    • choose file tab
    • click [upload] and choose your [mylib.py]
    • import mylib

    Update (oct 2019): If you don't want to upload every time, you can store it in S3 and mount it to Colab, as shown in this gist

    Update (apr 2020): Now that you can mount your Google Drive automatically. It is easier to just copy it from Drive than upload it.

    • Store mylib.py in your Drive
    • Open a new Colab
    • Open the (left)side pane, select Files view
    • Click Mount Drive then Connect to Google Drive
    • Copy it by !cp "drive/My Drive/mylib.py" .
    • import mylib
    0 讨论(0)
  • 2020-11-28 23:12

    You can upload those .py files to Google drive and allow Colab to use to them:

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

    All your files and folders in root folder will be in drive.

    0 讨论(0)
  • 2020-11-28 23:16

    I face the same problem. After reading numerous posts, I would like to introduce the following solution I finally chose over many other methods (e.g. use urllib, httpimport, clone from GitHub, package the modules for installation, etc). The solution utilizes Google Drive API (official doc) for proper authorization.

    Advantages:

    1. Easy and safe (no need for code to handle file operation exceptions and/or additional authorization)
    2. Module files safeguarded by Google account credentials (no one else can view/take/edit them)
    3. You control what to upload/access (you can change/revoke access anytime on a file-by-file basis)
    4. Everything in one place (no need to rely upon or manage another file hosting service)
    5. Freedom to rename/relocate module files (not path-based and won't break your/other's notebook code)

    Steps:

    1. Save your .py module file to Google Drive - you should have that since you're already using Colab
    2. Right click on it, "Get shareable link", copy the part after "id=" - the file id assigned by Google Drive
    3. Add and run the following code snippets to your Colab notebook:
    !pip install pydrive                             # Package to use Google Drive API - not installed in Colab VM by default
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    
    from google.colab import auth                    # Other necessary packages
    from oauth2client.client import GoogleCredentials
    
    auth.authenticate_user()                         # Follow prompt in the authorization process
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    
    drive = GoogleDrive(gauth)
    your_module = drive.CreateFile({"id": "your_module_file_id"})   # "your_module_file_id" is the part after "id=" in the shareable link
    your_module.GetContentFile("your_module_file_name.py")          # Save the .py module file to Colab VM
    import your_module_file_name                                    # Ready to import. Don't include".py" part, of course :)
    

    Side note

    Last but not least, I should credit the original contributor of this approach. That post might have some typo in the code as it triggered an error when I tried it. After more reading and troubleshooting my code snippets above worked (as of today on Colab VM OS: Linux 4.14.79).

    0 讨论(0)
  • 2020-11-28 23:22

    It's Jun 2019. Make sure in the Python package's __init__.py all related files are imported in order. Push the code to Git or use this code.

    for e.g,

    from .Boxes import *
    from .Circles import *
    from .Rectangles import *
    ...
    
    

    Don't use Package name in __init__.py file for importing the files.

    in Google colab,

    ! rm -rf SorghumHeadDetection
    ! git clone https://github.com/user/amazing-repo-name/
    
    0 讨论(0)
提交回复
热议问题