用Pip下载jupyter notebook时发现启动时会有两个值得注意的地方,一个是启动时的环境变量参数路径设定,一个是启动后默认的工作区设定。
因为默认只能在 C:/users/用户名 路径下执行jupyter notebook,这样环境变量参数路径和工作区都设在了该目录,怎样修改呢?
我写了一段python代码 start_jupyter.py ,用来指定启动时的环境变量参数路径:
1 import os 2 import subprocess 3 4 base = 'D:Program Files (x86)PythonPython36' 5 jupyter_dir = os.path.join(base, '.jupyter') 6 7 if not os.path.exists(jupyter_dir): 8 os.mkdir(jupyter_dir) 9 10 dirs = {'JUPYTER_CONFIG_DIR': jupyter_dir, 'JUPYTER_RUNTIME_DIR':os.path.join(jupyter_dir,'runtime'), 'JUPYTER_DATA_DIR':os.path.join(jupyter_dir,'data')} 11 12 for k, v in dirs.items(): 13 if not os.path.exists(v): 14 os.mkdir(v) 15 os.environ[k] = v 16 17 ipython_dir = os.path.join(base,'.ipython') 18 os.environ['IPYTHONDIR'] = ipython_dir 19 20 subprocess.call(['D:\Program Files (x86)\Python\Python36\Scripts\jupyter-notebook.exe'])
接下来轮到设定工作区,打开 .jupyter/jupyter_notebook_config.py, 修改 c.NotebookApp.notebook_dir =‘’ 为实际的工作区地址:
c.NotebookApp.notebook_dir = 'D:Program Files (x86)PythonPython36jupyter_workpath'
这样在jupyter notebook启动时就可以指定工作区, 执行启动命令: python start_jupyter.py