将本地文件导入google colab

时间:2018-03-21 23:11:50

标签: google-colaboratory

我不明白colab如何使用目录,我创建了一个笔记本,并且colab将它放在/ Google Drive / Colab笔记本中。

现在我需要导入一个文件(data.py),其中我有一堆我需要的功能。 Intuition告诉我将文件放在同一目录中并使用以下命令导入:

导入数据

但显然那不是......

我也尝试将目录添加到路径集中,但我正在错误地指定目录..

任何人都可以帮忙吗?

提前致谢!

6 个答案:

答案 0 :(得分:9)

Colab笔记本存储在Google云端硬盘上。但它在另一台虚拟机上运行。因此,您还需要复制data.py。这样做是为了通过Colab上传data.py。

from google.colab import files
files.upload()
# choose the file on your computer to upload it then
import data

答案 1 :(得分:6)

要轻松上传本地文件,您可以使用Google Colab的新功能:

  • 点击屏幕左侧的向右箭头(在Google下方 Colab徽标)enter image description here
  • 选择“文件”标签
  • 点击“上传”按钮

它将打开一个弹出窗口,以选择要从本地文件系统上载的文件。

答案 2 :(得分:3)

要将本地文件从系统上载到collab存储/目录。

ImportError                               Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     13         try:
---> 14             return importlib.import_module(mname)
     15         except ImportError:

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\__init__.py in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap.py in _gcd_import(name, package, level)

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap.py in _find_and_load(name, import_)

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap.py in _find_and_load_unlocked(name, import_)

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap.py in _load_unlocked(spec)

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap.py in module_from_spec(spec)

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap_external.py in create_module(self, spec)

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>()
     57 
---> 58   from tensorflow.python.pywrap_tensorflow_internal import *
     59   from tensorflow.python.pywrap_tensorflow_internal import __version__

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in <module>()
     16             return importlib.import_module('_pywrap_tensorflow_internal')
---> 17     _pywrap_tensorflow_internal = swig_import_helper()
     18     del swig_import_helper

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     15         except ImportError:
---> 16             return importlib.import_module('_pywrap_tensorflow_internal')
     17     _pywrap_tensorflow_internal = swig_import_helper()

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\__init__.py in import_module(name, package)
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 

ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-1-2453dc764bc7> in <module>()
----> 1 import tensorflow as tf
      2 x = tf.random_normal([10, 10])
      3 y = tf.random_normal([10, 10])
      4 z = tf.matmul(x, y)
      5 

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\__init__.py in <module>()
     20 
     21 # pylint: disable=g-bad-import-order
---> 22 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
     23 from . import app
     24 from . import bitwise

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\__init__.py in <module>()
     47 import numpy as np
     48 
---> 49 from tensorflow.python import pywrap_tensorflow
     50 
     51 # Protocol buffers

~\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>()
     72 for some common reasons and solutions.  Include the entire stack trace
     73 above this error message when asking for help.""" % traceback.format_exc()
---> 74   raise ImportError(msg)
     75 
     76 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
  File "C:\Users\Pwego\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\Pwego\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Pwego\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Pwego\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Pwego\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\Pwego\AppData\Local\Continuum\anaconda3\envs\tfp3.6\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

enter image description here

答案 3 :(得分:1)

所以,这就是我最终解决此问题的方法。但是,我必须指出,就我而言,我必须使用不断变化的几个文件和专有模块。

我发现最好的解决方案是使用FUSE包装器将colab“链接”到我的Google帐户。我使用了此特定工具:

https://github.com/astrada/google-drive-ocamlfuse

有一个如何在此处设置环境的示例,但这是我的操作方法:

df.loc[45]

这时,您已经安装了包装器,上面的代码将生成几个链接,供您授权访问您的Google云端硬盘帐户。

您必须在colab文件系统中创建一个文件夹(据我所知,请记住这不是永久的...),然后将驱动器安装在该文件夹中:

# Install a Drive FUSE wrapper.
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse


# Generate auth tokens for Colab
from google.colab import auth
auth.authenticate_user()
# Generate creds for the Drive FUSE library.
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

!ls 命令将打印目录内容,以便您可以检查它的工作原理,仅此而已。现在,您已经拥有了所需的所有文件,并且可以对其进行更改,而不会造成任何进一步的麻烦。请记住,您可能需要重新启动内核以更新导入和变量。

希望这对某人有用!

答案 4 :(得分:1)

现在google正式为轻松访问和使用Gdrive提供支持。

您可以使用以下代码将驱动器安装到Colab:

from google.colab import drive
drive.mount('/gdrive')
%cd /gdrive/My\ Drive/{location you want to move}

答案 5 :(得分:0)

您可以在colab中编写以下命令来安装驱动器

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

,您可以通过类似这样的简单linux命令wget从某些外部URL下载到驱动器中

!wget 'https://dataverse.harvard.edu/dataset'