Of course! The import autopy statement is the first step to using the powerful autopy library in Python. Let's break down what it is, what it does, how to install it, and see some practical examples.
What is autopy?
autopy is a Python library that provides a cross-platform, OS-native way to programmatically control your mouse and keyboard. It's essentially a bridge between your Python script and your computer's graphical user interface (GUI).
Think of it as giving your Python script "hands and fingers" to interact with other applications, games, or your desktop environment.
Installation
Before you can import autopy, you need to install it. The easiest way is using pip.
Open your terminal or command prompt and run:
pip install autopy
Important Note for macOS Users:
On macOS, autopy requires the XQuartz system. If you don't have it, you might get an error during installation or when running the script. You can download it from the official XQuartz website.

Core Functionality
The autopy library is primarily split into a few main modules:
autopy.mouse: For controlling the mouse (moving, clicking, scrolling).autopy.keyboard: For simulating key presses and typing text.autopy.bitmap: For capturing, saving, and manipulating screenshots.autopy.screen: For getting information about the screen size and resolution.
Practical Examples
Let's dive into some code. After installing autopy, you can run these examples.

Example 1: Basic Mouse Control
This script will move the mouse to the center of the screen, click, and then move it in a small square.
import autopy
import time
# Get the screen size
screen_width, screen_height = autopy.screen.size()
print(f"Screen size: {screen_width}x{screen_height}")
# 1. Move the mouse to the center of the screen
center_x = screen_width / 2
center_y = screen_height / 2
print("Moving mouse to the center...")
autopy.mouse.move(center_x, center_y)
time.sleep(1) # Wait for 1 second
# 2. Perform a left click
print("Performing a left click...")
autopy.mouse.click(autopy.mouse.Button.LEFT)
time.sleep(1)
# 3. Move the mouse in a small square
print("Moving mouse in a square...")
square_size = 50
autopy.mouse.move(center_x + square_size, center_y)
time.sleep(0.2)
autopy.mouse.move(center_x + square_size, center_y + square_size)
time.sleep(0.2)
autopy.mouse.move(center_x, center_y + square_size)
time.sleep(0.2)
autopy.mouse.move(center_x, center_y)
time.sleep(0.2)
# 4. Scroll the mouse
print("Scrolling up and down...")
autopy.mouse.scroll(10) # Scroll up
time.sleep(1)
autopy.mouse.scroll(-10) # Scroll down
Example 2: Basic Keyboard Control
This script will type a sentence, pressing the Enter key in between.

import autopy
import time
print("You have 3 seconds to focus on a text editor (like Notepad or VS Code)...")
time.sleep(3)
# 1. Type a string
print("Typing 'Hello from Python!'...")
autopy.key.type_string("Hello from Python!")
time.sleep(1)
# 2. Simulate pressing the Enter key
print("Pressing Enter...")
autopy.key.tap("RETURN") # 'RETURN' or 'ENTER' both work
time.sleep(1)
# 3. Type more, using key combinations
print("Typing 'Nice to meet you' with a shift key...")
autopy.key.type_string("Nice to meet you")
autopy.key.tap("SHIFT") # Hold shift
autopy.key.tap("a") # This will be a capital 'A'
autopy.key.key_up("SHIFT") # Release shift
autopy.key.type_string("utopy!")
Note: For special keys, use their names like RETURN, TAB, SPACE, SHIFT, CONTROL, ALT.
Example 3: Taking and Saving a Screenshot
This script will capture your entire screen and save it as a PNG file.
import autopy
import time
print("Taking a screenshot in 3 seconds...")
time.sleep(3)
# 1. Capture the screen
# This returns an autopy.bitmap.Bitmap object
bitmap = autopy.screen.capture()
# 2. Save the captured bitmap to a file
# The file will be saved in the same directory as your script
filename = "my_screenshot.png"
bitmap.save(filename)
print(f"Screenshot saved as '{filename}'")
Advanced Example: Drawing with the Mouse
This is a classic "hello world" for GUI automation. It will draw a simple smiley face on your screen using MS Paint (or a similar drawing program).
Instructions:
- Open MS Paint (or your preferred drawing app).
- Run the Python script below.
- Quickly click on the Paint canvas to give it focus.
import autopy
import time
import math
# --- Configuration ---
canvas_width, canvas_height = autopy.screen.size()
# Center of the screen where we'll draw
center_x = canvas_width / 2
center_y = canvas_height / 2
radius = 100 # Radius of the face
eye_radius = 10
eye_offset_x = 30
eye_offset_y = -30
mouth_radius = 60
mouth_start_angle = math.pi * 0.2
mouth_end_angle = math.pi * 0.8
# --- Drawing Logic ---
print("Drawing a smiley face in 3 seconds...")
print("Make sure your drawing app is open and focused!")
time.sleep(3)
# Move to the starting position (top of the head)
start_x = center_x
start_y = center_y - radius
autopy.mouse.move(start_x, start_y)
autopy.mouse.toggle(autopy.mouse.Button.LEFT, down=True) # Hold down the left mouse button
# Draw the face (a circle)
# We approximate a circle with many small line segments
num_segments = 100
for i in range(num_segments + 1):
angle = 2.0 * math.pi * i / num_segments
x = center_x + radius * math.cos(angle)
y = center_y + radius * math.sin(angle)
autopy.mouse.move(x, y)
time.sleep(0.01) # Small delay for smooth drawing
# Draw the left eye
left_eye_x = center_x - eye_offset_x
left_eye_y = center_y + eye_offset_y
autopy.mouse.move(left_eye_x, left_eye_y)
for i in range(num_segments + 1):
angle = 2.0 * math.pi * i / num_segments
x = left_eye_x + eye_radius * math.cos(angle)
y = left_eye_y + eye_radius * math.sin(angle)
autopy.mouse.move(x, y)
time.sleep(0.005)
# Draw the right eye
right_eye_x = center_x + eye_offset_x
right_eye_y = center_y + eye_offset_y
autopy.mouse.move(right_eye_x, right_eye_y)
for i in range(num_segments + 1):
angle = 2.0 * math.pi * i / num_segments
x = right_eye_x + eye_radius * math.cos(angle)
y = right_eye_y + eye_radius * math.sin(angle)
autopy.mouse.move(x, y)
time.sleep(0.005)
# Draw the mouth (a smile)
mouth_center_x = center_x
mouth_center_y = center_y + 20
autopy.mouse.move(
mouth_center_x + mouth_radius * math.cos(mouth_start_angle),
mouth_center_y + mouth_radius * math.sin(mouth_start_angle)
)
for i in range(int((mouth_end_angle - mouth_start_angle) * num_segments / (2 * math.pi)) + 1):
angle = mouth_start_angle + (2.0 * math.pi * i / num_segments) * ((mouth_end_angle - mouth_start_angle) / (2 * math.pi))
x = mouth_center_x + mouth_radius * math.cos(angle)
y = mouth_center_y + mouth_radius * math.sin(angle)
autopy.mouse.move(x, y)
time.sleep(0.01)
# Release the mouse button
autopy.mouse.toggle(autopy.mouse.Button.LEFT, down=False)
print("Done!")
Important Considerations and Caveats
- Permissions (macOS): Since High Sierra, macOS has increased security for accessibility features. You may need to grant your terminal or Python IDE permission in
System Settings>Privacy & Security>Accessibilityto allowautopyto control your mouse and keyboard. - Responsiveness: GUI automation can be fragile. If another window pops up or your computer slows down, the script might fail. Adding small
time.sleep()delays can make your scripts more robust
