67

I cloned a github repo using !git clone https://github.com/llSourcell/Pokemon_GAN.git. I wanted to modify a .py file inside Colab. So i used %load filename.py as suggested here (How to load/edit/run/save text files (.py) into an IPython notebook cell?). But whenever i run this command, i get disconnected after some time. I was wondering if there is some other way to edit .py file without undergoing the hassle of downloading it to pc,editing and then re uploading it.

starball
  • 20,030
  • 7
  • 43
  • 238
randomName
  • 671
  • 1
  • 5
  • 3
  • 2
    There's no particularly good way to edit directly in colab right now. Can you file an issue with repro steps for the hang on `%load` at https://github.com/googlecolab/colabtools? – Craig Citro Feb 09 '18 at 05:35

14 Answers14

63

In the early days of Colab you could have used Ipython magic commands. Use below command

%pycat code.py

A pop up will appear displaying the code. You can copy it and edit it locally.
Remove the file using below command

!rm code.py

Copy the edited code to a cell in notebook and add below command at the top of the cell

%%writefile code.py

Run the cell. A file will be created with the contents present in the cell.

Updates: Now there are lot more easy and convenient options.

  1. In the files section, there is an option to upload files or you can double click on the file, make changes and ctrl+s to save those changes.
  2. You can also use https://github.com/abhishekkrthakur/colabcode to edit using visual studio code server.
brianlmerritt
  • 2,644
  • 1
  • 34
  • 39
rahul
  • 1,133
  • 12
  • 17
43

Colab includes a text editor you can use to create, open, and delete .py files directly.

All is done in the Files view (see below).

  • To create or delete a file, right click and choose "New file" or "Delete file".
  • To edit a file, double click on it. It appears on the right portion of your screen. Make any changes you wish. Changes are saved automatically.

enter image description here

mherzog
  • 1,085
  • 1
  • 12
  • 24
Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • 1
    simplest thing are best! – user305883 Oct 30 '19 at 13:53
  • 4
    It's the sort of thing I was looking for. However, after editing the python there, colab was still using the old version of the file. I had to right click and do that Refresh which is shown in the left-hand part of the screen for it to be truly updated. A little maddening that I cannot accomplish things in the right window. – demongolem May 01 '20 at 16:33
  • 3
    how to open the file editor? double click the file? – cloudscomputes Jul 09 '20 at 08:45
  • Thanks! The UI doesn't make it clear that you can double-click a file to open it. Also not clear how to save the edits, but refreshing the file worked for me. – roygbiv Feb 21 '23 at 15:11
24

Unfortunately, it seems, colab do not support %load line magic (yet), and yet, you can see the file content using !cat your_file.py and then manually, copy the output contents, write them to a new cell and write %%writefile your_new_file_name.py at the top of the new cell to save this back to the instance. Note that, this will not be saved to your google drive yet.

Example:
!ls
output: colabData/

%%writefile something.py
print("everything's fine.")

!ls
output: colabData/ something.py

%run something.py
output: everything's fine.
b.g.
  • 847
  • 8
  • 14
  • What about drive notepad, does it work? It gives me an error everytime I try to access a file using it with "Open With". – aspiring1 May 14 '19 at 11:23
15

you can edit it like this:

  1. click on the triple bar (≡ on the left side of your windows)
  2. click on files (the folder icon on the left)
  3. click on Mount Drive and mount your drive
  4. find your .py file and double click on it
  5. edit it
  6. press ctrl+s to save

enter image description here

enter image description here

edit:
these steps were after cloning your code into your drive
you should first mount your drive and clone your repo into your drive

hossein hayati
  • 1,088
  • 2
  • 15
  • 34
4

Not a perfect solution but can be useful for someone.

You can use !cat file_name.py to access file_name.py contents, copy the contents in the next cell and now you can run it or edit it.

Krishna
  • 6,107
  • 2
  • 40
  • 43
4

I found it easier to edit the file locally.

  1. You can download it from the left panel.
  2. Right click on any file and download it.
  3. Next, edit the file.
  4. Next, upload the file.
  5. use mv to move the file to the right location.

enter image description here

Netro
  • 7,119
  • 6
  • 40
  • 58
3

Solution:

p = """
Yadda yadda
whatever you want just don't use triple quotes.
"""

c = """text_file = open("text.text", "w+");text_file.write(p);text_file.close()""" 

exec(c)
maxymoo
  • 35,286
  • 11
  • 92
  • 119
2

Easiest solution just double click on the file you want to edit.The file opens and edit the file,save it and that's that.You are done

Sihat Afnan
  • 742
  • 7
  • 14
1

There is an app called Python Compiler Editor that you can connect to your Google Drive account, edit files and save them back.

Marafon Thiago
  • 337
  • 2
  • 6
1

The easiest way is:

1- Go to where you want the file to be with:

%cd WhereYouWantItToBe

2- Then write using:

%%writefile NameOfFile.txt
Hey there here is the start of the text
and also here
here is the end

3- Now run this cell and the file is going to be saved where you decided in step one.

Sam
  • 135
  • 2
  • 9
0

While I don't have a way of editing in the notebook, I will share my pipeline. Quite obvious really:

  • fork the repo or create a new one(for a new project)
  • create a branch just for uploading
  • make changes and push
  • evaluate
  • make changes

Hope that helps.

aneesh joshi
  • 553
  • 5
  • 11
0

With the addition of the terminal (the icon is in the lower left corner), now we can edit files through vim.

Vim

andandandand
  • 21,946
  • 60
  • 170
  • 271
0

You can open the file using the file explorer programatically, like this:

from google.colab import files
files.view('your_file.py')

It will open your file in a separate panel and you can then edit and save it there directly.

jerpint
  • 399
  • 2
  • 16
0

Solution: Use the file management tool to modify and save the files like this:

from google.colab import files
files.view('your_file.py')

Then a new panel will appear on the right side so that to you can modify and save it.

KingLiu
  • 59
  • 4