我正在尝试运行一个python脚本,该脚本模拟交通传感器将数据实时发送到我的Google Cloud Shell上的PubSub.我收到这个错误Traceback (most recent call last):File ./send_sensor_data.py, line 87, in modulepscl...

我正在尝试运行一个python脚本,该脚本模拟交通传感器将数据实时发送到我的Google Cloud Shell上的PubSub.我收到这个错误
Traceback (most recent call last):
File "./send_sensor_data.py", line 87, in <module>
psclient = pubsub.Client()
AttributeError: 'module' object has no attribute 'Client'
尝试运行google.cloud.pubsub .__ file__,没有重复项.
我到处都在搜索,流行的共识是将pubsub软件包安装到一个虚拟环境中,但我一直没有用.
到目前为止,我已经尝试过:
>将虚拟机设置为干净状态
>卸载并重新安装所有gcloud组件
>将所有gcloud组件更新为最新版本
>未安装并重新安装的python pubsub库
>在virtualenv内安装pubsub
>尝试过其他项目
>从其他GCP帐户尝试
这是我的脚本:
import time
import gzip
import logging
import argparse
import datetime
from google.cloud import pubsub
TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
TOPIC = 'sandiego'
INPUT = 'sensor_obs2008.csv.gz'
def publish(topic, events):
numobs = len(events)
if numobs > 0:
with topic.batch() as batch:
logging.info('Publishing {} events from {}'.
format(numobs, get_timestamp(events[0])))
for event_data in events:
batch.publish(event_data)
def get_timestamp(line):
# look at first field of row
timestamp = line.split(',')[0]
return datetime.datetime.strptime(timestamp, TIME_FORMAT)
def simulate(topic, ifp, firstObsTime, programStart, speedFactor):
# sleep computation
def compute_sleep_secs(obs_time):
time_elapsed = (datetime.datetime.utcnow() - programStart).seconds
sim_time_elapsed = (obs_time - firstObsTime).seconds / speedFactor
to_sleep_secs = sim_time_elapsed - time_elapsed
return to_sleep_secs
topublish = list()
for line in ifp:
event_data = line # entire line of input CSV is the message
obs_time = get_timestamp(line) # from first column
# how much time should we sleep?
if compute_sleep_secs(obs_time) > 1:
# notify the accumulated topublish
publish(topic, topublish) # notify accumulated messages
topublish = list() # empty out list
# recompute sleep, since notification takes a while
to_sleep_secs = compute_sleep_secs(obs_time)
if to_sleep_secs > 0:
logging.info('Sleeping {} seconds'.format(to_sleep_secs))
time.sleep(to_sleep_secs)
topublish.append(event_data)
# left-over records; notify again
publish(topic, topublish)
def peek_timestamp(ifp):
# peek ahead to next line, get timestamp and go back
pos = ifp.tell()
line = ifp.readline()
ifp.seek(pos)
return get_timestamp(line)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Send sensor data to Cloud Pub/Sub in small groups, simulating real-time behavior')
parser.add_argument('--speedFactor', help='Example: 60 implies 1 hour of data sent to Cloud Pub/Sub in 1 minute', required=True, type=float)
args = parser.parse_args()
# create Pub/Sub notification topic
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
psclient = pubsub.Client()
topic = psclient.topic(TOPIC)
if not topic.exists():
logging.info('Creating pub/sub topic {}'.format(TOPIC))
topic.create()
else:
logging.info('Reusing pub/sub topic {}'.format(TOPIC))
# notify about each line in the input file
programStartTime = datetime.datetime.utcnow()
with gzip.open(INPUT, 'rb') as ifp:
header = ifp.readline() # skip header
firstObsTime = peek_timestamp(ifp)
logging.info('Sending sensor data from {}'.format(firstObsTime))
simulate(topic, ifp, firstObsTime, programStartTime, args.speedFactor)
解决方法:
pubsub.Client类存在,直到pubsub python软件包的0.27.0版本.因此,我刚刚创建了一个虚拟环境,并在其中安装了0.27.0版本的pubsub.
以下是命令:
virtualenv venv
source venv/bin/activate
pip install google-cloud-pubsub==0.27.0
本文标题为:如何修复AttributeError:在Google Cloud Interactive Shell中运行python时,“模块”对象没有属性“客户端”


基础教程推荐
- Python爬虫爬取属于自己的地铁线路图 2023-08-05
- Pygame实现小球躲避实例代码 2023-08-05
- Numpy安装、升级与卸载的详细图文教程 2022-10-20
- C++开发python windows版本的扩展模块示例 2023-09-03
- Python轻松破解加密压缩包教程详解 2023-08-05
- CentOS7 安装 Python3.6 2023-09-04
- python中的GUI实现计算器 2023-08-04
- pandas学习之df.fillna的具体使用 2022-08-30
- pytorch和tensorflow计算Flops和params的详细过程 2022-08-30
- Linux下Python2升级Python3 2023-09-03