Python3 Environment Basics For IDA Pro (Windows) (Patreon)
Content
Usually Python and IDA play nice but when they don't the troubleshooting process can be maddening. In this tutorial we will cover some IDA Python environment basics that can assist with troubleshooting and configuring a sane Python 3 environment for IDA Pro.
If you are running the latest version of IDA and you have a single Python environment installed on your host you may never need this guide. However, if you are like us, and you want to run multiple versions of IDA to support older plugins, along with multiple versions of Python, read on!
First here are some references for known issues.
- IDA 7.4 and Python 3.8
- IDA 7.5 and Python 3.9
- IDA 7.5 idapyswitch tool does not support Python 3.10+
Selecting The IDA Python Environment With idapyswitch
Since the release of version 7.4 IDA has shipped with a tool called idapyswitch. This tool is located in the IDA install directory and can be used to change the Python environment that IDA is using. This tool can also be used to determine IDA's current Python environment.
The version of Python in use by IDA can also be verified directly from the IDA Python command line.
Installing Python Modules For IDA Plugins
One of the most common IDA Python issues is a mismatch between the version of Python that is being used to install modules to support an IDA plugin and the version of Python used by IDA. This happens when IDA is using a version of Python that is different from the version of Python used by the host command line.
It is important to understand which Python environment IDA is using and use the same environment when installing modules to support an IDA plugin from the command line. To avoid these issues we recommend the following process when installing an IDA plugin that requires additional Python modules.
- Use idapyswitch to determine the exact Python environment used by your IDA install. This will also show you the Python version path. For example, C:\Python38\
- Open cmd and use the same path to invoke pip by appending \Scripts\pip.exe to the path. For example, C:\Python38\Scripts\pip.exe
- Use this pip to install the modules required by the IDA plugin. For example, C:\Python38\Scripts\pip.exe install requests
By using this procedure you can ensure that the modules you are installing are installed for the correct IDA Python environment.
The \python IDA Directory
In addition to installing Python modules via pip IDA also has a special \python directory in the root of its install directory. For example, C:\Program Files\IDA Pro 7.6\python. This directory is automatically added to the IDA Python path meaning that any python file, or module in this directory will be available to IDA Python at runtime.
Some plugins may require a custom module be deployed to this directory. For example the FLARE-IDA tools.
The idapythonrc.py Configuration File
The IDA user directory can be used to add additional configuration settings to IDA Python via an idapythonrc.py configuration file. This python file that can be created in the IDA user directory %APPDATA%/Hex-Rays/IDA/idapythonrc.py. Python commands contain in the file will be executed at the end of IDA Python's initialization. One common use for this file is to add additional paths to the IDA Python environment.
The Beginner's Guide to IDAPython
For further information on scripting with IDA Python we highly recommend Alex Hanel's excellent (pay what you want) book The Beginner's Guide to IDAPython.