Skip to content
Mikaël Capelle edited this page Jun 25, 2020 · 3 revisions

This page will get you started for creating a MO2 python plugin.

Required tools

It is possible to write a MO2 python plugin using any text editor, but this guide will focus on [Visual Studio Code]. This guide assumes that:

Note: In the following, I will refer to the MO2 installation directory as $MO2DIR. So if you installed MO2 at C:\MO2 and you are asked to copy a file to $MO2DIR/plugins, it refers to C:\MO2\plugins.

Preparation

Note: This part is optional but highly recommended if you want a proper environment to work with. Everything here is written to be as simple as possible but you can of course adapt it to your preferences: use a python virtual environment, use workspace settings instead of global ones, etc.

1. Get the mobase stubs

mobase is the MO2 Python module. The module is written in C++ and thus cannot be read directly by tools such as flake8 or mypy. Instead, we provide stubs which can be used for auto-completion or type-checking.

The stubs for mobase are available at https://github.com/ModOrganizer2/pystubs-generation/tree/master/stubs. You want to download the mobase.pyi file in the folder corresponding to your MO2 version and put it under $MO2DIR/plugins/data.

Note: It is possible to put the stubs in a different location, but we are going to use $MO2DIR/plugins/data for PyQt5, so we might as well use it for the stubs.

2. Configure Visual Studio Code for mobase

We are going to configure Visual Studio Code to have auto-completion and linting (error and type checking) for the MO2 Python module. Open settings.json (Ctrl+Shift+P, then "Open Settings (JSON)"), and add the following entries:

"python.linting.enabled": true,
"python.linting.mypyEnabled": true,
"python.linting.flake8Enabled": true,
"python.autoComplete.extraPaths": [
    "$MO2DIR\\plugins\\data",
]

3. Configure mypy to find the mobase stubs

There are multiply way to configure mypy:

  1. You can create a mypy.ini file somewhere containing:
[mypy]
mypy_path = $MO2DIR\plugins\data

And then add the following to settings.json (replace with the correct path):

"python.linting.mypyArgs": [
    "--config-file=path-to-mypy.ini",
]
  1. You can set the MYPYPATH environment variable to $MO2DIR\plugins\data.