我正在mi gcloud存储桶中设置一项新功能,该功能允许我使用名为“ boto”的python库上载或下载文件,但出现此错误
我正在使用Linux,Visual Studio代码,python 3.7,gsutil和boto的最新版本。
import os
import boto
import gcs_oauth2_boto_plugin
import shutil
import io
import tempfile
import time
import sys
# Activate virtual environment
activate_this = os.path.join(VENV + 'bin/activate_this.py')
exec(open(activate_this, dict(__file__=activate_this)))
# Check arguments
if len(sys.argv) < 2:
print ("Usage: " + sys.argv[0] + ' FILENAME')
quit()
filename = sys.argv[1]
# URI scheme for Cloud Storage.
GOOGLE_STORAGE = "gs"
# URI scheme for accessing local files.
LOCAL_FILE = "file"
header_values = {"x-goog-project-id": PROJECT_ID}
# Open local file
with open(filename, 'r') as localfile:
dst_uri = boto.storage_uri(BUCKET + '/' + filename, GOOGLE_STORAGE)
# The key-related functions are a consequence of boto's
# interoperability with Amazon S3 (which employs the
# concept of a key mapping to localfile).
dst_uri.new_key().set_contents_from_file(localfile)
print ('Successfully created "%s/%s"' % (dst_uri.bucket_name, dst_uri.object_name))
回溯(最近通话最近): 文件“ ./upload2gcs.py”,第10行,在 导入博托 ImportError:没有名为boto的模块
答案 0 :(得分:0)
可能从Python查找要导入的模块的任何路径中找不到包含boto模块的目录。
在脚本中,检查sys.path
列表,查看是否存在预期的目录:
import pprint
import sys
pprint.pprint(sys.path)
例如,gsutil与自己的Boto分支打包在一起;它会在运行时执行一些其他步骤,以确保Boto模块的父目录已添加到sys.path
,这使后续的import boto
语句可以正常工作:
https://github.com/GoogleCloudPlatform/gsutil/blob/c74a5964980b4f49ab2c4cb4d5139b35fbafe8ac/gslib/init.py#L102