问题描述
我需要在另一个脚本中使用jupyter的本地目录的位置.例如,启动jupyter时,控制台将打印:
I need the location of jupyter's local directory to use in another script. For example, when starting jupyter, the console prints:
> Serving notebooks from local directory: C:\Users\PC\Documents
不知何故,jupyter将此路径设置为提供笔记本计算机的默认本地位置.这是我的jupyter环境的根".如何找到此默认设置?我可以解析一个预先存在的文件,或者可以键入一个命令吗?
Somehow, jupyter has set this path as the default local location from which my notebooks are served. It is the "root" of my jupyter environment. How do I find this default setting? Is there a pre-existing file I can parse, or a command I can type?
在其他尝试中,我尝试了以下操作:
Among other attempts, I've tried the following:
> jupyter notebook --help-all
--NotebookApp.notebook_dir=<Unicode>
Default: ''
The directory to use for notebooks and kernels.
注意:我没有配置文件,也不希望生成配置文件以获得此信息.为了清楚起见,我不关心更改目录.
Note: I do not have a config file, nor do I not wish to generate a config file to get this information. For clarity, I am not concerned with changing the directory.
推荐答案
来自 https://stackoverflow.com/a/37690452,似乎默认位置是在Anaconda文件夹的cwp.py
文件中设置/派生的.对于Windows上的默认安装,似乎该脚本位于(对于用户名PC
,如您的问题所示)C:\Users\PC\AppData\Local\Continuum\Anaconda3\cwp.py
.
From https://stackoverflow.com/a/37690452, it seems that the default location is set/derived in the cwp.py
file in the Anaconda folder. For default installs on Windows, it seems that this script would be at (for username PC
, as in your question) C:\Users\PC\AppData\Local\Continuum\Anaconda3\cwp.py
.
在 github.com/ContinuumIO/menuinst上找到cwp.py的源,看来它基本上是在使用当前的Windows用户的Documents文件夹.
Finding the source of cwp.py on github.com/ContinuumIO/menuinst, it seems that it's basically using the current Windows User's Documents folder.
我在 notebook/注释中要求的问题/2941#issuecomment-337076059 :
除非在配置或环境变量中另外指定,否则笔记本服务器将在启动时使用当前工作目录 作为笔记本目录.您的文件夹似乎已设置为documents文件夹,因为您用来启动笔记本的Anaconda快捷方式在启动笔记本服务器之前设置工作目录有些不可思议.为了您的目的,您可以:
Unless specified otherwise in config or environment variables, the notebook server will use the current working directory at start time as the notebook directory. Yours seems to be set to the documents folder because the Anaconda shortcut which you're using to start the notebook is doing a little magic to set the working directory before starting the notebook server. For you purposes, you could:
- 在配置文件或环境变量中明确设置笔记本目录
-
假设笔记本的根目录位于
- Explicitly set the notebook directory in a config file or environment variable
Assume the notebook root is at
os.path.expanduser('~/Documents')
(请注意,如果操作系统语言不是英语,这可能会需要调整)
(note this may require adjustment if OS language is not English)
使用pip/conda软件包 menuinst
查找文件夹,完全复制了与anaconda快捷方式相同的逻辑-类似以下内容:
Use the pip/conda package menuinst
to find the folder, replicating the same logic as the anaconda shortcut exactly - something along the lines of:
from menuinst.windows.knownfolders import FOLDERID, get_folder_path
documents_folder, exception = get_folder_path(FOLDERID.Documents)
if exception:
documents_folder, exception = get_folder_path(FOLDERID.PublicDocuments)
if exception:
print('bang! exception! do some error handling')
else:
print(documents_folder)
(请注意,这可能会在非Windows系统上失败)
(note this will probably fail on non-windows systems)
这篇关于查找默认的本地Jupyter目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!