# name 'bpy' is undefined

In test.py I have the following line. It was taken from this page:

bpy.data.objects


Then I start blender from the bash prompt:

blender Rigged\ Hand.blend  --python test.py


The response on the terminal:

Traceback (most recent call last):
File "/home/sam/proj/Rigged Hand/a/setup.py", line 1, in <module>
bpy.data.objects
NameError: name 'bpy' is not defined


Even though blender has opened my file, I can open the python prompt inside blender and type the same thing; I get the response <bpy_collection[5], BlendDataObjects> as expected.

Why can I not use bpy in the script which I invoked from the command line?

• You have to "import bpy" first. – Doyousketch2 Apr 16 '17 at 7:55
• @Doyousketch2 Okay, that was unobvious, because you don't have to import bpy from the python console inside Blender. But post it as an answer, and I'll accept it. – Wilson Apr 16 '17 at 13:04