admin管理员组文章数量:1303389
I am using Google Colab for the first time, and I am having trouble importing one Colab notebook into another. I have created two new notebooks in my Colab Notebooks folder titled NB1.ipynb
and NB2.ipynb
. I wish to import the latter from the former, i.e. I want to access all of the functions and variables in NB2.ipynb from NB1.ipynb. I have tried the following code as suggested by Gemini:
import nbimporter
import sys
sys.path.append('/content/drive/MyDrive/Colab Notebooks')
from NB2 import *
This produced the following error message:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-15-a10fc15ee9a7> in <cell line: 0>()
1 # Import the desired notebook as a module
----> 2 from NB2 import *
3
4 # Now you can access variables, functions, and classes defined in NB2
ModuleNotFoundError: No module named 'NB2'
I'm a bit confused as to why Colab can't find NB2.ipynb, as both files are in the same folder. What's the correct way to import NB2.ipynb?
I am using Google Colab for the first time, and I am having trouble importing one Colab notebook into another. I have created two new notebooks in my Colab Notebooks folder titled NB1.ipynb
and NB2.ipynb
. I wish to import the latter from the former, i.e. I want to access all of the functions and variables in NB2.ipynb from NB1.ipynb. I have tried the following code as suggested by Gemini:
import nbimporter
import sys
sys.path.append('/content/drive/MyDrive/Colab Notebooks')
from NB2 import *
This produced the following error message:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-15-a10fc15ee9a7> in <cell line: 0>()
1 # Import the desired notebook as a module
----> 2 from NB2 import *
3
4 # Now you can access variables, functions, and classes defined in NB2
ModuleNotFoundError: No module named 'NB2'
I'm a bit confused as to why Colab can't find NB2.ipynb, as both files are in the same folder. What's the correct way to import NB2.ipynb?
Share Improve this question asked Feb 10 at 18:24 LeonidasLeonidas 7451 gold badge10 silver badges27 bronze badges 11 | Show 6 more comments3 Answers
Reset to default 0You can convert a Notebook file into Python using nbconvert
, example here Colab provides this option File
> Download
> Download .py
and import it from there.
First you need to be sure you have mounted google drive. next install ipynb:
%pip install ipynb
Then change working path to where notebook are like this:
import os
os.chdir("/content/drive/MyDrive/")
After that try to import the notebook as usual:
import ipynb.fs.full.NB1 as NB1R
If you encounter any error like ImportError: Could not import ... incorrect version or language Then you have to do following step:
First download notebook from File > Download > Download.ipynb
Then Open downloaded notebook with any good text editor like notepad++ or similar app and
find the following word kernelspec
it should be looks like this:
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
Then add line "language": "python"
it should be looking like this:
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
}
Then save and upload the notebook to google drive. Open the notebook again and try to import it. This time it should work.
You don't have to do it every time you made any change in notebook. Only first time necessary
Use nbimporter to import .ipynb files directly.
Ensure the notebook path is correct and the notebook contains only executable Python code.
Alternatively, convert the notebook to a .py file and import it as a regular module.
本文标签: pythonImporting one Google Colab notebook into anotherStack Overflow
版权声明:本文标题:python - Importing one Google Colab notebook into another - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741698455a2393146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
os.chdir()
in place ofsys.path.append()
. Goole colab seems doesn't work withsys.path.append
. – CB Acnt Commented Feb 10 at 20:17import os
os.getcwd()
– CB Acnt Commented Feb 10 at 20:27sys.path.append()
toos.chdir()
, I got the following error:FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Colab Notebooks'
. Both NB1.ipynb and NB2.ipynb are saved in My Drive/Colab Notebooks. – Leonidas Commented Feb 10 at 20:30os.getcwd()
and it returned "/content". – Leonidas Commented Feb 10 at 20:31FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Colab Notebooks'
means it cant access'/content/drive/MyDrive/Colab Notebooks'
. Can you check if'/content/drive/MyDrive/Colab Notebooks'
acesseble fom colab notebook? – CB Acnt Commented Feb 10 at 20:36