AccessDenied in psutil

psutil is an interesting Python package that provides us with valuable insights about running processes, their memory and CPU usage and many more key aspects for monitoring and profiling processes across all major platforms. Thus, it is an incredibly useful tool for system admins, developers and testers alike. Now one of my favourite methods is cmdline(). Defined in the package’s Process class, it yields the whole CLI command of a running process in array form. There is one important catch on Windows though that got me kinda by surprise: While iterating casually through all my processes I suddenly got an AccessDenied error.

What did I try?

For an advanced verification in a process management – related test case I wanted to print the executed CLI command in a log file, so basically this:

import psutil

for pc in psutil.process_iter():
    print(pc.cmdline())

But that will just throw a painful AccessDenied error at us. Uff.

Why did that happen?

The reason is that psutil is quite consequent: It really lists every running process. That means even SYSTEM or root processes . That’s okay, it might even be interesting in one case or another, and you can still access selected attributes like the name() of a root process, if you want. But for me, it doesn’t suffice. I want to see the full-fledged cmdline(), but I understand that SYSTEM processes are none of my business. Once I came to term with that fact, the solution was easy: We just skip them.

The solution

What I did was applying a try-except around the loop’s inner statement:

import psutil

for pc in psutil.process_iter():
    try:
        print(pc.cmdline())
    except psutil.AccessDenied:
        continue

The continue statement will make sure that the processes that I’m not supposed to see are happily skipped without hurting the rest of the program flow.

But what if I need to monitor foreign processes?

In that case, we would need to execute the script within the process owner’s user context. That might be a bit fiddly in Windows depending on the use case, but of course that’s still possible. Just remember to keep the try-except block, because there still will be processes you wouldn’t be allowed to see.

Mine, for example.

Conclusion

So far for today. I hope this little Q3A (quick question quick answer) could shed some light upon that surprising AccessDenied error. If you want to learn more about psutil, I strongly recommend the readthedocs page. Otherwise, if you want to see more quick tips, I have one more for Python about environment variables. That one covers Python’s environment variable handling. As an alternative, if you are – like me – into containers, here is a handy docker ps trick useful for monitoring tasks as well.

Happy coding!

Home » Q3A
Share it with:

Python Environment Variables: getenv() vs. environ[]

Last week, while running one of our functional test suites that relies on a specific environment variable, I forgot to set it—classic oversight. The result? An abrupt and unhelpful Python KeyError, offering little insight into what went wrong.

That experience raised two important questions:

  1. What options do I have to access environment variables in Python?
  2. Which method is the most reliable?

Let’s explore both and see which one fits your use case best.

Accessing Environment Variables with os.environ[key]

The first method uses a dictionary-like object, os.environ, which is pre-populated with all environment variables when the Python process starts. It’s a familiar and straightforward interface for directly retrieving values by their keys.

Let’s try it:

For python environment variables with os.environ[key], do the following: export MY_VAR="test", python3 -i, import os and os.environ["MY_VAR"]. This should yield 'test'.
Python environment variables with os.environ[key]

But what happens when we attempt to access a variable that hasn’t been set?

os.environ["UNSET_VAR"] # Raises KeyError

Unless we’ve wrapped it in a try-except block, this call will immediately raise a KeyError and stop your script. That’s exactly what happened during my test run.

Using os.getenv(key, default=None)

The second approach is more high-level and uses the os.getenv() function, which accepts the environment variable name and an optional default value. It’s safer to use because it won’t throw an error if the variable is missing.

Let’s test it:

For python environment variables with os.getenv(key, default), do the following: export MY_VAR="test", python3 -i, import os and os.getenv("MY_VAR"). This should yield 'test'. If you query MY_VAR2 instead, it should yield None. If you do os.getenv("MY_VAR2", "default"), it should yield default.
Python environment variables with os.getenv and default value handling

This method gives you more flexibility. If the environment variable isn’t set, os.getenv() simply returns None or a fallback default if provided. However, since the default return value is None, this approach can introduce subtle bugs if not handled carefully—especially if None is a valid value in your application logic. Always consider specifying an appropriate default value when using os.getenv().

Conclusion: Which One Should You Use?

We’ve explored both options—but which one is best for your use case?
Ultimately, the best choice depends on two key factors:

  1. Do you prefer low-level, strict access (with os.environ) or a more forgiving, high-level approach (os.getenv)?
  2. Do you want your code to fail fast with clear errors, or handle missing values more gracefully?

Both methods have their merits. Choose the one that aligns with your coding style and project needs.

That wraps up this quick dive into Python’s environment variable handling. Usually, I write more about Rust, like in my Cucumber Rust tutorial or my Rust module cheat sheet, but Python is my main language at work—so you can expect more content like this.

Also, with container technologies becoming increasingly central to my job, I’ll soon be sharing more tips—starting with a handy Docker ps command trick. Or if you’re in the mood to keep coding, check out my Java threads tutorial next.

Happy holidays, and merry Christmas, Eeve!

Home » Q3A
Share it with: