Opening a processο
Everything starts with OpenProcess β the unified entry point for all three
operating systems. Under the hood it returns the right backend
(WindowsProcess, LinuxProcess or MacProcess), but you never have to care:
the API is identical.
Basic usageο
from PyMemoryEditor import OpenProcess
with OpenProcess(process_name="notepad.exe") as process:
print("PID:", process.pid)
The with block automatically calls process.close() when you leave it,
releasing any OS handle the backend acquired.
Identify the process β by name or PIDο
You can identify the target in two ways:
| Argument | What it does |
|---|---|
process_name="notepad.exe" |
Looks up the PID by name. Most natural way for desktop apps and games. |
pid=1234 |
Skip the name lookup and attach to a known PID directly. |
If you pass both, pid wins. If you pass neither, TypeError is raised.
Name matching: case and partial matchesο
By default, process_name matching is:
Case-sensitive on Linux and macOS.
Case-insensitive on Windows (matches the OS convention).
Exact: the name has to match the executable name exactly.
You can override both with keyword arguments:
# Match "chrome.exe", "ChroMe.EXE", "chrome" β anywhere case-insensitively.
OpenProcess(process_name="chrome", case_sensitive=False, exact_match=False)
Argument |
Type |
Default |
Effect |
|---|---|---|---|
|
|
platform-dependent |
When |
|
|
|
When |
If a partial match resolves to more than one process, an
AmbiguousProcessNameError is raised β pick one PID from the listed candidates
and pass it via pid= instead.
Permissions (Windows-only)ο
Only the Windows backend accepts a permission= mask β it maps directly to the
PROCESS_* access rights
of OpenProcess. The default opens a read + write handle:
# Default β same as:
OpenProcess(
process_name="notepad.exe",
permission=(
ProcessOperationsEnum.PROCESS_VM_READ
| ProcessOperationsEnum.PROCESS_VM_WRITE
| ProcessOperationsEnum.PROCESS_VM_OPERATION
| ProcessOperationsEnum.PROCESS_QUERY_INFORMATION
),
)
For a read-only handle (less powerful but useful for static analysis):
from PyMemoryEditor import OpenProcess, ProcessOperationsEnum
OpenProcess(
process_name="notepad.exe",
permission=ProcessOperationsEnum.PROCESS_VM_READ
| ProcessOperationsEnum.PROCESS_QUERY_INFORMATION,
)
For full control:
OpenProcess(
process_name="notepad.exe",
permission=ProcessOperationsEnum.PROCESS_ALL_ACCESS,
)
π§π On Linux and macOS
The permission argument is ignored β those platforms govern access via
ptrace_scope (Linux) or Mach entitlements (macOS). Passing a value emits a
UserWarning so a Windows-shaped mask doesnβt disappear silently.
See Platform Notes for details.
Closing the handleο
When youβre done, close the process:
process.close()
Or use the context manager (recommended):
with OpenProcess(pid=1234) as process:
...
# process is closed here, even if an exception was raised
Once closed, any further call raises ClosedProcess.
Common errorsο
Exception |
Meaning |
|---|---|
|
No process matches the given |
|
The given |
|
More than one process matches a partial name β pick a PID from |
|
The OS denied access β usually fixable; see Troubleshooting. |
|
Operation attempted on an already-closed handle. |
All exceptions inherit from PyMemoryEditorError, so you can catch them
collectively:
from PyMemoryEditor import OpenProcess, PyMemoryEditorError
try:
with OpenProcess(process_name="game.exe") as process:
...
except PyMemoryEditorError as e:
print("Failed to open process:", e)