cq-cli icon indicating copy to clipboard operation
cq-cli copied to clipboard

Model file/script unable to load python file from parent directory

Open justbuchanan opened this issue 9 months ago • 1 comments

I have a setup something like this:

# file /models/model.py

# add parent dir to path so we can import my_utils
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))

from my_utils import *

# build a model...
# file /my_utils.py

# define some helper functions...

This works fine when loading /models/model.py in cq-editor and when loading the model with python /models/model.py. However, when using cq-cli to render the model, I get NameError: name '__file__' is not defined. Did you mean: '__name__'? (note that __name__ is "cqgi").

I haven't investigated this much yet, but I think this has to do with the way cq-cli is loading the model script by reading it as a text file, then passing it to cqgi.parse().

Any suggestions for what the best/right approach is here? Is it reasonable for cq-cli to set the __file__ variable to the script file's path? Or should I be doing something like PYTHONPATH=<parent directory> cq-cli /models/model.py?

justbuchanan avatar Feb 21 '25 19:02 justbuchanan

CQGI can cause issues with some built-in variables like this, and as you note, reading the script as text can cause the issue as well.

I don't think it's unreasonable for cq-cli to artificially set the __file__ variable, but I have not investigated it.

jmwright avatar Feb 21 '25 20:02 jmwright