Osvald Laurits
Osvald Laurits

Reputation: 1364

python import takes very long when run in debugger

I'm using https://github.com/DuyguA/DEMorphy . I run the following code with a python3.7 interpreter.

import time

start = time.time()
from demorphy import Analyzer
end = time.time()
print(end-start)

The output is

0.46335792541503906

When run with the Pycharm Debugger the output is

/home/user/virtualenvs/demorphy_test/bin/python3 /home/user/programs/pycharm-community-2019.2.1/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 57954 --file /home/user/demorphy_test/test.py
713.0500545501709

When run with the Visual Studio Code Debugger the output is

/home/user/virtualenvs/ptdev/bin/python /home/user/.vscode/extensions/ms-python.python-2019.10.41019/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 42604 /home/user/demorphy_test/test.py 
693.3676333427429
Terminated

When run with the Eclipse Pydev Debugger the output is

pydev debugger: starting (pid: 26462)
706.7083044052124

Why does the import statement take so long when run with the debugger? How can I make it run faster?

To install the dawg library which demorphy depends on https://github.com/pytries/DAWG/issues/31 is necessary.

Upvotes: 2

Views: 1091

Answers (1)

Brett Cannon
Brett Cannon

Reputation: 16080

It's slow due to how Python debuggers work and how imports work. Importing in Python is literally executing the code in the module. Executing code under a debugger is inherently slower as every step of the execution has to pass through the debugger to see if e.g. a breakpoint is hit.

The only way for this to go faster is for the debuggers to be faster (the Python extension for VS Code is looking at pulling in some debugger speed improvements, but there's no timeline on when those will land and initially they will only be for Python 3.7).

Upvotes: 1

Related Questions