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

python.dataScience.notebookFileRoot needs an option to launch with CWD = current file path #3605

Closed
clamydo opened this issue Feb 19, 2019 · 18 comments
Assignees

Comments

@clamydo
Copy link

clamydo commented Feb 19, 2019

Environment data

  • VS Code version: 1.31
  • Extension version (available under the Extensions sidebar): 2019.1.0
  • OS and version: Debian Stretch
  • Python version (& distribution if applicable, e.g. Anaconda): 3.5.3
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): N/A
  • Relevant/affected Python packages and their versions: N/A

Expected behaviour

Having a Python script with cell markers #%%, I'd expect that setting python.dataScience.notebookFileRoot = ${fileDirname} makes the interactive session start in the directory the file resides.

Maybe I misunderstand that setting? At least, it is what calculateWorkingDirectory is supposed to do, isn't it?

Also see this Stack Overflow question

Actual behaviour

The interactive session starts from the workspace directory.

Steps to reproduce:

Having a structure like this

workingDir
|- foo
   |- file.py

and a file.py:

#%%
import os
print(os.getcwd())

the output in the interactive session is the absolute path of the workingDir.

@IanMatthewHuff
Copy link
Member

@fkjogu The notebookFileRoot setting does do some, but not all of what you are looking for. The VSCode variables are specific to task and debug configuration files (launch.json and task.json). We've specifically added code to recognize ${workspaceFolder} for our settings page but we don't recognize other VSCode variables in there. So you can put a specific path in there (and save it at the workspace level to affect only that folder) but you can't use ${fileDirname} in there to always get the path of the file.

While we don't currently support that I do think that this could be a reasonable option (currently vscode defaults to the workspace root as cwd, which we copied while Jupyter defaults to the path of the file) so I'm keeping this issue open to look into that.

@IanMatthewHuff IanMatthewHuff changed the title python.dataScience.notebookFileRoot is not respected in the interactive session python.dataScience.notebookFileRoot needs an option to launch with CWD = current file path Feb 20, 2019
@IanMatthewHuff
Copy link
Member

I did change the title quick to make it more clear the work that we are considering.

@clamydo
Copy link
Author

clamydo commented Feb 25, 2019

What is the reason behind not regocognizing the built-in variables? I kinda expected them to work everywhere in the settings.

@IanMatthewHuff
Copy link
Member

@fkjogu I'm not fully sure on the reasoning for VSCode as a whole not supporting this. But it is specifically mentioned here:

https://code.visualstudio.com/docs/editor/variables-reference#_is-variable-substitution-supported-in-user-and-workspace-settings

They are not created for use in the settings files. For the python extension we just specifically had use for ${workspaceFolder} so we coded up manually tracking the current workspace folder as part of our extension so that we could use it in our settings.

@wn0x00
Copy link

wn0x00 commented Jul 1, 2019

it always get first dir when there are many dir,but set "python.dataScience.notebookFileRoot": "${workspaceFolder}/dd",it will change dd dir.

@contang0
Copy link

contang0 commented Aug 7, 2019

While we don't currently support that I do think that this could be a reasonable option (currently vscode defaults to the workspace root as cwd, which we copied while Jupyter defaults to the path of the file) so I'm keeping this issue open to look into that.

Yes please. When working interactively, I always want the kernel to start in the directory of the file I'm executing. If it is starting elsewhere it will mess up my imports unless I add file's directory to PATH manually..

@simeneide
Copy link

yes please(!)

I love to work with python files directly instead of the notebook format, but Ive multiple times forgot to remove the changedir commands at the start. Or if Im starting a new python file I need to remember the command.

Could a possible fix be something like "if opening interactive window from a file (through shift+enter), do the folder changing command as the first command"?

@rholland
Copy link

yes - this would be helpful

@Ir1d
Copy link

Ir1d commented Sep 22, 2019

+1 on this, launching with current file path wont make imports a mess

possible related #3141

@Ir1d
Copy link

Ir1d commented Sep 22, 2019

image
deleting the contents in this block seems to work for me (inside a workspace folder)

@contang0
Copy link

contang0 commented Sep 27, 2019

Most likely it only worked for you by chance. The kernel seems to start in the directory of the project that appears first in your explorer. Did you only have one project?

For me it still doesnt work, with or without variables set in Notebook File Root.

@rchiodo rchiodo self-assigned this Oct 1, 2019
@DonJayamanne DonJayamanne self-assigned this Oct 4, 2019
@rchiodo
Copy link
Contributor

rchiodo commented Oct 24, 2019

This should be in our current release.

@rchiodo rchiodo closed this as completed Oct 24, 2019
@onietosi
Copy link

Could you confirm once this is released and what the exact setting should be to launch with cwd? It doesn't seem to work yet in version 1.39.2 using ${fileDirname}.

@rchiodo
Copy link
Contributor

rchiodo commented Oct 30, 2019

1.39.2 is actually the VS code version. You need to look here to see what version of the python extension you have:

image

2019.10.44104 should have this fix in it.

You can verify if it works by doing something like so:

#%%
import os
print(os.getcwd())

@Alalalalaki
Copy link

I change "python.dataScience.notebookFileRoot" to ${fileDirName} under version 44104 but os.getcwd() still shows workspace folder.

@onietosi
Copy link

onietosi commented Nov 4, 2019

It seems to work as expected (with v2019.10.44104) when launching the interactive window for the first time since opening VS Code. But if I want to then use the interactive window from a python file in another folder, the root directory does not change, even after closing the interactive window tab.

For the root directory to change to the new cwd you have to interrupt/restart the kernel (or close VS Code).

@rchiodo
Copy link
Contributor

rchiodo commented Nov 4, 2019

Yes we don't restart the kernel when closing the interactive window. Maybe we should. We do that for notebooks.

@rchiodo
Copy link
Contributor

rchiodo commented Nov 4, 2019

We also have a work item to allow 1 interactive window per file started. That would also alleviate this problem.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 11, 2019
@microsoft microsoft unlocked this conversation Nov 14, 2020
@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-python Nov 14, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests