0

i have some scripts which imports pandas, numpy... and the time to import all these packages is about 1.5 s and the total execution time of the script is less 2s

Is there any way to speed up the import modules under python3 ?

Using the technique from pandas import xxx of how to reduce the time of python import modules? does not improve the execution time (despite of i only import three functions) Thank you.

Ksartor
  • 75
  • 8
  • Try converting the script into a background server process that responds to API requests via socket or some other interface, and a client script that takes user input and makes API requests to the server script. Since the server loads those large modules only once at startup, subsequent API requests would no longer have such overhead. – blhsing Jul 23 '21 at 04:57
  • Unfortunately, there's no good way to speed up importing pandas and numpy (two of the largest packages in the entire python ecosystem) besides simply increasing the specs of the machine you're running the program on. Alternatively you could try running using pypy instead of cpython, see if that gets you some speed improvement. – Green Cloak Guy Jul 23 '21 at 05:00
  • Importing only a specific name from a module still requires that python import the entire module behind the scenes, most of the time, so that's not a solution you can use. – Green Cloak Guy Jul 23 '21 at 05:01
  • @GreenCloakGuy: i can't change the server used – Ksartor Jul 23 '21 at 13:26
  • @blhsing: is there any way to build a unique server to be used by all script and only precharge the module? because i don't need a API. Is there any python capabilities to precharge module in cache? – Ksartor Jul 23 '21 at 13:27

0 Answers0