pythonwindowspowershelljupyter-notebookjupyter

How can I open jupyter in a venv with a shortcut?


I installed Jupyter in a virtual environment on Windows 10 using the following Powershell commands:

virtualenv --python C:\Path\To\Python\python.exe venv-name
.\venv-name\Scripts\activate
pip install numpy matplotlib jupyter notebook pandas joblib scipy sympy
jupyter notebook

I have done absolutely nothing beyond this other than having Python and virtualenv on my computer. To get into Jupyter notebook, I run lines 2 and 4 of this in Powershell, which works just fine.

How can I achieve the same effect of opening Jupyter notebook using a Desktop/Taskbar shortcut?

I read the answers to this question, but both of the useful answers make use of conda and Anaconda, which I do not have, and do not really want to have as it seems like it should be unnecessary for this task.


Solution

  • Based on your description of how you open Jupyter Notebook from an interactive PowerShell session (lines 2 and 4), you'll need to make your shortcut file call powershell.exe, the Windows PowerShell CLI, with said statements.

    Here's a PowerShell snippet that creates a shortcut file programmatically:

    # Path to the shortcut file; adjust as needed.
    $shortcutFile = "$env:USERPROFILE\Desktop\Jupyter Notebook.lnk"
    
    # Create the shortcut file...
    $shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut($shortcutFile)
    
    # ... and set its properties.
    
    # Specify the target executable and its arguments.
    $shortcut.TargetPath = 'powershell.exe' 
    $shortcut.Arguments =
      '-NoExit -Command .\venv-name\Scripts\activate; jupyter notebook'
    
    # Specify the working directory.
    $shortcut.WorkingDirectory = '%USERPROFILE%'
    
    # Set the window style, if needed.
    $shortcut.WindowStyle = 7 # Open the PowerShell console window minimized.
    
    $shortcut.Save()
    
    # Test invocation (simulate opening the shortcut interactively)
    Invoke-Item $shortcutFile
    

    Note:


    [1] PowerShell supports multiple runspaces (threads) per process, each of which can have a separate working directory. This contrasts with the single, process-wide working directory, as used by .NET and other in-process APIs. Therefore, a given PowerShell session's (runspace's) working directory cannot be kept in sync with the process-wide one, and the two typically differ. The upshot is that you should always pass full, file-system-native paths to .NET and other in-process APIs. For more information, see this answer.