Idapython: the solution of importing “site” failed error

0 x00 background

suddenly one day IDA6.8 opens and prompt idapython error:

0x01 problem cause

IDA website has an explanation for this problem:

This issue is usually caused by presence of non-standard python27.dll
in the PATH which uses its own set of modules (you should edit PATH in
this case). However, it may happen if your Python installation is
broken in some way. Reinstalling Python manually may fix it.

The general reason for

is that your host’s environment variable path contains the non-standard python27.dll, and, of course, it may also be due to an unexpected terminal during an installation. Manually reinstalling Python may fix this problem.

0x02 problem fix

official also kindly give detailed repair method:

  1. view python27. DLL location
    c:\>where python27.dll
  1. view in the registry HKEY_LOCAL_MACHINE\SOFTWARE\Python 2.7\InstallPath the value is python2.7’s installation path
  2. adding an environment variable with the key PYTHONHOME and the value is Python’s installation path.
  3. if idapython was loaded normally but this error still exists, print sys.path to determine if it is as expected
Python>import sys
['C:\\Windows\\system32\\', 'C:\\Python27-x64\\Lib', 'C:\\Python27-x64\\DLLs', 'C:\\Python27-x64\\Lib\\lib-tk', 'C:\\Program Files\\IDA 7.0\\python', 'C:\\Python27-x64', 'C:\\Python27-x64\\lib\\site-packages', 'C:\\Program Files\\IDA 7.0\\python\\lib\\python2.7\\lib-dynload\\ida_32', 'C:\\Program Files\\IDA 7.0\\python']
  1. tracks the loading process of IDAPython module. Set the environment variable PYTHONVERBOSE=1 before IDA runs, and the path path will be printed out (or add -l to the IDA run command to generate the log file).

0x03 problem solved

using the third step, creating an environment variable PYTHONHOME will solve my current problem.

is the pitfall of some blog that creates the environment variable %PYTHONHOME%. What the hell is that?

in the context of environment variables, %path% means that path is a defined call to an environment variable, equivalent to $path in Linux. The first time you define a variable, you don’t use %%.

0x04 reference

Read More: