vscode-code-runner icon indicating copy to clipboard operation
vscode-code-runner copied to clipboard

[Bug] python: package-import from an other folder above only works in terminal, not in output

Open MakusuB opened this issue 2 years ago • 3 comments

  • VS Code Version: 1.77.3
  • OS Version: Windows 10
  • Code Runner Version: v0.12.0

Describe the bug Importing a python-package from an other folder above the current file does only works in terminal. Compiled in Output-Window, it gives a ModuleNotFoundError.

To Reproduce

  1. workspace-folder structure:
  • test_package_import
    • .venv
    • .vscode
    • package
      • __init__.py
      • inner_package.py
    • projects
      • inner_project_main.py
    • main.py
  1. define a function "func" in inner_package.py
  2. in main.py, write from package.inner_package import func
  3. in inner_project_main.py from package.inner_package import func

Actual behavior In main.py the statement works in both, terminal and output (using "code-runner.runInTerminal": true or false to switch). But for the inner_project_main.py it only works in terminal. When I run it in output, I got an ModuleNotFoundError: No module named 'package'.

Expected behavior Should work identically in both files (like e.g. in PyCharm)

More Backround I have tried everything from https://stackoverflow.com/questions/50966876/vs-code-code-runner-doesnt-work-with-virtualenvs, but nothing worked for me.

Neither

"code-runner.executorMap": { "python": "$pythonPath $fullFileName" },

or

"code-runner.executorMap": { "python": "$pythonPath -u $fullFileName" },

don't change the behaviour.

Or is there anything I have missunderstood? It would by fantastic if that could be fixed (due to better Highlithing in the Output). Thank you very much!

MakusuB avatar Apr 28 '23 13:04 MakusuB

linux: "code-runner.executorMap": { "python": "export PYTHONPATH=$workspaceRoot && $pythonPath -u `$fullFileName" }

windows: "code-runner.executorMap": { "python": "set PYTHONPATH=$workspaceRoot && $pythonPath -u `$fullFileName" }

iknownoth1ng avatar May 11 '23 07:05 iknownoth1ng

Thank you very much for your answer. I asked ChatGPT a third time yesterday, after I also had problems with paths to import files and spent a lot of time the last 2 weeks looking for a general answer. But now I was able to phrase the question better and got the correct answer (which works for me on Windows): "code-runner.executorMap": { "python": "cd $dir && $pythonPath $fullFileName" }

In my humble opinion, your answer goes in the same direction, but I got a Module/FileNotFoundError again. However, now others with possibly the same problem have a number of options to find the right solution for themselves.

Thanks again!

MakusuB avatar May 11 '23 07:05 MakusuB

Similar here. only works with setting "code-runner.runInTerminal": true


And when tested with setting like:

{
    "python.venvPath": ".",
    // "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
        "python": "$pythonPath -m pip list",
    },
}

The output would simply be like:

/home/******/.venv/bin/python: No module named pip

Binly42 avatar Oct 15 '23 01:10 Binly42