我使用以下PowerShell命令在Windows 10的虚拟环境中安装了Jupyter:

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

除了在我的计算机上安装了Python和Virtualenv之外,我完全没有做过任何事情. 为了进入Jupyter笔记本,我在PowerShell中运行了其中的第2行和第4行,它运行得很好.

如何使用桌面/任务栏快捷方式达到打开Jupyter笔记本的相同效果?

我读了this个问题的答案,但这两个有用的答案都使用了conda和Anaconda,我没有,也不想拥有,因为它似乎对这项任务来说是不必要的.

推荐答案

根据您对如何从交互式PowerShell会话打开Jupyter Notebook的描述(第2行和第4行),您需要将快捷方式文件调用powershell.exe,Windows PowerShell CLI,并使用上述语句.

以下是以编程方式创建快捷方式文件的PowerShell代码片段:

# 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

注:

  • -NoExit使PowerShell会话保持打开状态,这样您就可以判断启动时发生了什么;一旦您验证启动笔记本电脑按预期工作,就可以将其删除.

  • 100启动PowerShell会话minimized(因为您可能对查看控制台窗口不感兴趣.因此,结合-NoExit,如果你想判断在启动时发生了什么,你需要通过任务栏激活最小化的窗口.

  • 赋值为100,即指定目标可执行文件,将导致赋值为instantly resolved to a full path:

    • 如果你分配一个mere file name -例如powershell.exe -它会在路径中查找,即在标准$env:PATH环境变量中列出的目录中查找.

      • Caveat:如果使用了名称为happens to exist in the same directory as the shortcut fileit的可执行文件.
    • 因此,要获得完全的稳健性,您可能需要指定full path,例如:

      • 使用文字或基于环境变量的路径,例如:

         .TargetPath = "$env:windir\System32\WindowsPowerShell\v1.0\powershell.exe"
        
      • 或者通过在设计时执行$env:PATH查找;例如:

         .TargetPath = (Get-Command powershell.exe).Path
        
  • 默认情况下,已启动进程的工作目录设置为:

    • 当快捷方式文件打开时为directory in which the shortcut file is located interactively(通常是通过任务栏、桌面或文件资源管理器)

    • 相反,在programmatic调用中,它依赖于caller's working directory,有一个微妙的区别.

      • 对于Invoke-Item,它是调用者process的工作目录,通常与PowerShell's的工作目录不同.[1]
      • 对于Start-Process,它是呼叫PowerShell session的工作目录(位置).
    • 因此,最好是explicitly通过分配给100属性来分配一个工作目录.

  • 如上面的.WorkingDirectory赋值中所示,you may use 101-style environment-variable references(如%USERPROFILE%)和这样的引用在.TargetPath.Arguments.IconLocation属性中同样受支持.

  • 一百:

    • 尽管名为.CreateShortcut(),但.CreateShortcut()方法也可以打开existing个快捷方式文件,例如判断它们的属性.

    • 如果您(可能无意中)在try 完全(重新)创建一个existing快捷方式文件时只填写了它的some个属性,则任何preexisting个属性值都是preserved.

    • 因此,如果意图是创建快捷方式文件from scratch,则首先删除任何先前存在的版本.

  • 文档链接:


[1]PowerShell支持每个进程多个运行空间(线程),每个运行空间(线程)可以有一个单独的工作目录.这与.NET和其他进程内API使用的single, process-wide工作目录形成对比.因此,给定的PowerShell会话(运行空间)的工作目录不能与进程范围的工作目录保持同步,两者通常为differ.结果是,您应该始终传递full, file-system-native条指向.NET和其他进程内API的路径.有关详细信息,请参阅this answer.

Python相关问答推荐

如何在Python中使用ijson解析SON期间检索文件位置?

Python无法在已导入的目录中看到新模块

使用多个性能指标执行循环特征消除

指示组内的rejected_time是否在creation_timestamp后5分钟内

添加包含中具有任何值的其他列的计数的列

Pandas 第二小值有条件

使用miniconda创建环境的问题

沿着数组中的轴计算真实条目

无法使用requests或Selenium抓取一个href链接

如何调整QscrollArea以正确显示内部正在变化的Qgridlayout?

在Python中动态计算范围

利用Selenium和Beautiful Soup实现Web抓取JavaScript表

当点击tkinter菜单而不是菜单选项时,如何执行命令?

在Python中调用变量(特别是Tkinter)

在极中解析带有数字和SI前缀的字符串

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

(Python/Pandas)基于列中非缺失值的子集DataFrame

基于Scipy插值法的三次样条系数

pandas:在操作pandora之后将pandora列转换为int

如何在Gekko中处理跨矢量优化