You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何虚拟环境在Google Drive时Python requests导入耗时15秒?

问题背景

在Win11系统上运行PyCharm 2024.3.2时,若项目(含虚拟环境venv)存储在Google Drive中,代码执行到import requests会出现约15秒的延迟。但以下两种场景无延迟:

  • 在C盘创建新项目并使用本地虚拟环境
  • Google Drive中的项目指向C盘的虚拟环境

需分析该延迟问题的根源。

测试代码

import logging
logger = logging.getLogger(__name__)

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

logger.info('about to import requests')
import requests
logger.info('after requests')

print("hello world")

base_url = 'https://www.google.com'
response = requests.get(base_url)
print(response)
logger.info('all done')

不同场景的运行结果

场景1:项目与虚拟环境均在Google Drive

"G:\My Drive\PyCharmProjectsHoth\requests_test\.venv\Scripts\python.exe" "G:\My Drive\PyCharmProjectsHoth\requests_test\test.py" 
2025-02-14 10:32:56 - INFO - about to import requests
hello world
2025-02-14 10:33:10 - INFO - after requests
2025-02-14 10:33:10 - INFO - all done
<Response [200]>

注:import requests操作耗时约14秒

场景2:项目与虚拟环境均在C盘

C:\test\requests_test\.venv\Scripts\python.exe C:\test\requests_test\test.py 
2025-02-14 10:35:05 - INFO - about to import requests
2025-02-14 10:35:05 - INFO - after requests
hello world
<Response [200]>
2025-02-14 10:35:05 - INFO - all done

注:无明显延迟

场景3:项目在Google Drive,虚拟环境在C盘

C:\test\requests_test\.venv\Scripts\python.exe "G:\My Drive\PyCharmProjectsHoth\requests_test\test.py" 
2025-02-14 10:36:16 - INFO - about to import requests
2025-02-14 10:36:16 - INFO - after requests
hello world
<Response [200]>
2025-02-14 10:36:16 - INFO - all done

注:无明显延迟

问题根源分析

  1. Google Drive同步机制的额外IO开销
    Google Drive桌面客户端会对存储文件进行实时同步校验,包括文件哈希比对、云端状态检查等。当Python从Google Drive的虚拟环境加载requests时,需要读取大量依赖文件(如requests源码、urllib3、certifi等依赖库的文件),每一次文件读取都会触发同步校验逻辑,大幅增加IO耗时,最终导致模块导入延迟。

  2. 小文件批量读取的适配差异
    Python导入requests时会递归加载所有依赖模块,涉及成百上千个小文件的读取操作。本地磁盘(如C盘)的IO延迟极低,可快速处理这类批量小文件读取;但Google Drive的网络同步层对小文件批量读取优化不足,频繁的文件操作会被同步逻辑阻塞,累积形成明显延迟。

  3. 虚拟环境位置的核心影响
    当虚拟环境在C盘时,即使项目文件在Google Drive,Python加载模块的路径指向本地磁盘的venv,所有依赖文件读取都走本地IO,避开了Google Drive的同步校验,因此无延迟。这直接证实:延迟的核心是虚拟环境所在位置的文件系统差异,而非项目文件的存储位置。

内容的提问来源于stack exchange,提问作者Greg Vaeth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 09:26:16