Skip to content

Instantly share code, notes, and snippets.

@nmpowell
Last active February 22, 2019 17:46
Show Gist options
  • Save nmpowell/2ddac15144e5b5749afbf2ea59a31b64 to your computer and use it in GitHub Desktop.
Save nmpowell/2ddac15144e5b5749afbf2ea59a31b64 to your computer and use it in GitHub Desktop.
Windows batch script to set up Jupyter's QtConsole and Notebook in a Python3 virtual environment. With shortcuts.
REM Windows batch script to set up Jupyter QtConsole and Notebook in a Python3 virtual environment.
REM Place this script .bat file inside the root venv directory (same level as 'Scripts' and 'Lib' subfolders), and run.
REM Also creates convenience launching scripts.
REM Get current directory name and full path
for %%* in (.) do set VenvDirName=%%~nx*
set VenvDirFullPath=%~dp0
REM Activate the venv
call %VenvDirFullPath%Scripts\activate.bat
REM Install requirements
pip install ipykernel pyqt5 qtconsole jupyter
REM Make ready
REM Here we run the command, "ipython kernel install", and set the complete output line to variable VAR:
for /f "tokens=*" %%i in ('ipython kernel install') do set VAR=%%i
REM We know the output string will be like: "Installed kernelspec python3 in <dir>"; we want <dir>, the 5th token.
for /f "tokens=5" %%G IN ("%VAR%") do set kernelPath=%%G
REM Get the final part in that output, the actual kernel name. It's probably "python3".
for /f "delims=" %%i in ("%VAR%") do set kernelName=%%~nxi
REM Perform string replacement to get a new kernelPath, and move the folder to rename it
call set newKernelPath=%%kernelPath:%kernelName%=%VenvDirName%%%
echo "Moving %kernelPath% --> %newKernelPath%"
move %kernelPath% %newKernelPath%
REM Deactivate the venv
call deactivate
REM Edit the kernel.json file so that the kernel is named for the Venv, so will be identifyable in Jupyter Notebooks etc.
set kernelFilePath=%newKernelPath%\kernel.json
REM Note that -encoding UTF8 inserts a BOM character and this breaks JuPyter when loading; ASCII is a subset of UTF8 and does not insert the BOM.
powershell -Command "(gc %kernelFilePath%) -replace 'Python 3', 'Python 3 (%VenvDirName%)' | Out-File -encoding ASCII %kernelFilePath%"
REM Create 2 .bat files in this directory to easily launch the QtConsole and Notebooks.
REM Note that the below hard-codes the current venv dir; the variable is set above.
set consoleRunnerBat=launch_Jupyter_QtConsole.bat
set notebookRunnerBat=launch_Jupyter_Notebook.bat
@echo off
echo REM Windows batch script to run Jupyter's QtConsole in this Python3 virtual environment. > "%consoleRunnerBat%"
echo REM Activate the venv >> "%consoleRunnerBat%"
echo call %VenvDirFullPath%Scripts\activate.bat >> "%consoleRunnerBat%"
echo REM Launch QtConsole >> "%consoleRunnerBat%"
echo call jupyter qtconsole --kernel %VenvDirName% >> "%consoleRunnerBat%"
echo REM Windows batch script to run Jupyter's Notebook in this Python3 virtual environment. > "%notebookRunnerBat%"
echo REM Activate the venv >> "%notebookRunnerBat%"
echo call %VenvDirFullPath%Scripts\activate.bat >> "%notebookRunnerBat%"
echo REM Launch Notebook >> "%notebookRunnerBat%"
echo call jupyter notebook >> "%notebookRunnerBat%"
@echo on
@nmpowell
Copy link
Author

At minimum for the Qt Console:

pip install jupyter pyqt5 qtconsole

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment