Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CQ Editor cannot find files in module search path #439

Open
lenianiva opened this issue Jun 15, 2024 · 4 comments
Open

CQ Editor cannot find files in module search path #439

lenianiva opened this issue Jun 15, 2024 · 4 comments

Comments

@lenianiva
Copy link

lenianiva commented Jun 15, 2024

Consider a project setup like this:

/
|- pyproject.toml
|- poetry.lock
|- mylib
    |- __init__.py
    |- mystery.py
|- example
    |- example.py

where

# mystery.py

def mystery():
    return Cq.Workplane().box(10, 5, 5)

in a poetry shell environment, this succeeds:

(mylib-3.12) [example/] $ python3 -c "import mylib.mystery"

but if example.py contains any reference to import mylib.mystery, loading it up in cq-editor will fail, unless the script starts with this:

# example.py
import os, sys, os.path

root = os.path.dirname(os.getcwd())
print(root)

sys.path.append(root)

import mylib.mystery

The spooky thing is after this part of the script is loaded into cq-editor, commenting it out would not reintroduce the import error. Is there a way for cq-editor to recognize the current venv so it can load libraries referenced by cad scripts?

@lenianiva lenianiva changed the title CQ Editor cannot find current project CQ Editor cannot find files in module search path Jun 15, 2024
@lenianiva
Copy link
Author

lenianiva commented Jun 15, 2024

Addendum: This seems to be a problem with sys.path

The sys.path outside of cq-editor is

(mylib-3.12) [example/] python3 -c "import sys; print(sys.path)"
['', '/usr/lib/python312.zip', '/usr/lib/python3.12', '/usr/lib/python3.12/lib-dynload', '/home/aniva/.cache/pypoetry/virtualenvs/nhf-Q4XoAPRK-py3.12/lib/python3.12/site-packages', '/home/aniva/Projects/mylib']

The sys.path inside cq-editor is

['/opt/cq-editor-bin/base_library.zip', '/opt/cq-editor-bin', '/opt/cq-editor-bin/IPython/extensions']

I think it would be nice if there is an option to inherit sys.path from the current environment

@adam-urbanczyk
Copy link
Member

adam-urbanczyk commented Jun 16, 2024

You seem to be using the packaged CQ-editor, so it has no relation with your venv. Does using preferences->debugger->Add script dir to path solve your issue?

Reading your question for the second time, what I mention above won't help. For now you could install your module in the (conda) env shipped with the installer.

@lenianiva
Copy link
Author

lenianiva commented Jun 18, 2024

You seem to be using the packaged CQ-editor, so it has no relation with your venv. Does using preferences->debugger->Add script dir to path solve your issue?

Reading your question for the second time, what I mention above won't help. For now you could install your module in the (conda) env shipped with the installer.

I found a workaround which is to put a sentinel file at the project root like this:

import mylib.mystery

obj = mylib.mystery.mystery()
show_object(obj)

but when I made changes to the object, cq-editor cannot pick it up even if I click Render. This is not a major problem since I can just copy whatever object I'm working on to this sentinel file.

Edit: and the workaround of this is to either use the new nightly version (enabling "Reload imported modules") or add the following in the sentinel file

# sentinel.py

...
import importlib
importlib.reload(mylib.mystery)
...

Then

cq-editor sentinel.py

will react to changes in sentinel.py and will update correctly upon pressing Render (F5).

@adam-urbanczyk
Copy link
Member

So I think the conclusion here is that CQ-editor has no "ope dir" or "open project" functionality. Might be a nice thing to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants