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?