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

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

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 3.8: speed up the import of external libraries after restarting the PC
Imports of large Python library (aws-cdk-lib) extremely slow
Speeding up the python "import" loader - Stack Overflow
Matplotlib 3.5.1 pyplot import is slow - Installation - Matplotlib
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.
zipping up as many pyc files as feasible (with proper directory structure for packages), and putting that zipfile as the very first entry in sys.path (on the best available local disk, ideally) can speed up startup times a lot.
The first things that come to mind are:
- Try a smaller path
- Make sure your modules are pyc's so they'll load faster
- Make sure you don't double import, or import too much
Other than that, are you sure that the disk operations are what's bogging you down? Is your disk/operating system really busy or old and slow?
Maybe a defrag is in order?
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.
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))
My script has a long *startup time* just importing all its modules at the top of the file.
But not all of those modules are needed for every feature of the script, so I tried to reduce startup time by moving those import statements to local imports inside only the functions that need them.
Are there any reasons not to do this?