ubuntu上搭建虚拟环境使用jupyter notebook

潘安平
2023-12-01

整体流程

$ sudo apt update
$ sudo apt install python3-pip python3-dev
$ sudo -H pip3 install --upgrade pip 
$ sudo -H pip3 install virtualenv
$ mkdir jupyter_dir
$ cd jupyter_dir
$ virtualenv jupyter_env
$ source jupyter_env/bin/activate
$ pip3 install jupyter 
$ jupyter notebook

(pip install 推荐使用镜像,
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
具体方法见文末)

具体执行

搭建虚拟环境

https://bynss.com/linux/572213.html
创建unknown number的目录

mkdir unknown_number
cd unknown_number

使用virtualenv命令创建新的虚拟环境

virtualenv unknown_number_jupyter

结果如下:

wendy@wendy-virtual-machine:~/unknown_number$ virtualenv unknown_number_jupyter
created virtual environment CPython3.10.4.final.0-64 in 204ms
  creator CPython3Posix(dest=/home/wendy/unknown_number/unknown_number_jupyter, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/wendy/.local/share/virtualenv)
    added seed packages: pip==22.2.1, setuptools==63.2.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

激活虚拟环境

source unknown_number_jupyter/bin/activate

激活后可以看到终端命令的开头增加了虚拟环境的名称

wendy@wendy-virtual-machine:~/unknown_number$ source unknown_number_jupyter/bin/activate
(unknown_number_jupyter) wendy@wendy-virtual-machine:~/unknown_number$ pip3 install jupyter

下面,在这个虚拟环境下安装jupyter
使用pip install jupyter 显示错误,如下:

(unknown_number_jupyter) wendy@wendy-virtual-machine:~/unknown_number$ pip3 install jupyter
Collecting jupyter
  Using cached jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)
Collecting ipykernel
  Downloading ipykernel-6.15.1-py3-none-any.whl (132 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 132.9/132.9 kB 5.8 kB/s eta 0:00:00
Collecting jupyter-console
  Downloading jupyter_console-6.4.4-py3-none-any.whl (22 kB)
ERROR: Could not find a version that satisfies the requirement ipywidgets (from jupyter) (from versions: none)
ERROR: No matching distribution found for ipywidgets

(这里暂时不解决这个问题)
尝试在终端输入jupyter notebook,显示没有jupyer命令。

(unknown_number_jupyter) wendy@wendy-virtual-machine:~/unknown_number$ jupyter notebook
Command 'jupyter' not found, but can be installed with:
sudo snap install jupyter       # version 1.0.0, or
sudo apt  install jupyter-core  # version 4.9.1-1
See 'snap info jupyter' for additional versions.

终端显示:推荐使用snap或者apt。
pip snap apt比较:
pip install安装的python包,可以只安装在当前工程内。apt 和 snap则会在系统中完全安装。

后来在运行时发现,不使用pip安装Jupyter会导致运行的jupyter notebook不在创建的虚拟环境内,使用的内核不是ipykernal

因此,使用清华镜像重新pip install jupyter,很快完成安装。

运行

安装完成后,虚拟环境下,输入jupyter notebook,打开

(unknown_number_jupyter) wendy@wendy-virtual-machine:~/unknown_number$ jupyter notebook

点击最后的网址,复制到浏览器上,即可见到jupyter界面。

问题描述——没有sklearn模块

在jupyter界面运行时,出现错误 No module named ‘sklearn’

ModuleNotFoundError: No module named 'sklearn'

打开一个新的终端,进入到当前的虚拟环境,pip install sklearn.
或者可以在notebook里输入,加感叹号表示为行命令。

!pip install sklearn

期间失败了好几次,显示“read time out”,在重新联网,将pip install numpy 和 pip install sklearn 分开后,成功。
pi

补充1 virtualenv命令下退出虚拟环境、删除虚拟环境

退出虚拟环境

直接输入deactive

删除虚拟环境

直接删除包含虚拟环境的文件夹即可
rm

补充2 pip install 使用清华镜像加速

pip 清华大学开源软件镜像站
使用国内镜像速度会快很多:

临时使用:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

例如,安装 Django:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Django

如果要设为默认需要升级 pip 到最新的版本 (>=10.0.0) 后进行配置:

pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

如果您到 pip 默认源的网络连接较差,临时使用本镜像站来升级 pip:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U

补充3 pip对包的管理

pip list 将显示虚拟环境中安装的所有软件包:

(tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

pip freeze` 将生成一个类似的已安装包列表,但输出使用 pip install 期望的格式。一个常见的约定是将此列表放在 requirements.txt 文件中:

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

然后可以将 requirements.txt 提交给版本控制并作为应用程序的一部分提供。然后用户可以使用 install -r 安装所有必需的包:

(tutorial-env) $ python -m pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
  ...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
  ...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
  ...
Installing collected packages: novas, numpy, requests
  Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

来源:
https://bynss.com/linux/572213.html(整体安装流程)
https://www.runoob.com/w3cnote/python-pip-install-usage.html (镜像加速)
https://docs.python.org/zh-cn/3/tutorial/venv.html(pip管理)

 类似资料: