"保存和检查点"在Google DFS文件夹中运行jupyter笔记本时

时间:2018-05-20 21:06:19

标签: python google-drive-api jupyter-notebook jupyter

我一直在我的Google云端硬盘中使用Jupyter Notebook,它运行得非常好。最近我被迫切换到较新版本Drive File Stream。我创建了文件夹Available offline,这意味着我将文件夹保存在本地存储空间中。当我启动Jupyter Notebook时,它运行正常,但尝试Save and Checkpoint会给我一个错误Checkpoint failed。似乎它以某种方式正确保存文件但返回错误。

我在Jupyter Notebook中运行Anaconda Prompt启动了jupyter笔记本。当我尝试Save and Checkpoint时,会收到以下错误。似乎它抱怨保存的文件是相同的 - shutil.SameFileError这真的很奇怪,因为我确实通过额外的Save Attempt x更改笔记本来帮助我告诉它是否已保存。我还尝试删除文件夹.ipynb_checkpoints中的相应检查点并执行Save and Checkpoint,但我仍然遇到相同的错误...

我向Jupyter Notebook github page提交了一张发行票,似乎其他几个人也遇到了同样的问题,但没有收到任何帮助。

以下是我的日志:

[I 10:49:44.573 NotebookApp] Saving file at /Writing_MVUE_2d.ipynb [E 10:49:44.690 NotebookApp] Unhandled error in API request Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\base\handlers.py", line 516, in wrapper result = yield gen.maybe_future(method(self, *args, **kwargs)) File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\gen.py", line 1015, in run value = future.result() File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\concurrent.py", line 237, in result raise_exc_info(self._exc_info) File "<string>", line 3, in raise_exc_info File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\gen.py", line 285, in wrapper yielded = next(result) File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\handlers.py", line 278, in post checkpoint = yield gen.maybe_future(cm.create_checkpoint(path)) File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\manager.py", line 468, in create_checkpoint return self.checkpoints.create_checkpoint(self, path) File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\filecheckpoints.py", line 56, in create_checkpoint self._copy(src_path, dest_path) File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\fileio.py", line 241, in _copy copy2_safe(src, dest, log=self.log) File "C:\ProgramData\Anaconda3\lib\site-packages\notebook\services\contents\fileio.py", line 51, in copy2_safe shutil.copyfile(src, dst) File "C:\ProgramData\Anaconda3\lib\shutil.py", line 98, in copyfile raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) shutil.SameFileError: 'G:\\My Drive\\Research\\Camera_Project\\Writing_MVUE_2d.ipynb' and 'G:\\My Drive\\Research\\Camera_Project\\.ipynb_checkpoints\\Writing_MVUE_2d-checkpoint.ipynb' are the same file [E 10:49:44.698 NotebookApp] { "Cache-Control": "no-cache", "Cookie": "username-localhost-8891=\"2|1:0|10:1525749572|23:username-localhost-8891|44:ZGE5NjFkMTgwOTI3NDFjMWJhMzY1NDIwNjhlODk5ODA=|52cc6ad64c261ccd78d2f66a0c14a95edc8468e7e4ac9c27770ce2bbebd8cd55\"; _xsrf=2|f34c78bf|7c7913196386a495e7fb6a7c3c7bbf7e|1525206477; username-localhost-8890=\"2|1:0|10:1525723124|23:username-localhost-8890|44:MGFhNmE2ZjMxZmFmNDU0M2IyMTgzNWE2NDdmMmRhMDc=|73c556d569c5f55b60a18dd36f06850f90c89c2dc329e11a4041a6c5ede7cb38\"; username-localhost-8889=\"2|1:0|10:1526327000|23:username-localhost-8889|44:YjdmMTExODQyNjNmNGM5MWIzZmM0OWIzYjE3NDgzZmM=|43a55e40b88869d8e054546feca0fbf7efae9c993b450311f5046f4c0ba45a66\"; username-localhost-8888=\"2|1:0|10:1526395222|23:username-localhost-8888|44:ZDJmZWNjZWUzZDJjNDVmMjkwMTZhNWZmNTI1MjhhMTE=|9cd671557f9b67b203088e3852aaf37de56ad7c8ad45f26798bc3e0d47a49c43\"", "Referer": "http://localhost:8888/notebooks/Writing_MVUE_2d.ipynb", "X-Xsrftoken": "2|f34c78bf|7c7913196386a495e7fb6a7c3c7bbf7e|1525206477", "Origin": "http://localhost:8888", "Content-Length": "0", "Connection": "Keep-Alive", "Accept-Encoding": "gzip, deflate", "Accept": "application/json, text/javascript, */*; q=0.01", "Accept-Language": "en-US", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299", "X-Requested-With": "XMLHttpRequest", "Host": "localhost:8888" } [E 10:49:44.698 NotebookApp] 500 POST /api/contents/Writing_MVUE_2d.ipynb/checkpoints (::1) 12.00ms referer=http://localhost:8888/notebooks/Writing_MVUE_2d.ipynb

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

deniz195的一些出色调试导致了一个不错的答案!这个临时解决方案适用于我和其他许多github朋友!

在这里引用他的原始答案:

  

我调试了一下问题,找到了以下解决方案:

     

第1部分-最小的工作示例   该问题似乎源于shutil.copyfile,该文件检查源文件和目标文件是否相同(使用os.path.samefile)。

     

似乎Windows和Google Drive文件流的组合产生了无效的结果。这是最小的示例(假设使用python 3.6.2,存在G:\ My Drive \ foo.txt):

>>> f1 = 'G:\\My Drive\\foo.txt'
>>> f2 = 'G:\\My Drive\\foo2.txt'
>>> import shutil
>>> shutil.copyfile(f1, f2)
>>> shutil.copyfile(f1, f2)
     

->尽管显然不应该在最后一行抛出SameFileError!

     

位置:

>>> f1 = 'G:\\My Drive\\foo.txt'
>>> f3 = 'C:\\Scratch\\foo2.txt'
>>> import shutil
>>> shutil.copyfile(f1, f3)
>>> shutil.copyfile(f1, f3)
     

->不抛出错误(正确)!

     

第2部分-为什么?   这是怎么发生的?

     

事实证明,shutil.copyfile使用os.path.samefile来确定是否在其自身上复制文件:   (来自https://github.com/python/cpython/blob/master/Lib/genericpath.py

# Are two filenames really pointing to the same file?
def samefile(f1, f2):
    """Test whether two pathnames reference the same actual file"""
    s1 = os.stat(f1)
    s2 = os.stat(f2)
    return samestat(s1, s2)

# Are two stat buffers (obtained from stat, fstat or lstat)
# describing the same file?
def samestat(s1, s2):
    """Test whether two stat buffers reference the same file"""
    return (s1.st_ino == s2.st_ino and
            s1.st_dev == s2.st_dev)
     

不幸的是,现在st_ino取决于文件系统(Meaning of ST_INO (os.stat() output) in Windows OS)上的Windows,对于Google云端硬盘文件流中的所有文件来说,st_ino == 0。

     

第3部分-到哪里去指责谁?   看来此错误是一个懒惰的文件系统(为什么不将某种uid报告为inode的运气)和一个朴素的os python库的不幸组合(以这种方式检查文件身份似乎不能一概而论……为什么不检查)如果st_ino == 0?)

     

第4部分-肮脏的修补程序

     
      
  1. 找到您的python库的genericpath.py:
  2.   
>>> import os
>>> os.path.genericpath.__file__
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Anaconda3_64\\lib\\genericpath.py'
     
      
  1. 在此文件中,用以下补丁替换samestat函数:
  2.   
# Are two stat buffers (obtained from stat, fstat or lstat)
# describing the same file?
def samestat(s1, s2):
    """Test whether two stat buffers reference the same file"""
    return (s1.st_ino != 0 and
          s2.st_ino != 0 and
          s1.st_ino == s2.st_ino and
            s1.st_dev == s2.st_dev)
     
      
  1. 保存文件。重新启动python(和/或jupyter)。
  2.   
  3. 感到高兴,等到google或python正确解决了这个问题...
  4.