Not an actual answer to the question, but a hint on how to profile the import speed with Python 3.7 and tuna (a small project of mine):

python3 -X importtime -c "import scipy" 2> scipy.log
tuna scipy.log

Answer from Nico Schlömer on Stack Overflow
Top answer
1 of 7
104

Not an actual answer to the question, but a hint on how to profile the import speed with Python 3.7 and tuna (a small project of mine):

python3 -X importtime -c "import scipy" 2> scipy.log
tuna scipy.log

2 of 7
29

you could build a simple server/client, the server running continuously making and updating the plot, and the client just communicating the next file to process.

I wrote a simple server/client example based on the basic example from the socket module docs: http://docs.python.org/2/library/socket.html#example

here is server.py:

# expensive imports
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import scipy.ndimage
import scipy.signal
import sys
import os

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while 1:
    conn, addr = s.accept()
    print 'Connected by', addr
    data = conn.recv(1024)
    if not data: break
    conn.sendall("PLOTTING:" + data)
    # update plot
    conn.close()

and client.py:

# Echo client program
import socket
import sys

HOST = ''    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(sys.argv[1])
data = s.recv(1024)
s.close()
print 'Received', repr(data)

you just run the server:

python server.py

which does the imports, then the client just sends via the socket the filename of the new file to plot:

python client.py mytextfile.txt

then the server updates the plot.

On my machine running your imports take 0.6 seconds, while running client.py 0.03 seconds.

🌐
Python
bugs.python.org › issue22557
Issue 22557: Local import is too slow - Python tracker
April 29, 2022 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/66747
Discussions

Python 3.8: speed up the import of external libraries after restarting the PC
I’m developing a script written with Python 3.8.10 and that uses external libraries installed with pip (e.g. torch: torch · PyPI). I’m running that script in Windows 10. The problem is that importing those libraries is very slow the first time I run the script after restarting my PC. More on discuss.python.org
🌐 discuss.python.org
0
0
February 13, 2023
Imports of large Python library (aws-cdk-lib) extremely slow
🐛 Bug Report Affected Languages TypeScript or Javascript Python Java .NET (C#, F#, ...) Go General Information JSII Version: N/A Platform: MacOS What is the problem? Originally reported as aws/aws-... More on github.com
🌐 github.com
40
February 17, 2022
Speeding up the python "import" loader - Stack Overflow
I'm getting seriously frustrated at how slow python startup is. Just importing more or less basic modules takes a second, since python runs down the sys.path looking for matching files (and genera... More on stackoverflow.com
🌐 stackoverflow.com
Matplotlib 3.5.1 pyplot import is slow - Installation - Matplotlib
I’m using Matplotlib 3.5.1 with Python 3.10.1 on Linux. I have lately observed that the import time is noticeably very slow (~3.6s). $ time python -c 'import matplotlib' python -c 'import matplotlib' 1.14s user … More on discourse.matplotlib.org
🌐 discourse.matplotlib.org
0
February 19, 2022
🌐
Reddit
reddit.com › r/python › import of modules is slowing down executable. can it be imported locally or what can be done differently ?
r/Python on Reddit: Import of modules is slowing down executable. Can it be imported locally or what can be done differently ?
March 16, 2022 -

I am importing the following modules/libraries which is causing the exe to take a few minutes to start. How can I store them locally or what can I do differently so that the exe starts quickly? When I run this from the Notebook it the application runs without any delay. I wonder if there is a way to speed up the exe.

import time
import openpyxl
from os import path
from selenium import webdriver
from openpyxl.styles import Alignment
from webdriver_manager.chrome import ChromeDriverManager
from openpyxl.styles.borders import Border, Side
from selenium.webdriver.support.ui import Select
import os
import math
import win32com.client as client
from PIL import ImageGrab

Thank you in advance.

🌐
Python.org
discuss.python.org › python help
Python 3.8: speed up the import of external libraries after restarting the PC - Python Help - Discussions on Python.org
February 13, 2023 - I’m developing a script written with Python 3.8.10 and that uses external libraries installed with pip (e.g. torch: torch · PyPI). I’m running that script in Windows 10. The problem is that importing those libraries is very slow the first time I run the script after restarting my PC.
🌐
Ask Ubuntu
askubuntu.com › questions › 1323018 › python-imports-are-extremely-slow
spyder - Python imports are extremely slow - Ask Ubuntu
March 12, 2021 - Python imports are extremely slow. Some examples include: pandas ~ 8s numpy ~ 4s ants ~ 40-120s (larger package named antspyx) My collegues use the exact same hardware, but for them it runs much ...
🌐
GitHub
github.com › aws › jsii › issues › 3389
Imports of large Python library (aws-cdk-lib) extremely slow · Issue #3389 · aws/jsii
February 17, 2022 - A simple import should not take longer than 1 second (ideally even less, but there are constrains that make that diffucult here I understand). As seen above, it takes ~17 seconds.
Author   rix0rrr
🌐
Gregoryszorc
gregoryszorc.com › blog › 2019 › 01 › 10 › what-i've-learned-about-optimizing-python
Gregory Szorc's Digital Home | What I've Learned About Optimizing Python
January 10, 2019 - As I've demonstrated with PyOxidizer, ... take 70-80% of its original time! Having a single module per filesystem file introduces filesystem overhead and can slow down Python applications in the critical first milliseconds of execution....
🌐
Tomrochette
blog.tomrochette.com › problems › 2020 › 02 › 29
Slow python imports | tomrochette.com
January 22, 2026 - How can I figure out which ones are the source of slowness? Python offers a really useful functionality you can use that will list how long each import took.
Find elsewhere
🌐
Matplotlib
discourse.matplotlib.org › community › installation
Matplotlib 3.5.1 pyplot import is slow - Installation - Matplotlib
February 19, 2022 - I’m using Matplotlib 3.5.1 with Python 3.10.1 on Linux. I have lately observed that the import time is noticeably very slow (~3.6s). $ time python -c 'import matplotlib' python -c 'import matplotlib' 1.14s user 0.09s system 99% cpu 1.233 total $ time python -c 'import matplotlib.pyplot' python -c 'import matplotlib.pyplot' 3.50s user 0.10s system 99% cpu 3.608 total I have search for similar problem on the internet, and found python - Extremely slow import of matplotlib afm - Stack O...
🌐
Gregoryszorc
gregoryszorc.com › blog › 2018 › 12 › 28 › faster-in-memory-python-module-importing
Gregory Szorc's Digital Home | Faster In-Memory Python Module Importing
But the bigger result is the obvious speedup with PyOxidizer and its in-memory importing: PyOxidizer can import almost the entirety of the Python standard library ~100ms faster - or ~70% of original - than a typical standalone CPython install with a hot disk cache!
🌐
Medium
medium.com › analytics-vidhya › 10-ways-to-speed-up-your-python-code-bddd9f9902d0
10 Ways To Speed Up Your Python Code! 🚀 | by Aayushi Ratanghayra | Analytics Vidhya | Medium
August 31, 2021 - (dot) it first calls __getattribute()__ or __getattr()__ which then use dictionary operation which costs time. So, try using from module import function. The global keyword in Python is used to declare global variables.
🌐
Bemusement
files.bemusement.org › talks › OSDC2008-FastPython
How to make a fast command line tool in Python
April 17, 2014 - Obviously to achieve this, your program actually has to be fast at doing its work. But what if you've written your code in Python, and it takes 800ms1 just to import your code, let alone start running it? This paper explains why Python programs tend to be slow to start, and how to make them ...
Top answer
1 of 3
2

Obviously sympy does a lot when being imported. It could be initialization of internal data structures or similar. You could call this a flaw in the design of the sympy library.

Your only choice in this case would be to avoid redoing this initialization.

I assume that you find this behavior annoying because you intend to do it often. I propose to avoid doing it often. A way to achieve this could be to create a server which is started just once, imports sympy upon its startup, and then offers a service (via interprocess communication) which allows you to do whatever you want to do with sympy.

If this could be an option for you, I could elaborate on how to do this.

2 of 3
2

I took a look at what happens when you run import sympy, and it imports all of sympy.

https://github.com/sympy/sympy/blob/master/sympy/__init__.py

If you are only using certain parts of sympy, then only import those parts that you need.

It would be nice if you could do this:

import sympy.sets

But (as you point out) that imports sympy and then sets.

One solution is to write your own importer. You can do this with the help of the imp module.

import imp
sets = imp.load_module("sets", open("sympy/sets/__init__.py"), "sympy/sets/__init__.py", ('.py', 'U', 1))

But, even that may not optimize enough. Taking a look at sympy/sets/__init__.py I see that it does this:

from .sets import (Set, Interval, Union, EmptySet, FiniteSet, ProductSet,
    Intersection, imageset, Complement, SymmetricDifference)
from .fancysets import TransformationSet, ImageSet, Range, ComplexRegion
from .contains import Contains
from .conditionset import ConditionSet

Maybe you can import only the sets module from simpy sets namespace?

import imp
sets = imp.load_module("sets", open("sympy/sets/set.py") "sympy/sets/set.py", ('.py', 'U', 1))
🌐
Python
peps.python.org › pep-0690
PEP 690 – Lazy Imports - Python Enhancement Proposals
The Python standard library already includes built-in support for lazy imports, via importlib.util.LazyLoader. There are also third-party packages such as demandimport. These provide a “lazy module object” which delays its own import until first attribute access.
🌐
PyTutorial
pytutorial.com › optimize-python-import-performance
PyTutorial | Optimize Python Import Performance
May 10, 2025 - Large projects often suffer from import bottlenecks. The Python Import System has overhead that adds up. ... This shows each import's time contribution. Focus on the slowest ones first.
🌐
Quora
quora.com › Do-installed-python-packages-slow-your-program-down-if-you-dont-import-them
Do installed python packages slow your program down if you don't import them? - Quora
Answer (1 of 4): For all practical purposes, no. But depending on the underlying file system, long search paths of bloated directories could conceivably slow down imports of modules you do use. Probably isn’t worth worrying about that unless you make measurements and find it is an actual problem ...
🌐
Python Forum
python-forum.io › thread-42625.html
Sudden Extremely Slow / Failed Python Imports
August 19, 2024 - I have numerous Python scripts that I've developed over the years on a VM at my office. 2 days ago, all of a sudden when I was continuing to enhance/refine several of these Python scripts, upon trying to execute them on the VM in either Sublime Text...