最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Importing one Google Colab notebook into another - Stack Overflow

programmeradmin2浏览0评论

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
  • You need to use os.chdir() in place of sys.path.append(). Goole colab seems doesn't work with sys.path.append. – CB Acnt Commented Feb 10 at 20:17
  • You can check working path with import os os.getcwd() – CB Acnt Commented Feb 10 at 20:27
  • After changing sys.path.append() to os.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:30
  • I ran os.getcwd() and it returned "/content". – Leonidas Commented Feb 10 at 20:31
  • FileNotFoundError: [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
 |  Show 6 more comments

3 Answers 3

Reset to default 0

You 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.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>