I have a python project where the main .py file imports other .py files and the main file is then run for different input arguments using a .sh file. Can anyone guide me how to transfer and run the entire project on Google Colab?
Commit your files to a git repo.
In a new google colab file, run
!git clone <http url of your repo>
Then execute your program
!python main.py --foo bar
Alternatively, you could upload your project to google drive and then import the project from there.
Related
Scenario:
You have a github repo you want to work on. The repo has the following file structure:
src
|
-- directory containing .py files
notebook1.ipynb
notebook2.ipynb
You head to colab and create a new empty notebook as the entry point between your repo and the google colab runtime.
On that empty colab notebook you add the following command to clone your github repo:
!git clone your_repo_address
Checking the colab file explorer we seen that our repo and file format are copied to colab runtime.
So far so good, now say you want to open notebook1.ipynb and execute cells and work on it.
How the hell you do that?
Every time I try that it opens in a file explorer without the possibility to execute the notebook cells.
Why can't colab work similar to jupyter. It's extremely cumbersome and time wasting in this regard compared to jupyter.
I am learning python programing using Google Colab. When i open two google colab files from the same account, the folders in content drive are different.
I wanted to know that how can we make the two Content Folder of the two Google Colab files same.
Thanks
Each Google Colab session allocates different resources from Google Cloud. You cannot combine them. Instead, you can connect the notebook to Google Drive account, where you can create a folder and manage its contents.
Step 1: Connect to Google Drive:
from google.colab import drive
drive.mount('/content/drive')
Step 2: Create a folder:
!cd /content/drive/My Drive
!mkdir mydirectory
Step 3: Move to the newly created directory and start working there:
!cd mydirectory
After this, any OS-related operations (create, update, delete files, etc.) you perform will take place in this directory.
You can also create a Jupyter Notebook within this directory and work with that notebook, although that is not strictly necessary.
I want to run deep learning models in Google Colab that has a power GPU support. I am quite new to Colab. Originally I though I could uplod the images using os.path.join and PIL.Image.open as I did in the environment of Spyder. But the Colab gives a FileNotFoundError FileNotFoundError: [Errno 2] No such file or directory (the images indeed exist in a local directory). It seems I did not do correctly for the uploading.
You can directly upload to colab or upload images to a google drive folder. Then mount that folder in google colab to use it. On the left there is an arrow that opens a sidebar which contains searchable code snippets. Here, you can search for drive to get google drive related code snippets.
This notebook contains some examples,
https://colab.research.google.com/notebooks/snippets/drive.ipynb#scrollTo=u22w3BFiOveA
The command below list contents of folder named DeepLearning,
!ls -la "/content/gdrive/My Drive/DeepLearning"
Copy contents of drive DeepLearning folder to Virtual Machine in DeepLearning folder,
!cp /content/gdrive/My\ Drive/DeepLearning ./DeepLearning
Copy contents of Virtual Machine DeepLearning folder to google drive DeepLearning folder,
!cp ./DeepLearning /content/gdrive/My\ Drive/DeepLearning
You can run %cd DeepLearning to change directory to DeepLearning folder.
GPU support can be enabled by Runtime > Change Runtime Type > Hardware Accelerator > GPU.
I uploaded a Jupyter notebook to Google Colab. The notebook accessed a couple of images from local path /images/pic1.png How to upload data to the directory in Colab where the notebook is running? Please note that these are temporary files. So, I don't mind them being deleted on terminating the session.
I used Files upload feature but it doesn't seem to upload to Google Drive. I want to upload the folder with the same hierarchy as in the local environment without needing any change in the paths of the files in the code.
The better way I've found is mounting a folder in your Google Drive. Upload the image folder to your drive. After that, in your notebook you write:
from google.colab import drive
drive.mount('/content/drive')
After you allow the authentication, you can work using the path "/content/drive/My Drive" with the path for your image folder. Like that:
from IPython.display import Image
Image("/content/drive/My Drive/images/pic1.png")
See more in the notebook with documentation: https://colab.research.google.com/notebooks/io.ipynb
I followed the steps given in this Medium tutorial on google colab and then tried to clone a git repository but I cannot see the repository anywhere in my drive.
The following image is the code snippet I used which is exactly the same as that from the Medium tutorial:
To mount google drive on google Colab just use these commands:
from google.colab import drive
drive.mount('/content/drive')
It would need an authentication process. Do whatever it needs (open the link and copy the key)
Now you have mounted your google drive in /content/drive/ directory in your google Colab machine.
To clone a git repository, first set your current directory as a path in /content/drive/, then just clone the git repository. Like below:
path_clone = "/content/drive/my_project"
%cd path_clone
!git clone <Git project URL address>
Now you would have the cloned Git project in my_projects folder in your Google Drive (which is also connected to your Google Colab runtime machine)
Adjust ur path where you git clone.
try adding drive folder to path
import os
os.chdir("drive")
Just do cd drive (without !) or %cd drive.
See cd vs !cd vs %cd in IPython.