Skip to content

Add script to create virtualenv and add to jupyter #114

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ Convenience Commands
directory in PROJECT_HOME and a virtualenv in WORKON_HOME. The project path
will automatically be associated with the virtualenv on creation.

``mkjupyter <name>``
Create a new virtualenv environment named *<name>* and add it to the
user's jupyter environment. Calls `mkvirtualenv -i ipykernel <name>` and
then, from the activated virtualenv,
`python -m ipykernel install --user --name <name>`.

``setprojectdir <full or relative path>``
If a virtualenv environment is active, define *<path>* as project
directory containing the source code. This allows the use of ``cdproject``
Expand Down
41 changes: 41 additions & 0 deletions scripts/mkjupyter.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@echo off
::
:: Convenience script to create a virtualenv and add it to jupyter
::
:: Syntax:
::
:: mkjupyter ENV_NAME
::

:getopts
:: print usage if no arguments given
if "%~1"=="" goto:usage & exit /b 0
if "%~1"=="-h" goto:usage & exit /b 0
if "%~1"=="--help" goto:usage & exit /b 0

:: use mkvirtualenv to create the environment with the ipython kernel
call mkvirtualenv -i ipykernel "%~1"

:: install the kernel in jupyter
python -m ipykernel install --user --name="%~1"

:: clean up and exit
goto:cleanup
exit /b 0

:error_message
echo.
echo. ERROR: %*
echo.
goto:cleanup

:usage
echo.Usage: mkjupyter ENV_NAME
echo.
echo. ENV_NAME The name of the environment to create.
echo.
echo.The new environment is automatically activated after being initialized.
:: fall through

:cleanup
goto:eof
2 changes: 2 additions & 0 deletions scripts/virtualenvwrapper.bat
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ echo. lssitepackages: list contents of the site-packages directory
echo.
echo. lsvirtualenv: list virtualenvs
echo.
echo. mkjupyter: create a new virtualenv with an ipykernel and add it to jupyter
echo.
echo. mkproject: create a new project directory and its associated virtualenv
echo.
::echo. mktmpenv: create a temporary virtualenv
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
EMAIL = '[email protected]'
DESCRIPTION = ('Port of Doug Hellmann\'s virtualenvwrapper '
'to Windows batch scripts')
VERSION = '1.2.6'
VERSION = '1.2.6'
PROJECT_URL = 'https://github.com/davidmarble/%s/' % (PROJECT)
scripts_loc = 'scripts/'
scripts = [
Expand All @@ -20,6 +20,7 @@
'folder_delete.bat',
'lssitepackages.bat',
'lsvirtualenv.bat',
'mkjupyter.bat',
'mkproject.bat',
'mkvirtualenv.bat',
'rmvirtualenv.bat',
Expand Down
35 changes: 35 additions & 0 deletions tests/test_mkjupyter.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@echo off

set config.output=%TMP%\output%config.unique%
mkdir %config.output%


call _start_test mkjupyter_usage_from_noargs
:: check that we find parts of the usage string when
:: run without any arguments
set fname=%config.output%\%config.current_test%.output

call mkjupyter > %fname%

set "usage=The new environment is automatically"
call assertContains %fname% "%usage%


call _start_test mkjupyter_happy
set fname=%config.output%\%config.current_test%.output
pushd .
call mkjupyter mkjupyter_test_happy
set /a errno=%ERRORLEVEL%
set "curdir=%CD%"
popd

call assertEquals %errno% 0
call assertEquals "%VIRTUAL_ENV%" "%WORKON_HOME%\mkjupyter_test_happy"
call assertExist %VIRTUAL_ENV%\Lib\site-packages\ipykernel\ipkernel.py


:cleanup
set fname=
set usage=
rmdir "%config.output%" /s/q
call rmvirtualenv mkjupyter_test_happy