-1

I was trying to connect Google Colab to my VS Code and I successfully did that but when connected to Colab kernel, I can't read files that are in my local machine. But in local Python environment the code can successfully read the file

with open ("./sample_data/sample.txt", 'r') as file:
    content = file.readlines()
    print(content)

The code is fine. It run in totally fine in the local python environment, but not in colab ipkarnel.

2
  • as I know Google Colab runs on server and it can have access to files on Google Drive. It can't access directly for security reason. For local file you have to upload them on Google Drive or on space in Google Colab (virtual) machine. (files.upload()). I don't remeber but there is method to run Google Colab with access to local files (and then it doesn't have access to Google Drive) but it may need some settings. Commented Apr 11 at 10:35
  • Google Colab has direct access to local file only when you switch "runtime" from "Google Colab server" to "local computer" but then it runs all code on local computer and it can use only local GPU. It doesn't have access to GPU/TPU on Google server. And it needs to run local Jupyter as special server which Google Colab uses to access local files and execute code. So all this needs a lot of work and it can be useless if you need access to GPU/TPU. Commented Apr 11 at 12:12

1 Answer 1

1

By default, Notebook runs in a sandboxed, Google-hosted runtime. That's usually a good thing; if there's any doubt as to the integrity of the code you're running - or if you don't want to use your own machine's compute to run the program - you can just get Google's servers to do it for you. However, it also means that Colab doesn't have access to anything stored locally. Usually, this is fine since importing data into Colab (whether by mounting your drive or uploading individual files) will serve the purpose you need.

If you're still very certain that you want to run locally - which gives Colab access to your local files but also loses all the benefits of a sandbox - you can start a "local runtime". For the VS Code extension, you do this by clicking "Select Kernel" in the top right and choosing "Python Environments" rather than "Colab".

If you're on the web, the local runtime documentation says you'll need to run the following Jupyter Notebook command (given that you've already installed Jupyter):

jupyter notebook \
    --NotebookApp.allow_origin='https://colab.research.google.com' \
    --port=8888 \
    --NotebookApp.port_retries=0 \
    --NotebookApp.allow_credentials=True

Then, click the "Connect" button (where you would usually select your parameters for running the program, such as CPU/GPU type) and choose "Connect to local runtime...", entering arguments from the above command's output as they are requested.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.