I cannot render an animation with CUDA
The -t option is to specify how many threads are used for rendering, this should be followed by a number but the -a you have following it will give you an error.

The main issue you have with your command is that blender executes the arguments in the order they are given. Following it through means that blender starts in background mode, loads mainfile.blend, sets the engine to cycles, incorrectly sets the threads, renders an animation, opens the file //file then runs the script script.py.

What should work for you is -

blender -b mainfile.blend -E CYCLES -P script.py


If you are adding //file to specify the output file for the render, then you need to put -o in front of it (and add a file extension) and if it is a folder then also add a trailing slash -o //test_renders/.

You may also want to remove the render line from your script and use -f &lt;frame number&gt;. Note that using -f to render a specific frame from the command line will save the image to file so you don't need the render step in your script.

blender -b mainfile.blend -E CYCLES -P script.py -o //file.jpg -f 1