Skip to content

9.Importing and Using Airtest


Some users encounter errors such as module import failure or missing module when importing other .py/.air scripts in their automation scripts.

ImportError: cannot import name 'Select'

ModuleNotFoundError: No module named 'test'

In this article, we will delve into this issue and hopefully provide some solutions.

2.Python Standard Library and Third-Party Libraries

First, let's take a look at a fundamental question: What isthe difference between Python's standard library and third-party libraries? A quick answer is as shown in the following figure:


1) Python standard library

The standard library of Python consists of built-in modules such as random and os, which are included automatically when we install Python.

It is very simple to use this library. You can just import them directly and use them without any extra installation.

import random

2) Python third-party libraries

What about Python's third-party libraries? These are frameworks developed by unofficial organizations or individuals with specific functionalities. For example, Airtest is a Python third-party library for automated UI testing, and Django, as shown in the above figure, is a Python third-party library for web development.

To use these third-party libraries, you need to have a Python environment and install the libraries you need in it, such as Airtest and Poco.

pip install airtest
pip install pocoui

After successfully installing these third-party libraries into you Python environment, you can then import the desired functionalities from these libraries.

from airtest.core.api import *
snapshot(msg="Please fill in the test point.")

3) Solution for no module named 'airtest' error

After understanding the difference between the Python standard library and third-party libraries, you will know that errors like "no module named 'airtest'" or "no module named 'airtest-selenium'" occur because these third-party libraries are not installed in the current Python environment.

The solution is also very simple. Find the Python environment currently being used and install the required third-party libraries in that environment. (Note that if there are multiple Python environments on the local machine, make sure to install the required third-party libraries in the environment being used.)

3.Several Methods for Importing Modules in Python

1)import os

This import method is commonly used to import Python's standard library:

# Print the current working directory.

import os

2) from os import chmod

This import method is commonly used for importing package files, but it can also be used in this way:

from import *

Although this kind of import won't throw an error, it is relatively poor in readability.

3) import logging as log

This method assigns aliases to the imported modules, allowing us to use the alias to call the methods of the module later on:

import logging as log'this is a info message')

4.How to know from which module to import the method you need?

Let's take the use of the Airtest framework as an example. When we create a new .air script in the IDE, it will by default include this import statement:

from airtest.core.api import *

This statement imports all contents from the module airtest.core.api, which are the core APIs of Airtest. Commonly used methods such as auto_setup, start_app, touch, and swipe are all under this module.

1) Solution for "name 'xxx' is not defined" error

However, Airtest has many other modules, such as the report module. In the tutorial documentation, we have learned that we can use simple_report to generate Airtest reports, but when writing the script, we may encounter the following issue:

from airtest.core.api import *

snapshot(msg="Please fill in the test point.")


Traceback (most recent call last):
  File "airtest\cli\", line 73, in runTest
  File "site-packages\", line 703, in reraise
  File "airtest\cli\", line 70, in runTest
  File "D:\test\taikang_test.air\", line 11, in <module>

NameError: name 'simple_report' is not defined

This is because simple_report is not in the module airtest.core.api, so we need to import the module where simple_report is located separately.

So here comes the question: How do we know which module simple_report is in? The easiest way is to open the Airtest API documentation and search for simple_report:


We can see that the module where simple_report is located is We just need to add the import statement for this module in the script:

from import simple_report

As another example, in the case of simulating a swipe, we can use the following method to simulate long touch and drag operations:

# Long touch to delete the app.

longtouch_event = [
    DownEvent([908, 892]),# Coordinates of the app to be deleted.
    MoveEvent([165,285]),# Coordinates of the trash bin to delete the app.

We can still find out which modules need to be imported by referring to the API documentation as mentioned above:


Here are the API documentation links for Airtest and Poco:

  • Airtest API Documentation

  • Poco API Documentation

5.Using the using() API to Import Other .air Scripts

After discussing the usage of libraries and methods for importing modules, let's take a look at another issue related to importing - how to import other .air scripts? Airtest offers a using API that can add the required scripts to sys.path. The image files contained in them will also be added to the search path of the Template. For example, if we have two .air scripts at the same level and want to import one into another, we can achieve this as follows:

# -*- encoding=utf8 -*-
__author__ = "AirtestProject"

from airtest.core.api import *


from common import common_function

However, if we run this script outside of AirtestIDE, such as running it in Pycharm, the following error may occur:

Traceback (most recent call last):
  File "D:/test/taikang_test.air/", line 13, in <module>
    from common import common_function
ModuleNotFoundError: No module named 'common'

In this case, the easiest and most straightforward solution is to change the relative path in the using API to an absolute path:


from common import common_function

If you don't want to modify the script in the using API, you can also add an additional path of the script to be imported to sys.path:

import sys


from common import common_function

Adding the project root directory can also achieve similar results:

# PROJECT_ROOT needs to be filled with an absolute path.
ST.PROJECT_ROOT = "D:/test/user/project"


from common import common_function

1) Issues with path slashes

When filling in the path, make sure to use forward slashes "/" instead of backslashes "\", especially when copying paths from the computer, which uses backslashes "\" in its file paths. If not converted manually in the code, it may result in the following error:


The example path in the figure contains many backslashes "\" (special characters). One approach is to escape them by adding an additional backslash "\", which means the path string can be written as follows: D:\test\user\project. Alternatively, we can define a raw string (ignoring all escape characters, such as backslashes "\") by adding an "r" before the string, like this: r"D:\test\user\project".

2) The called module does not exist

During the process of calling, we often encounter the following error messages: 图片 The error message above means that the program can't find the module named test1 that we want to call. In this case, we can consider the following aspects: Double-check if the module being called exists in the path you specified. Check if you used a relative path instead of an absolute path when setting the default project root directory PROJECT_ROOT. If none of the above is the issue, but the program still cannot find the module you want to call, you can try adding the path to sys.path. Python will search for the corresponding .py file under the path in sys.path. Taking the example of the main.air script mentioned above:

# main.air
from airtest.core.api import *

# Add the path of test1.air to sys.path.

from test1 import test

This method modifies sys.path during runtime and becomes invalid after the execution ends. In addition, you can add paths to search for modules by setting the PYTHONPATH environment variable, which is similar to setting the Path environment variable. Note that you only need to add your search path, and Python's search path is not affected.

3) Scripts that can be run in IDE produce errors when run in the command line

In the previous section, we have introduced two methods for calling functions with examples run in the IDE. However, if we directly use the "airtest run absolute path of the script" in the command line, we may encounter an error that the module cannot be found.


This is because the parent path is added to sys.path by default in the IDE, but when running scripts in the command line, the parent path is not added by default. To avoid the above error, we can manually add the path to sys.path in the script.