How to mount google drive when using IJulia in google colab?

假装没事ソ 提交于 2021-02-10 04:54:45

问题


I am trying to get Julia working in colab. I want to mount gdrive to colab just like in IPython when using IJulia. In ipython I can do it as follows:

from google.colab import drive
drive.mount('/content/gdrive')

I tried following:

using PyCall
clb = pyimport("google")
clb.colab.drive.mount("/content/gdrive")
------------------------------------------------------------------------------------
Warning: Password input may be echoed.
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=...

Enter your authorization code:

PyError ($(Expr(:escape, :(ccall(#= /root/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'EOFError'>
EOFError()
  File "/usr/local/lib/python3.6/dist-packages/google/colab/drive.py", line 234, in mount
    fifo_file.write(_getpass.getpass(auth_prompt) + '\n')
  File "/usr/lib/python3.6/getpass.py", line 91, in unix_getpass
    passwd = fallback_getpass(prompt, stream)
  File "/usr/lib/python3.6/getpass.py", line 126, in fallback_getpass
    return _raw_input(prompt, stream)
  File "/usr/lib/python3.6/getpass.py", line 148, in _raw_input
    raise EOFError


Stacktrace:

 [1] pyerr_check at /root/.julia/packages/PyCall/ttONZ/src/exception.jl:60 [inlined]

 [2] pyerr_check at /root/.julia/packages/PyCall/ttONZ/src/exception.jl:64 [inlined]

 [3] macro expansion at /root/.julia/packages/PyCall/ttONZ/src/exception.jl:84 [inlined]

 [4] __pycall!(::PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyObject, ::Ptr{Nothing}) at /root/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44

 [5] _pycall!(::PyObject, ::PyObject, ::Tuple{String}, ::Int64, ::Ptr{Nothing}) at /root/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:29

 [6] #call#111 at /root/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:11 [inlined]

 [7] (::PyObject)(::String) at /root/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:89

 [8] top-level scope at In[9]:3
using PyCall
py"""
from google.colab import drive
drive.mount('/content/gdrive')
"""
cd("/content/gdrive/My\ Drive")
------------------------------------------------------------------------------------
Warning: Password input may be echoed.
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=...

Enter your authorization code:

PyError ($(Expr(:escape, :(ccall(#= /root/.julia/packages/PyCall/ttONZ/src/pyeval.jl:39 =# @pysym(:PyEval_EvalCode), PyPtr, (PyPtr, PyPtr, PyPtr), o, globals, locals))))) <class 'EOFError'>
EOFError()
  File "/root/.julia/packages/PyCall/ttONZ/src/pyeval.jl", line 2, in <module>
    const Py_file_input = 257
  File "/usr/local/lib/python3.6/dist-packages/google/colab/drive.py", line 234, in mount
    fifo_file.write(_getpass.getpass(auth_prompt) + '\n')
  File "/usr/lib/python3.6/getpass.py", line 91, in unix_getpass
    passwd = fallback_getpass(prompt, stream)
  File "/usr/lib/python3.6/getpass.py", line 126, in fallback_getpass
    return _raw_input(prompt, stream)
  File "/usr/lib/python3.6/getpass.py", line 148, in _raw_input
    raise EOFError


Stacktrace:

 [1] pyerr_check at /root/.julia/packages/PyCall/ttONZ/src/exception.jl:60 [inlined]

 [2] pyerr_check at /root/.julia/packages/PyCall/ttONZ/src/exception.jl:64 [inlined]

 [3] macro expansion at /root/.julia/packages/PyCall/ttONZ/src/exception.jl:84 [inlined]

 [4] pyeval_(::String, ::PyDict{String,PyObject,true}, ::PyDict{String,PyObject,true}, ::Int64, ::String) at /root/.julia/packages/PyCall/ttONZ/src/pyeval.jl:39

 [5] top-level scope at /root/.julia/packages/PyCall/ttONZ/src/pyeval.jl:232

 [6] top-level scope at In[10]:2


Both did not work.


回答1:


One way to achieve this is to mount google drive before installing Julia, when default Runtime type is Python by running:

from google.colab import drive
drive.mount('/content/gdrive')

That way, even after Runtime type is changed to Julia, google drive remains mounted until you change Hardware acceleration or session is lost.



来源:https://stackoverflow.com/questions/59717683/how-to-mount-google-drive-when-using-ijulia-in-google-colab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!