Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

Switch off the GPU in a Python script

Here’s a simple piece of code which effectively directs a Python script to use the CPU rather than the GPU.

I develop Python code on a Windows Subsystem for Linux (WSL2) environment that has access to my machine’s GPU via CUDA. For one application, I needed to disable the GPU, but was having difficulty.

The following code, placed at the beginning of my script, ensured that the GPU was not used:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

Forum posts suggest that this is a “messy” approach, but it gets the job done for my application.

   

Comments

No comments have yet been submitted. Be the first!

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.