最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

import - Is Python supposed to be this slow when importing? - Stack Overflow

programmeradmin5浏览0评论

My understanding is that imports can be slow when you import the first time, and then they should be cached for subsequent imports. However that's not what I'm experiencing. And I really don't think I'm doing something wrong like having the wrong settings because I remember this same thing happening when I tried using Python a year ago. I'm doing the following:

from langchain_google_genai import ChatGoogleGenerativeAI
from browser_use import Agent

Each of those import statements take about 6 seconds each. This means each time I start my program I have to wait at least 10 seconds. Is that normal? I've heard the benefit of using Python in machine learning and AI contexts was the faster iteration time because you didn't have to recompile as you do in C++. Also, I've used NodeJS, and I don't remember the imports ever taking long, though to be fair I didn't have large projects. Is this normal?

Also, I stepped through the code to check whether it was doing io like reading in a file, as this is slow no matter what language you use, including C++, but no, it just seems slow overall.

Here is a minimal example of what's happening.

My understanding is that imports can be slow when you import the first time, and then they should be cached for subsequent imports. However that's not what I'm experiencing. And I really don't think I'm doing something wrong like having the wrong settings because I remember this same thing happening when I tried using Python a year ago. I'm doing the following:

from langchain_google_genai import ChatGoogleGenerativeAI
from browser_use import Agent

Each of those import statements take about 6 seconds each. This means each time I start my program I have to wait at least 10 seconds. Is that normal? I've heard the benefit of using Python in machine learning and AI contexts was the faster iteration time because you didn't have to recompile as you do in C++. Also, I've used NodeJS, and I don't remember the imports ever taking long, though to be fair I didn't have large projects. Is this normal?

Also, I stepped through the code to check whether it was doing io like reading in a file, as this is slow no matter what language you use, including C++, but no, it just seems slow overall.

Here is a minimal example of what's happening.

Share Improve this question edited Mar 24 at 3:21 Zebrafish asked Mar 21 at 21:44 ZebrafishZebrafish 15k3 gold badges66 silver badges153 bronze badges 18
  • 3 When you import, the file is executed, so if those modules have long-running initialization code at the top-level, that code needs to finish before the module can be initialized. If modules don't have top-level, blocking code (ie, just function definitions), importing will be basically instant. – Carcigenicate Commented Mar 21 at 21:46
  • Try adding a third import that just imports some trivial module in the same directory that you wrote, then put distinct prints after each to see which is the holdup. Your simple module should import instantly, but the other two may take a bit if they have initialization like I mentioned. I poked through the code for each and couldn't find any such blocking code, but I only looked briefly. If even your simple module takes forever to import, that is not typical. – Carcigenicate Commented Mar 21 at 22:00
  • 1 @Carcigenicate Yes, I stepped through it line by line, the first one takes 6 seconds, the second one takes 6 seconds, then other stuff is pretty much instant, so it's those two. – Zebrafish Commented Mar 21 at 22:01
  • Unfortunately, I don't think there's really anything you can do about that. Tbf to Python though, the same would happen in Node if you had blocking code like fs.readFileSync at the module level. – Carcigenicate Commented Mar 21 at 22:36
  • 1 @Zebrafish Run your script as a single-instance local socket server. If you start it as a background task when logging in, you'll only need to pay the start-up costs once. Use a lock file to check whether the script is already running - if not, start the server; otherwise send the required commands via the socket to the server (you could use json for complex commands). – ekhumoro Commented Mar 22 at 14:34
 |  Show 13 more comments

1 Answer 1

Reset to default 0

here's what i see

( h/ware/OS: Intel(R) Core(TM) i5-10310U CPU @ 1.70GHz hp laptop running WSL )


 

cat /tmp/z.py
import time

start = time.time()
print( "RUNNING : from langchain_google_genai import ChatGoogleGenerativeAI")
from langchain_google_genai import ChatGoogleGenerativeAI
end = time.time()
print(f"import took {end-start} seconds\n")
t1=end-start
start = time.time()
print( "RUNNING : from browser_use import Agent")
from browser_use import Agent
end = time.time()
print(f"import took {end-start} seconds\n")
t2=end-start

print(f"total time taken: {t1+t2}")

#
# first run
#
python3 /tmp/z.py
RUNNING : from langchain_google_genai import ChatGoogleGenerativeAI
import took 1.6047194004058838 seconds

RUNNING : from browser_use import Agent
INFO     [browser_use] BrowserUse logging setup complete with level info
INFO     [root] Anonymized telemetry enabled. See https://docs.browser-use/development/telemetry for more information.
import took 0.9617722034454346 seconds

total time taken: 2.5664916038513184

#
# a second time
#
python3 /tmp/z.py
RUNNING : from langchain_google_genai import ChatGoogleGenerativeAI
import took 0.9231696128845215 seconds

RUNNING : from browser_use import Agent
INFO     [browser_use] BrowserUse logging setup complete with level info
INFO     [root] Anonymized telemetry enabled. See https://docs.browser-use/development/telemetry for more information.
import took 0.5260987281799316 seconds

total time taken: 1.4492683410644531

#
# summary ....
#
grep 'total' /tmp/z.log | awk '{ total+=$NF } END { print "AVE time: " total/FNR " over " FNR " iterations"}'
AVE time: 1.37213 over 76 iterations
发布评论

评论列表(0)

  1. 暂无评论