1

I often want to start a jupyter notebook in a specific conda environment. In order to speed up that process, I add the two commands into one powershell script, like so:

# conda_notebook.ps1
conda activate my_env
jupyter notebook --ServerApp.root_dir C:\Users\me\my-notebooks

This works perfectly fine when executing in a anaconda powershell. I also have pyenv-venv virtual environments in my Windows installation for cases, where I have trouble with conda, e.g., python versions not supported in conda (yet?). The approach

# pyenv_notebook.ps1
pyenv-venv activate my_env2
jupyter notebook --ServerApp.root_dir C:\Users\me\my-other-notebooks

does not work, as pyenv-venv activate my_env2 spawns a new shell. Hence, the second argument is not executed immediately, but only once I exit the newly spawned shell. Then, it fails as there is no jupyter notebook command around in the previous shell. The pyenv suit has a pyenv shell command that changes the current shell instead of spawning a new one. It does not exist for pyenv-venv, unfortunately. However, there must be a way of piping the second command into the first, so that the second command is actually executed in the newly spawned shell. How do I do that?

EDIT1:

"jupyter notebook --ServerApp.root_dir D:\drescherlab\misc-win" | pyenv-venv activate dev3.12.3

and

ECHO "jupyter notebook --ServerApp.root_dir D:\drescherlab\misc-win" | pyenv-venv activate dev3.12.3

do not work. I guess we can expect that, as the pyenv-venv activate ... does not expect a piped in value. The command jupyter notebook ... has to be executed only after the command pyenv-venv activate ... has spawned the new shell.

1 Answer 1

0

I played around a little bit and found a potential solution:

# Execute the activation in a new Process
# BUT: keep input and output bound to the
# current window `-NoNewWindow`
Start-Process -NoNewWindow -FilePath "cmd.exe" -ArgumentList "/c pyenv-venv activate dev3.12.3"

# Wait for start up of new shell
Start-Sleep -Seconds 2

# Use WSH to send keystrokes
# As the newly spawned shell is bound to the
# inputs of this window, the inputs go to
# the newly spawned shell.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("jupyter notebook --ServerApp.root_dir D:\drescherlab\misc-win{ENTER}")

However, once the last command [System.Windows... is executed, the script returns. Outputs of the notebook are still shown, but now inputs go to the original shell and to the notebook. Furthermore, the output of the notebook is interspersed with PS C:\Users\me> outputs of the original shell.

I hope, powershell allows for a more elegant solution.

Sign up to request clarification or add additional context in comments.

1 Comment

If you want your cmd.exe session that runs your virtual environment to stay open, use /k in lieu of /c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.