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$ – Doyousketch2 Apr 16 '17 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$ – Wilson Apr 16 '17 at 13:04

"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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.