-->

Python is very slow to start on Windows 7

2019-04-03 23:36发布

问题:

Python takes 17 times longer to load on my Windows 7 machine than Ubuntu 14.04 running on a VM (inside Windows on the same hardware). Anaconda3 distribution is used on Windows and Ubuntu default python3.4.

From a Bash prompt (Git bash on Windows):

$ time python3 -c "pass"

returns in 0.614s on Windows and 0.036s on Linux

When packages are loaded the situation gets worse:

$ time python3 -c "import matplotlib"

returns in 6.01s on Windows and 0.189s on Linux

Spyder takes a whopping 51s to load on Windows and 1.5s on Linux.

I have not had any luck determining why I have this performance problems. Does anyone have any ideas what I should try next?

edit:

Why is python so much slower on windows? has been suggested as a possible duplicate but my performance different is far greater and not explained simply by different library dependencies and compilers. This seems to me to be related to filesystem differences.

I had suspected antivirus on-access scans but disabled the antivirus just in case.

>>> sys.path

['', 'c:\\Anaconda3\\python34.zip', 'c:\\Anaconda3\\DLLs', 'c:\\Anaconda3\\lib', 'c:\\Anaconda3', 'c:\\Anaconda3\\lib\\site-packages', 'c:\\Anaconda3\\lib\\site-packages\\Sphinx-1.2.3-py3.4.egg', 'c:\\Anaconda3\\lib\\site-packages\\cryptography-0.8-py3.4-win-amd64.egg', 'c:\\Anaconda3\\lib\\site-packages\\nose-1.3.4-py3.4.egg', 'c:\\Anaconda3\\lib\\site-packages\\win32', 'c:\\Anaconda3\\lib\\site-packages\\win32\\lib', 'c:\\Anaconda3\\lib\\site-packages\\Pythonwin', 'c:\\Anaconda3\\lib\\site-packages\\setuptools-14.3-py3.4.egg']

Update:

A fresh install of Windows 8.1 Pro on the same PC solved the problem. After reinstalling all applications and Anaconda3 Python performance is the best I have seen. Unfortunately the root cause of this issue is still unknown.

Correction:

After my IT dept installed Sophos SafeGuard encryption software and MS Endpoint Protection the problem returned. Same slow start as before. Disabling the extra software did not solve the problem so we are trying tests on other machines to trace the problem.

回答1:

The problem is solved by uninstalling Sophos SafeGuard. This is not really a satisfactory solution though since my company uses this filesystem encryption software on directories that I access daily. I have not seem any other performance problems except with Python (and apparently Ruby as well).

NOTE: Sophos SafeGuard is not antivirus software. It is an enterprise filesystem encryption system. The strange thing is that encryption is explicitly not enabled for local filesystems, such as where Python is installed.



回答2:

May not be relevant to your case, but I found that running python in Windows with Sophos Safeguard and Mcafee Enteprise Antivirus that python startup times were an order of magnitude slower if python was being run as an elevated process. Switching it to run as a normal process made a dramatic difference for me.



回答3:

  1. Perhaps a contributor to startup variance could be default loaded modules. You can use sys.modules to compare your two environments.

python -c "import sys;print(len(sys.modules))"

For me the answer is

$ time py -2 -c "pass"

real    0m0.054s
user    0m0.000s
sys     0m0.000s

$ py -2 -c "import sys;print(len(sys.modules))"
44

$ time py -3 -c "pass"

real    0m0.063s
user    0m0.000s
sys     0m0.000s

$ py -3 -c "import sys;print(len(sys.modules))"
54

And you can use virtual envs to manipulate the default loaded modules. https://virtualenv.pypa.io/en/latest/

  1. Git bash for windows seems to be misbehaving for me with python. I don't see the version banner when I launch the interpreter. I would compare start times with a cmd prompt. Or even with a python launching python. E.g.

-

import subprocess
import time
start = time.time()
subprocess.check_call(["python", '-c ', 'pass'])
print time.time() - start