microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.25k stars 275 forks source link

Requires the ability to systematically change file paths in Jupyter #15795

Open jeanne150 opened 1 week ago

jeanne150 commented 1 week ago

I want to change the file path to the parent of the file that is currently running. The solution now is to use the Python OS module to change the working directory. Use the os.chdir() of the os module to manually set the file path. It's like this:

import os 
os.chdir(os.path.expanduser("~"))

I'd like to have a way to do this systematically instead of manually modifying it every time.

jeanne150 commented 1 week ago

Related on StackOverflow:here

DonJayamanne commented 1 week ago

Thank you for filing this issue, please can you provide more details here. I've checked stack overflow and that doesn't seem to be realted, i.e. I do not see anyone in that issue trying to change the cwd to the user folder, in your case you are trying to change the directory to the user folder and not the parent folder.

Please can you share more information by roviding details that is missing in the issue template

collavoce commented 1 week ago
DonJayamanne commented 1 week ago

being run locally as an interactive window

What commands are you using exactly here. Or how did you achieve this?

Are you running Interactive Window or a Notebook, the file you linked is a Notebook Document. Please can you share a screen recording using gifcap.dev or the like

DonJayamanne commented 1 week ago

This works as expected for me.

Screenshot 2024-06-28 at 11 28 58

Here is my setting

Screenshot 2024-06-28 at 11 29 15

collavoce commented 6 days ago

vscode_settings_jupyter_root vscode_notebook_dir_path vsocde_interactive_editor_dir_path

I'm running an interactive window but same behavior when running the notebooks. To generate the interactive window, I opened the notebook file, then command palette and chose "Jupyter: Export to Python Script". Per the attached screenshots, I have the same Jupyter File Root setting as you but the working directory that's being used when the notebook is run is different than the actual working directory the files are in. I've tried with different virtual environments as interpreters and notebooks in other folders but they all return the same directory path, even those that don't roll up to it.

DonJayamanne commented 6 days ago

This is weird, hopefully the logs will share more light on this issue. Thank you for getting back and thank you for your patience, I'm certain we can get this addressed.

collavoce commented 6 days ago

Under the workspace tab, same Jupyter root tag -- no difference

I added the verbose option (and the settings.json file with that new setting is showing up under /Users/jm/workspace/learning/Python) and ran both Developer: Reload Window and Python: Clear Cache and Reload window.

Same behavior as before when running the cell containing os.getcwd() ("/Users/jm/workspace/learning/Python") -- and this was the output in the console:

09:37:33.786 [info] Disposing kernel .jvsc74a57bd0d0fb27f5aef107e0a3b82a4c76b7f537ca4a5f6b97d318ca945c3103546b5d9c./Users/~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12./Users/~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12.-m#ipykernel_launcher for notebook /Interactive-1.interactive due to selection of another kernel or closing of the notebook 09:37:33.787 [info] Dispose Kernel '/Interactive-1.interactive' associated with '~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097/Chapter 2/Ch2_challenge.py' 09:37:33.794 [info] Dispose Kernel '/Interactive-1.interactive' associated with '~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097/Chapter 2/Ch2_challenge.py' 09:39:19.459 [info] Starting interactive window for resource '~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097/Chapter 2/Ch2_challenge.py' with controller '.jvsc74a57bd0d0fb27f5aef107e0a3b82a4c76b7f537ca4a5f6b97d318ca945c3103546b5d9c./Users/~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12./Users/~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12.-m#ipykernel_launcher (Interactive)' 09:39:19.592 [info] Starting Kernel (Python Path: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12, Conda, 3.12.3) for '/Interactive-1.interactive' (disableUI=false) 09:39:19.595 [info] Process Execution: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12 -c "import ipykernel; print(ipykernel.version); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.file)" 09:39:19.601 [info] Process Execution: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12 -m ipykernel_launcher --f=/Users/~/Library/Jupyter/runtime/kernel-v2-491522L0D46pqWO1h.json

cwd: //Users/~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097/Chapter 2 09:39:21.099 [info] Kernel successfully started 09:39:21.101 [info] Generated code for 1 = with 5 lines 09:39:21.106 [info] Generated code for 2 = with 2 lines

DonJayamanne commented 5 days ago

Same behavior as before when running the cell containing os

Please can you share all of the output, There are some parts that I need, e.g. we calculate the working directory and set it up, spawn the processes, etc. You can see the information and feel free to strip out any sensitive information, we try not to include the user names and the like. If you do strip anything out, then do let me know what was replaced, thanks.

Please ensure you re-load VS Code and then try this, I would like to get some clean (new logs).

collavoce commented 4 days ago

Steps taken: 1) In settings, Jupyter Logging level is set to "info" with "verbose logging for tasks" enabled.

2) reloaded the window and this appears in the console under "output": Visual Studio Code (1.90.2, undefined, desktop) Jupyter Extension Version: 2024.5.0. Python Extension Version: 2024.8.1. Pylance Extension Version: 2024.6.1. Platform: darwin (arm64). Workspace folder ~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097, Home = /Users/jm

3) ran this in a notebook (and interactive editor):

import os
print(os.getcwd())

The resulting output was "/Users/jm/workspace/learning/Python"

The output in the console was:

09:45:09.350 [info] Starting interactive window for resource '~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097/Chapter 2/Ch2_challenge.py' with controller '.jvsc74a57bd0d0fb27f5aef107e0a3b82a4c76b7f537ca4a5f6b97d318ca945c3103546b5d9c./Users/~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12./Users/~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12.-m#ipykernel_launcher (Interactive)'
09:45:09.511 [info] Starting Kernel (Python Path: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12, Conda, 3.12.3) for '/Interactive-1.interactive' (disableUI=false)
09:45:11.222 [info] Process Execution: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12 -m pip list
09:45:11.292 [info] Process Execution: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12 -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
09:45:11.294 [info] Process Execution: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12 -m ipykernel_launcher --f=/Users/~/Library/Jupyter/runtime/kernel-v2-70162Ov4Fy8nwMJKl.json
    > cwd: //Users/~/workspace/learning/Python/nlp/src/advanced-nlp-with-python-for-machine-learning-3807097/Chapter 2
09:45:13.098 [info] Kernel successfully started
09:45:13.102 [info] Process Execution: ~/opt/anaconda3/envs/nlp_linkedin_prof_stripling/bin/python3.12 /Users/~/.vscode/extensions/ms-toolsai.jupyter-2024.5.0-darwin-arm64/pythonFiles/printJupyterDataDir.py
09:45:13.103 [info] Generated code for 1 = <ipython-input-1-9e96808d78df> with 5 lines
09:45:13.107 [info] Generated code for 2 = <ipython-input-2-9f0f8fa44d85> with 2 lines

(see screen shot)

Should I be looking elsewhere for the verbose logs other than the console?

jupyter_output_with_verbose_logging