svaante / dape

Debug Adapter Protocol for Emacs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python sys.path does not include the project root when running in a subfolder

zhenhua-wang opened this issue · comments

Thank you for this very useful package!

I find this behavior when dape is started from an sub-folder in a project. Although cwd and command-cwd point to the project root, the sys.path variable in python does not include the project root, instead it has the current directory (the subfolder). I wonder if I change this and include the project root in sys.path for dape repl?

Hey, sorry for the late response.
I think I have found the issue, if you specify the :program using an absolute path instead of an relative like the default configuration sys.path contains the expected file path. This is an debugy bug, one would expect cwd to affect the value of sys.path.

bild

Thanks for checking. Sorry, I didn't describe my situation clearly. I have a test.py in a subfolder called test. When I run dape on this test.py, Sys.path points to the test subfolder. Since the test file imports functions from lib/lib.py, it needs to be run from the root folder to correctly resolve the path. My current fix for this is to add os.getcwd() to Sys.Path manually at the beginning of the test file. I wonder if there is a way in dape to make it point to the root folder as well?

My project structure looks like this.

root/
    lib/
        lib.py
    test/
        test.py
    main.py

Ah I see, this has nothing to do with dape but with how sys.path works, AFAIK cwd has no affect on sys.path.

  • Use env variable PYTONPATH like debugpy :program "main.py" :env (:PYTHONPATH dape-cwd) to add root folder to sys.path
  • symlink your python package to site-packages with pip and editable install see link
  • Use unittest module debugpy-module :module "unittest" :args ["test.test"] (make sure that root/test/__init__.py exists)

Thanks for the explain! Your first solution works great for me.