简体   繁体   English

jupyter notebook 的当前工作目录设置为 vscode 中的临时文件夹

[英]Current working directory for jupyter notebook sets to temp folder in vscode

I'm trying to set current working directory (CWD) to edited file location for Jupyter Notebook in VS Code.我正在尝试将当前工作目录 (CWD) 设置为 VS Code 中 Jupyter Notebook 的已编辑文件位置。 I use ${fileDirname} in python.dataScience.notebookFileRoot setting.我在python.dataScience.notebookFileRoot设置中使用${fileDirname} However it uses temporary folder as ${fileDirname} instead of original file folder.但是它使用临时文件夹作为${fileDirname}而不是原始文件夹。

Same issue was discussed couple times already (eg https://stackoverflow.com/a/54794976/12488601 ) with tried solution pointed out.同样的问题已经讨论了几次(例如https://stackoverflow.com/a/54794976/12488601 )并指出了尝试过的解决方案。

Here is example of cwd:以下是 cwd 的示例:

os.getcwd()
.. 'C:\\Users\\MjH\\AppData\\Local\\Temp\\1f6cc207-562f-4ae1-8754-e2013ae2c12d'

While expected result is C:\\Workspace\\Project.而预期的结果是 C:\\Workspace\\Project。

So use of ${fileDirname} does not work in my case.所以在我的情况下使用${fileDirname}不起作用。 I use following ad-hoc solution, which, obviously, won't update if file is moved.我使用以下临时解决方案,显然,如果文件被移动,它不会更新。

import sys
import os
sys.path.insert(0, r'C:\workspace\project')
os.chdir(sys.path[0])

Now I'm trying to understand three things:现在我试图理解三件事:

  1. is my case a unique one?我的情况是独一无二的吗?
  2. if it's general issue, is there a feature request/issue submitted for VS Code to address it?如果是一般问题,是否有提交给 VS Code 的功能请求/问题来解决它?
  3. is there a better ad-hoc solution?有更好的临时解决方案吗?

VS Code version: Code 1.40.2 (f359dd6, 2019-11-25T14:54:45.096Z) VS Code 版本:Code 1.40.2 (f359dd6, 2019-11-25T14:54:45.096Z)
OS version: Windows_NT x64 10.0.17763操作系统版本:Windows_NT x64 10.0.17763 设置

截至 2021 年 1 月,在我的设置中添加以下行有助于解决问题

"jupyter.notebookFileRoot": "${workspaceFolder}",

I'm also affected by this issue.我也受到这个问题的影响。 Here is how I solved it.这是我解决它的方法。

Using the configuration parameter of ${fileDirName} in Python > DataScience: Notebook File Root , has this effect as I could check in my environment.Python > DataScience: Notebook File Root 中使用${fileDirName}的配置参数,具有这种效果,因为我可以检查我的环境。

If I open an Python Interactive Window using the commands Ctrl+Shift+P > Python:Show Python Interactive Window , and then run:如果我使用命令Ctrl+Shift+P > Python:Show Python Interactive Window打开Python 交互式窗口,然后运行:

import os
os.getcwd()

The output is a random temporal folder.输出是一个随机的时间文件夹。 So I couldn't do imports cause my local relative modules where not found.所以我不能做导入导致我的本地相关模块找不到。

However, If instead of opening directly a fresh Python Interactive Window I run a cell of code of any of my python files但是,如果不是直接打开一个新的 Python 交互式窗口,而是运行我的任何 Python 文件的代码单元格

#%%
print("Some string here")

and then find the current working path, it changes to the fileDirName as expected.然后找到当前的工作路径,它按预期更改为fileDirName Then any interaction with the interactive window will perform correctly (and my next imports will perform correctly).然后与交互式窗口的任何交互都将正确执行(并且我的下一个导入将正确执行)。

To avoid this behaviour and because I would like to perform operations within my workspaceFolder I changed Jupyter: Notebook File Root to ${workspaceFolder} .为了避免这种行为,并且因为我想在我的 workspaceFolder 中执行操作,我将Jupyter: Notebook File Root更改为${workspaceFolder} In this case opening a new fresh Python Interactive Window, will set the current path automatically to the workspaceFolder, with and without the need to execute any python cell.在这种情况下,打开一个新的 Python 交互式窗口,将自动将当前路径设置为工作空间文件夹,无论是否需要执行任何 python 单元格。

I think that, when using ${fileDirName} , it would be good to set the current working open file folder as the working path of the Python Interactive Window (Jupyter) instead of the temporal random folder (In the case of opening without executing a specific cell), but doesn't look like it behaves like that.我认为,当使用${fileDirName} ,最好将当前工作打开的文件夹设置为 Python Interactive Window (Jupyter) 的工作路径而不是时间随机文件夹(如果打开而不执行特定单元格),但看起来不像那样。

Hope it were useful!希望有用!

Edit : 编辑 :

Since Nov 2020 the Jupyter extension is separated from Python extension for VS Code.自 2020 年 11 月起,Jupyter 扩展与 VS Code 的 Python 扩展分开。 The setting key has been renamed from python.data science to jupyter设置键已从 python.data science 重命名为 jupyter

To answer your third question, try this:要回答您的第三个问题,请尝试以下操作:

import os

os.system("echo %cd% > dir")
file = open("dir", "r")
filePath = file.read()
file.close()

print(filePath.split("\n")[0])

I had the same problem.我有同样的问题。 I solved it by putting the setting into the settings.json file manually:我通过手动将设置放入settings.json文件来解决它:

{
    "jupyter.notebookFileRoot": "${fileDirname}",
}

Why it solves the issue (my guess):为什么它解决了这个问题(我的猜测):

Jupyter changes the working directory when starting. Jupyter 在启动时更改工作目录。 After this, the notebookFileRoot settings from settings.json files are implemented, however as ${fileDirname} is default, this is not added to the json file.在此之后,将实现settings.json文件中的 notebookFileRoot 设置,但是由于${fileDirname}是默认值,因此不会将其添加到 json 文件中。 Therefore the file root will not be changed back to ${fileDirname} if the setting is set through the UI settings in vscode.因此,如果通过 vscode 中的 UI 设置进行设置,则文件根不会更改回${fileDirname}

In the past the solutions provided before worked for me:过去,之前提供的解决方案对我有用:

  • Setting up "jupyter.notebookFileRoot": "${workspaceFolder}", would always set the jupyter notebook to the root folder of the project.设置 "jupyter.notebookFileRoot": "${workspaceFolder}",将始终将 jupyter notebook 设置为项目的根文件夹。

Since the last update (I am not on version 1.59.0) this no longer works.自上次更新(我不是 1.59.0 版)以来,这不再有效。 When launching the jupyter notebook from a script it does set it the right way the first time.当从脚本启动 jupyter notebook 时,它会第一次以正确的方式设置它。 But, after a restart it sets the current directory to the VS Code installation path.但是,重新启动后,它将当前目录设置为 VS Code 安装路径。

Do any of you know of another setting that came up or so, in the latest updates?你们中有人知道在最新更新中出现的其他设置吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM