0

What I'm trying to do is import functions and variables from a .ipynb file called A into B.ipynb, then import functions and variables from B to C.ipynb. The files are in Google Colab.

The way I've done this is to download A as a .py file, and then use the below code in B to seemingly access A.py:

!cp /content/drive/MyDrive/Colab_Notebooks/buildmergegraph8.py /content

followed by:

from buildmergegraph8 import *

This runs fine, giving me access to functions in A when called in B. Next is the problem, when I try to import from B into C using the following lines in C.ipynb:

!cp /content/drive/MyDrive/Colab_Notebooks/training_and_test_merge_graphs_with_labels.py /content
from training_and_test_merge_graphs_with_labels import *

This returns the error below. For clarity, the syntax error up arrow is pointing to the ! in !cp:

Traceback (most recent call last):

  File "/usr/local/lib/python3.10/dist-packages/IPython/core/interactiveshell.py", line 3553, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-3-5391baf2e3b0>", line 1, in <cell line: 1>
    from training_and_test_merge_graphs_with_labels import *

  File "/content/training_and_test_merge_graphs_with_labels.py", line 25
    !cp /content/drive/MyDrive/Colab_Notebooks/buildmergegraph8.py /content
    ^
SyntaxError: invalid syntax`

I notice in the last line of this error that the problem is really the call in the .py file training_and_test_merge_graphs_with_labels.py, which is a .py version of the training_and_test_merge_graphs_with_labels.ipynb. I think the problem is the training_and_test_merge_graphs_with_labels.py file is a download of a .ipynb file, and that .ipynb file had the first two lines of written code in my question above. Basically, you can't execute !cp in a .py file.

Am I correct in my last sentence? If so, how can I import functions and variables from A to B, and then from B to C, if they are all .ipynb?

I was expecting to be able to import functions and variables from A.ipynb to B.ipynb, and from B.ipynb to C.ipynb, but it's possible I've chosen a method that doesn't allow this or there is a better alternative that does allow this.

Any help would be great, especially a solution that avoids me needing to make .py files of my .ipynb files.

Edit: I have since found that the error is a Google Colab issue: GC doesn't have access to local files, or something like this. I think this question could be closed?

  • `!cp` is a Jupyter Notebook directive, it's not Python syntax. It shouldn't be in scripts. – Barmar Jun 28 '23 at 19:45
  • Thanks, I will look into Python-syntax coherent imports next then. If I want to import functions from A to B to C, all being .ipynb, is there a succinct way to do this, possibly without creating python scripts? – user21764386 Jun 28 '23 at 19:54
  • I have tried 'import import-ipynb' in B.ipynb coupled with 'from A.ipynb import *' but I am told there is no module A. This is confusing because B and A are in the same directory, so it should have worked. Still searching for suggestions. – user21764386 Jun 28 '23 at 21:17

1 Answers1

0

I cannot comment but I would have asked what do you mean by "download A as a .py file".

There are 2 possible solutions that I believe might be suitable.

First solution (simple and my preferred solution)

Save all the relevant variables and functions in some .py file (e.g. foo.py) and save that file in the same directory as A.ipynb, B.ipynb, and C.ipynb. Example directory structure:

dir/
    A.ipynb
    B.ipynb
    C.ipynb
    foo.py

Then within your notebooks you can just import the same functions using

from foo import some_function

Second solution

Use the ipynb library to import the relevant functions and other code directly from the notebooks. Download it using

pip install ipynb

Then, import the functions, classes, and import statements from A.ipynb into B.ipynb and C.ipynb using

# for specific imports
import ipynb.fs
from .defs.A import some_function

Or

# For making all functions, classes, and import statements available in the new notebooks
import ipynb.fs.defs.A

I will assume that A.ipynb, B.ipynb, and C.ipynb are in the same directory.

Here is some documentation for ipynb: docs

Here is a similar problem: import a function from another .ipynb file

BlockFace
  • 17
  • 7
  • Thanks Blockface, I've considered using Method 1 only I'd need to go back and edit my .py module as I go. Method 2 looked very good, but for some reason it says 'No module named ipynb.fs.defs.A'. I definitely have the .ipynb files in the same directory, so I'm unsure what the problem is. – user21764386 Jun 29 '23 at 11:22