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


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>
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?

  • 5
    $\begingroup$ You have to "import bpy" first. $\endgroup$ Apr 16, 2017 at 7:55
  • $\begingroup$ @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. $\endgroup$ Apr 16, 2017 at 13:04

1 Answer 1


"import bpy" first.

They automatically import that module in the console as a convenience,
so you don't have to type it in every time. -cheers


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .