Python3环境下ModuleNotFoundError: No module named 'urlparse'问题求助
问题背景
原代码实现
import json import csv import googleanalytics import google.analytics.data import urllib.parse # Load the JSON key file with open('key.json', 'r') as f: credentials = json.load(f) # Create a service account object service_account = googleanalytics.create_service(credentials) credentials_dict = { "options": { "client_email": service_account, "private_key": 'private_key' } } client = google.analytics.data.BetaAnalyticsDataClient(options=credentials_dict) # Set the view ID view_id = '74466483' # Set the date range start_date = '2023-01-01' end_date = '2023-05-01' # Define the metrics and dimensions metrics = ['ga:sessions', 'ga:bounce_rate', 'ga:average_time_on_page', 'ga:entrances', 'ga:pageviews', 'ga:unique_pageviews'] dimensions = ['ga:source', 'ga:page'] # Run the report report = client.reports().generate( view_id, start_date=start_date, end_date=end_date, metrics=metrics, dimensions=dimensions ).execute() # Write the report to a CSV file with open('report.csv', 'w') as f: writer = csv.writer(f) writer.writerow(report.header) for row in report.rows: writer.writerow(row)
报错信息
Traceback (most recent call last): File "C:\Users\MYNAME\Desktop\CompanyNameCode\CompanyName.py", line 3, in <module> import googleanalytics File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\googleanalytics\__init__.py", line 3, in <module> from . import auth, commands, utils, account, auth, blueprint, columns, errors, query, segments File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\googleanalytics\auth\__init__.py", line 15, in <module> from . import oauth File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\googleanalytics\auth\oauth.py", line 7, in <module> from oauth2client import client File "C:\Users\MYNAMEn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\oauth2client\client.py", line 32, in <module> import urlparse ModuleNotFoundError: No module named 'urlparse'
问题详情
试过安装多个依赖包、更新Python和所有包版本,但还是出现这个错误。代码里的import urllib.parse在VSCode里是灰色的,没被直接用到,推测问题出在包的调用逻辑或者版本兼容性上。知道urlparse是Python2专属模块,我用的是Python3,而且所有依赖包都更新过了,哪怕删掉import urllib.parse,报错还是存在。
需求上下文
需要从Google Analytics 3(GA3)获取指定指标和维度,保存成CSV;之后把CSV导入BigQuery,再回填到Google Analytics 4(GA4)。当前代码负责完成到保存CSV的流程。试过导入各种模块、更新包、卸载重装Python和所有依赖包,但问题还是没解决,求可行的解决方案。
解决方案
问题根源
你用的googleanalytics是第三方旧库,依赖的oauth2client版本是Python2时代的,这个版本里硬编码导入了Python2专属的urlparse模块,而Python3里对应的是urllib.parse,所以直接运行会报错。这个第三方库大概率已经停止维护,建议改用Google官方支持Python3的GA3 Reporting API库。
替换后的代码
使用官方的google-api-python-client库重写代码,兼容Python3:
import json import csv from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials # 定义API访问范围和GA3视图ID SCOPES = ['https://www.googleapis.com/auth/analytics.readonly'] VIEW_ID = '74466483' # 初始化GA Reporting服务 def initialize_analyticsreporting(): credentials = ServiceAccountCredentials.from_json_keyfile_name( 'key.json', SCOPES) # 构建v4版本的Analytics Reporting服务 analytics = build('analyticsreporting', 'v4', credentials=credentials) return analytics # 获取GA3报告数据 def get_report(analytics): return analytics.reports().batchGet( body={ 'reportRequests': [ { 'viewId': VIEW_ID, 'dateRanges': [{'startDate': '2023-01-01', 'endDate': '2023-05-01'}], 'metrics': [{'expression': 'ga:sessions'}, {'expression': 'ga:bounceRate'}, {'expression': 'ga:averageTimeOnPage'}, {'expression': 'ga:entrances'}, {'expression': 'ga:pageviews'}, {'expression': 'ga:uniquePageviews'}], 'dimensions': [{'name': 'ga:source'}, {'name': 'ga:page'}] } ] } ).execute() # 处理数据并写入CSV def save_to_csv(response): with open('report.csv', 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) # 生成表头:维度名(去掉ga:前缀)+ 指标名(去掉ga:前缀) header = [dim.split(':')[1] for dim in response['reports'][0]['columnHeader']['dimensions']] header += [metric['name'].split(':')[1] for metric in response['reports'][0]['columnHeader']['metricHeader']['metricHeaderEntries']] writer.writerow(header) # 写入每一行数据 for row in response['reports'][0]['data']['rows']: row_data = row['dimensions'] row_data += [metric['values'][0] for metric in row['metrics']] writer.writerow(row_data) def main(): analytics = initialize_analyticsreporting() report_data = get_report(analytics) save_to_csv(report_data) if __name__ == '__main__': main()
操作步骤
- 安装官方依赖包:
pip install google-api-python-client oauth2client
这里的oauth2client会安装适配Python3的版本,不会出现urlparse的问题。
2. 确保key.json服务账号密钥文件路径正确,且该账号已在GA3后台获得对应视图的读取权限。
3. 运行修改后的代码,即可生成包含指定指标和维度的CSV文件。
内容的提问来源于stack exchange,提问作者Daniel Kamen

