Recently, I found that many students are asking Python Problems of introduction and invocation , For example, if an error is reported, the module cannot be imported 、 Or a module cannot be found .
ImportError: cannot import name 'Select' ModuleNotFoundError: No module named 'test'
Today, let's talk about this problem alone , I hope I can give some solutions to the students .
1. Python Standard library and third-party library
First, let's look at a basic problem , Differentiate Python Standard library and third-party library . As shown in the figure below ：
1）Python Standard library
Python The standard library of , It's actually some built-in modules , As long as we install Python, Just bring these built-in modules , such as
os wait .
Using these libraries is also very simple , We don't need additional installation , You can do it directly
import Come in and use ：
import random print(random.random())
2）Python Third party Library
that Python What about your third-party library , Some unofficial organizations or individuals , A written framework with some specific functions , Like our Airtest, It's used to do UI A of automated testing Python Third party Library ; And the example in the figure Django, It's for carrying on web One developed Python Third party Library .
Using these third-party libraries , Except for one Python Beyond the environment , And in this Python In the environment , Install the third-party library we need , For example, use Airtest and Poco：
pip install airtest pip install pocoui
Successfully installed these third-party libraries into our Python After the environment , We can get from these libraries
import What we want ：
from airtest.core.api import * auto_setup(__file__) snapshot(msg=" Please fill in the test point .")
no module named 'airtest' Solutions for
If the students understand Python The difference between standard library and third-party library , Meet something like
no module named 'airtest' perhaps
no moudle named 'airtest-selenium' Wait for the question , You'll know it's because of the current Python The reason why these third-party libraries are not installed in the environment .
The solution is also very simple , Find the currently used Python Environmental Science , Then install the third-party library we need in this environment .（ Pay special attention if there are multiple Python Environmental time , The one that needs to be used by the students Python Install the required third-party libraries in the environment ）
2. Python Several ways to import modules
This import method is often used to import Python The standard library of ：
# Print the current working directory import os print(os.getcwd())
from os import chmod
This import method is often used to import package files , Of course, some students are used to using ：
from airtest.report.report import *
In this way, although the import will not report an error , But the readability is relatively poor .
import logging as log
This method aliases the imported module , Later, when we use the method of this module , You can call... With an alias ：
import logging as log log.info('this is a info message')
3. How to know from which module to introduce the method you need
We'll take Airtest For the use of framework , We are IDE It's new on the Internet 1 individual
.air Script , The introduced script will be brought by default ：
from airtest.core.api import *
This sentence means to introduce
airtest.core.api Everything in this module , That is to say Airtest At the heart of API.
Like we often use
swipe And so on are all under this module .
name 'xxx' is not defined Solutions for
however airtest There are many other modules , For example, the report module . In the tutorial documentation , We know we can use
simple_report To generate Airtest The report , But when you actually write a script , The following problems may occur ：
from airtest.core.api import * auto_setup(__file__,logdir=True) snapshot(msg=" Please fill in the test point .") simple_report(__file__,logpath=True) ----- Traceback (most recent call last): File "airtest\cli\runner.py", line 73, in runTest File "site-packages\six.py", line 703, in reraise File "airtest\cli\runner.py", line 70, in runTest File "D:\test\taikang_test.air\taikang_test.py", line 11, in <module> simple_report(__file__,logpath=True) NameError: name 'simple_report' is not defined -----
This is because
simple_report be not in
airtest.core.api In this module , We need to introduce additional
simple_report Where the module is .
So here comes the question , How do I know
simple_report Under which module ？ The easiest way , open Airtest Of API file , And then search
simple_report that will do ：
You can see
simple_report The module is
airtest.report.report , We just need to add the introduction of the module in the script ：
from airtest.report.report import simple_report simple_report(__file__,logpath=True)
Another example , In the case of simulated sliding , We can see that the following method can be used to simulate the operation of long press and drag ：
# Long press to delete the application longtouch_event = [ DownEvent([908, 892]),# Coordinates of the application to be deleted SleepEvent(2), MoveEvent([165,285]),# Delete the trash can coordinates of the application UpEvent(0)] device().touch_proxy.perform(longtouch_event)
We can still check through the above API How to document , To know which modules need to be introduced ：
Here we attach Airtest and Poco Of API Document address ：
- Airtest API file ：https://airtest.readthedocs.io/zh_CN/latest/index.html
- Poco API file ：https://poco.readthedocs.io/zh_CN/latest/index.html
4. Introduce other
.air The script
After talking about the use of the library and the methods of module introduction , Let's look at another question about introduction , How to introduce other
.air Script .
using Interface , Be able to add scripts that need to be referenced
sys.path in , The picture files contained in it will also be added
Template In the search path of .
for instance , We have two peers
.air Script , To be in 1 individual
.air The script introduces another
.air Script , That's how it works ：
# -*- encoding=utf8 -*- __author__ = "AirtestProject" from airtest.core.api import * auto_setup(__file__) using("common.air") from common import common_function common_function()
But if we leave AirtestIDE To run this script , For example pycharm Open this script to run , The following error reports may occur ：
Traceback (most recent call last): File "D:/test/taikang_test.air/taikang_test.py", line 13, in <module> from common import common_function ModuleNotFoundError: No module named 'common'
here , The simplest and most brutal way is to
using Change the relative path to the absolute path ：
using(r"D:/test/common.air") from common import common_function common_function()
If you don't want to change
using Script for , We can also add 1 A script to be introduced
sys.path in ：
import sys sys.path.append("D:/test/common.air") using(r"common.air") from common import common_function common_function()
So in today's tutorial, we mainly talk about the following contents ：
- Python The difference between standard library and third-party library and the introduction method
- Python Several kinds of
- stay API The module where the document query method is used
- Introduce other
If there are other problems related to the introduction , Welcome to our official exchange Q Group （654700783） feedback .
Official Q & A Q Group ：654700783
ah , You've seen it so seriously , Please give me a recommendation and support , Thank you very much ~