0

我有 2 个用于 GCS(谷歌云存储)的脚本来上传和下载文件。我使用pip install google. 我的脚本正在运行,但现在我看到错误: ModuleNotFoundError: No module named 'google'

我在这里阅读了很多关于同一问题的问题,但没有找到解决方案。

我尝试了什么:

  • 升级点

  • pip uninstall googlepip3 install google

  • 重新安装 VS 代码

  • 我试过以下:

    pip install virtualenv virtualenv venv source venv/bin/activate pip install google-cloud-storage

这里source venv/bin/activate没有在终端中运行

你能帮我么?

当我这样做时,pip list我看到:

    Package                  Version
------------------------ ---------
beautifulsoup4           4.10.0   
cachetools               4.2.4    
certifi                  2021.10.8
charset-normalizer       2.0.9
distlib                  0.3.4
filelock                 3.4.2
google                   3.0.0
google-api-core          2.3.2
google-api-python-client 2.34.0
google-auth              2.3.3
google-auth-httplib2     0.1.0
google-cloud             0.34.0
google-cloud-core        2.2.1
google-cloud-storage     1.43.0
google-crc32c            1.3.0
google-resumable-media   2.1.0
googleapis-common-protos 1.54.0
httplib2                 0.20.2
idna                     3.3
pip                      21.3.1
platformdirs             2.4.1
protobuf                 3.19.1
pyasn1                   0.4.8
pyasn1-modules           0.2.8
pyparsing                3.0.6
requests                 2.26.0
rsa                      4.8
six                      1.16.0
soupsieve                2.3.1
uritemplate              4.1.1
urllib3                  1.26.7
virtualenv               20.13.0

人们问我的代码。这里是。(虽然不需要解决问题)我运行的代码:

注意:(它上传到GCS。文件的名称如“客户- ...”,“帐户- ...”。文件的类型被上传到相应的命名存储桶中的前缀[bucket_name/Prefix_name]我称之为file_type_name在代码中)

from google.cloud import storage
import os
import glob
import json
import sys

def upload_files(config_file):
    # Reading 3 Parameters for upload from JSON file
    with open(config_file, "r") as file:
        contents = json.loads(file.read())

    # Setting up login credentials
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = contents['login_credentials']
    # The ID of GCS bucket
    bucket_name = contents['bucket_name']
    # Setting path to files
    LOCAL_PATH = contents['folder_from']

    for source_file_name in glob.glob(LOCAL_PATH + '/**'):

    # For multiple files upload
    # Setting destination folder according to file name 
        if os.path.isfile(source_file_name):
            partitioned_file_name = os.path.split(source_file_name)[-1].partition("-")
            file_type_name = partitioned_file_name[0]

            # Setting folder where files will be uploaded
            destination_blob_name = file_type_name + "/" + os.path.split(source_file_name)[-1]

            # Setting up required variables for GCS 
            storage_client = storage.Client()
            bucket = storage_client.bucket(bucket_name)
            blob = bucket.blob(destination_blob_name)

            # Running upload and printing confirmation message
            blob.upload_from_filename(source_file_name)
            print("File from {} uploaded to {} in bucket {}.".format(
                source_file_name, destination_blob_name, bucket_name
            ))


upload_files(sys.argv[1])
4

1 回答 1

0

我的朋友帮我解决了这个问题。我正在使用 Visual Studio Code,我不得不更改 Python 解释器。Ctrl++ShiftP输入“解释器”。从我选择的列表中Python 3.9.9 64-bit (windows store)。希望它对某人有帮助

于 2022-01-10T17:38:12.383 回答