Writing code across multiple files
-
I’m having issues importing code from one file into another. I have a file,
blob.py
, which contains a classBlob
.Blob
has a method calleddraw
, which contains the linefill(*self.color)
.In another file,
post.py
, I want to useBlob
s, so I includedfrom blob import Blob
. When I runpost.py
in DrawBot, I get an error when I callBlob.draw
, saying that the namefill
is not defined. Why is this? How can I get files to know about DrawBot functionality when they are not directly run within DrawBot?
-
In blob.py, import Drawbot with:
import drawBot
When you use a Drawbot function in blob.py, add ‘drawBot’ before the function:
drawBot.fill(*self.color)
-
Thank you, that fixed it!
It seems like I need to quit and reopen DrawBot for any changes in blob.py to be seen — is there any easier way?
-
You can reload the module by using reload(). See an example in this post of @gferreira in this (highly relevant) topic Import python file.