microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.29k stars 1.17k forks source link

REPL doesn’t work when the python file is in nested folder #23656

Open rebornix opened 2 months ago

rebornix commented 2 months ago

Type: Bug

My python file is in a sub folder nested/test.py and there is a csv file next to it nested/data.csv, however if I try to load the file with relative path "data.csv", it only works if I use "nested/data.csv".

I was expecting the relative path to just work. However if this is as designed, I would love to have a setting to set the cwdto the parent folder of the python file when using it in REPL.

Extension version: 2024.9.11731014 VS Code version: Code - Insiders 1.91.0-insider (Universal) (63d06667600a2c40245a0ab1f9e2ea816d2fe849, 2024-06-21T10:24:54.816Z) OS version: Darwin arm64 23.5.0 Modes:

User Settings

``` languageServer: "Pylance" testing • promptToConfigure: false ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |Dev Containers|ms-vscode-remote.remote-containers|0.373.0| |EditorConfig for VS Code|EditorConfig.EditorConfig|0.16.4| |ESLint|dbaeumer.vscode-eslint|3.0.10| |Extension Test Runner|ms-vscode.extension-test-runner|0.0.9| |Flame Chart Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-flame|1.0.9| |GitHub Copilot|GitHub.copilot|1.205.0| |GitHub Copilot Chat|GitHub.copilot-chat|0.17.2024062101| |GitHub Issue Notebooks|ms-vscode.vscode-github-issue-notebooks|0.0.130| |GitHub Pull Requests|GitHub.vscode-pull-request-github|0.91.2024062004| |GitHub Theme|GitHub.github-vscode-theme|6.3.4| |GitLens — Git supercharged|eamodio.gitlens|2024.6.2205| |JavaScript Debugger|ms-vscode.js-debug|1.90.0| |JavaScript Debugger Companion Extension|ms-vscode.js-debug-companion|1.1.2| |Jupyter|ms-toolsai.jupyter|2024.6.2024062001| |Jupyter Cell Tags|ms-toolsai.vscode-jupyter-cell-tags|0.1.9| |Jupyter Keymap|ms-toolsai.jupyter-keymap|1.1.2| |Jupyter Notebook Renderers|ms-toolsai.jupyter-renderers|1.0.18| |Jupyter Slide Show|ms-toolsai.vscode-jupyter-slideshow|0.1.6| |Markdown Checkboxes|bierner.markdown-checkbox|0.4.0| |Markdown Preview Github Styling|bierner.markdown-preview-github-styles|2.0.4| |Mocha Test Explorer|hbenl.vscode-mocha-test-adapter|2.14.1| |Node.js Notebooks (REPL)|donjayamanne.typescript-notebook|2.0.6| |Nord|arcticicestudio.nord-visual-studio-code|0.19.0| |PR Pinger|jrieken.vscode-pr-pinger|0.0.6| |Prettier - Code formatter|esbenp.prettier-vscode|10.4.0| |Pylance|ms-python.vscode-pylance|2024.6.101| |Python|ms-python.python|2024.9.11731014| |Python Debugger|ms-python.debugpy|2024.7.11591012| |Ruff|charliermarsh.ruff|2024.28.0| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.9| |Test Adapter Converter|ms-vscode.test-adapter-converter|0.1.9| |Test Explorer UI|hbenl.vscode-test-explorer|2.21.1|
System Info |Item|Value| |---|---| |CPUs|Apple M3 Max (14 x 2400)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|3, 2, 2| |Memory (System)|36.00GB (0.07GB free)| |Process Argv|--enable-proposed-api rebornix.anyllm --enable-proposed-api rebornix.lulu --crash-reporter-id 1ce5da70-91d5-46fb-a706-70ff08933f9c| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscod805:30301674 vsaa593:30376534 py29gd2263:31024238 c4g48928:30535728 a9j8j154:30646983 962ge761:30841072 pythongtdpath:30726887 welcomedialog:30812478 pythonnoceb:30776497 asynctok:30898717 dsvsc013:30777762 dsvsc014:30777825 dsvsc015:30821418 pythonregdiag2:30926734 pythonmypyd1:30859725 h48ei257:31000450 pythontbext0:30879054 accentitlementst:30870582 dsvsc016:30879898 dsvsc017:30880771 dsvsc018:30880772 cppperfnew:30980852 pythonait:30973460 jchc7451:31067543 showvideot:31016890 chatpanelt:31014475 g316j359:31013175 a69g1124:31018687 dvdeprecation:31040973 dwnewjupyter:31046869 impr_priority:31057980 nativerepl2:31071685 ```
anthonykim1 commented 2 months ago

This is weird - does print(os.getcwd()) yield / for you as well? Ideally it should be the workspace path.. trying to think of why we only get this in the new native repl vs. terminal repl.

saurabhbikram commented 1 month ago

This is weird - does print(os.getcwd()) yield / for you as well? Ideally it should be the workspace path.. trying to think of why we only get this in the new native repl vs. terminal repl.

I can confirm i have the same issue, and print(os.getcwd()) does indeed yield /

setting "python.REPL.sendToNativeREPL":false i get the correct working directory in the 'old' terminal.

Anstetten commented 1 month ago

This is weird - does print(os.getcwd()) yield / for you as well? Ideally it should be the workspace path.. trying to think of why we only get this in the new native repl vs. terminal repl.

I am facing the same issue. It spawns in the wrong working directory. It "thinks" it is in the right directory (provides autocomplete for stuff like import backend.components.blabla) but importing anything fails.

anthonykim1 commented 1 month ago

Hey folks, good news here: working to get this resolved this month :) As in directory context for REPL should be set properly when users launch REPL, similar to how terminal is able to pull up current working directory. If user is inside multi-root directory we would show options for user to select which directory to use, but otherwise in a more common one workspace directory, we would automatically just set that as a cwd.

Will let you all know once I merge in the changes so you can try it all out. Thanks!

anthonykim1 commented 1 month ago

@rebornix As far as settings to straight up let the users set their cwd for REPL, I think that would be feature request. I think the default directory/workspace behavior should be same as Terminal as a baseline regardless.

Not too sure about the setting as folks could just

os.chdir(new_dir)

and set their cwd if they want to override the default workspace directory setting.

anthonykim1 commented 1 month ago

Let's track the bug here: https://github.com/microsoft/vscode-python/issues/23821
and I'll change this to feature request as in wanting for explicit setting to set directory for REPL even in single workspace scenario.

github-actions[bot] commented 1 month ago

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

anthonykim1 commented 1 month ago

Hi folks. As promisedm we are NOW setting the directory properly for 3 different scenarios as a result of https://github.com/microsoft/vscode-python/pull/23912 (got merged about two days ago), so please give this a try after updating to the latest pre-release version of Python extension.

3 different scenario should cover:

  1. Single workspace scenario
  2. Multi workspace scenario (we will give you quick selection option)
  3. No workspace scenario (empty vs code)
print(os.getcwd())

should yield expected workspace directory, following same trend as terminal.

As far as OPTION to more explicitly choose directory for REPL launch (if you think the result of PR is not enough), please give this issue an upvote so we can see the interest.

FYI

os.chdir('/path/to/your/desired/directory') 

will also allow you to easily navigate through whichever subdirectory or directory of your chose if you want.