轻量Python开发环境JupyterLab

尝试了很多Python的开发环境(Integrated Development Environment, IDE)后,终于找到了一款轻量的、交互的、基于浏览器的IDE。

轻量Python开发环境JupyterLab

Jupyter Lab是一款开源的Python开发环境,定位于:Interactive Data Science and Scientific Computing。

这款软件的主要特点是:

  1. 开源(Open Source):即是免费。不过现在大部分的IDE是免费的,这也不算是特别的优点。
  2. 基于浏览器(Web-based):不需要安装额外的软件,因此使用起来会方便快捷,毕竟每台电脑都有浏览器。
  3. 最重要的特点是可以把代码分成多个独立的Cells,每个Cells可独立运行。我们可以只运行自己想要运行的单元格,不用运行整个代码。

Jupyter的安装也很简单,只需要命令提示符中输入:

pip install jupyterlab

或者(可执行文件的名称为pip3时使用)

pip3 install jupyterlab

安装好后可通过以下命令运行

jupyter lab

为什么Jupyter会适合科学计算呢?

  1. 这款软件是通过Code Cells来管理代码的,每个Cell可以单独运行。这种设计的话是方便调试。科学计算需要大量探索代码块的运行结果,然后来调试代码。
  2. JupyterLab 支持多种输出格式,包括文本、图像、图表和交互式小部件,从而能够有效地可视化数据。

使用Jupyter Lab也非常简单,只需要在空白的Code Cell里面输入Python代码,然后按[Shift + Enter]执行。

另外还有一些非常实用的功能:

  1. Tap Completion: Pressing the Tab key will search the namespace for any variables (objects, functions, etc.) matching the characters you have typed so far and show the results in a convenient drop-down menu. 这功能可通过点击[Tab]键索引之前定义过的变量或者引用过的变量,提升编程的效率。Python自带的IDE是没有此项功能,大部分IDE都有这个功能。
  2. Introspection: Using a question mark (?) before or after a variable will display some general information about the object. 这个功能可通过变量前后加一个问题,执行后可显示这个变量的信息,完全不用调配type()函数来了解变量的类型。
  3. 可以通过在代码单元内加上感叹号(!)作为前缀来直接执行 shell 命令。例如在 Jupyter Notebook 中安装numpy的包,只需要运行!pip install numpy,不需要在shell中执行pip instal numpy

如何你还是不想安装Jupyter Notebook,那可以直接用Google Colab,效果和Jupyter Notebook一样,还有免费的GPU TPU资源,甚至AI编程。

Python venv环境如何运行Jupyter?

这种问题可直接问大模型:how to create venv in jupyter?

You don't directly "create" a Python venv directory within the Jupyter Notebook interface itself. Jupyter is an interactive computing environment, not a file system manager for creating virtual environments. Here's a recap of the process, emphasizing where the venv directory is actually created on Macbook:

1. Open your Terminal or Command Prompt:

This is the essential first step. You need a shell environment to execute the commands that create the venv directory.

2. Navigate to your desired project directory:

This is where your venv directory will be created.

cd /path/to/your/project

(Replace /path/to/your/project with the actual path to where you want your project and its virtual environment to reside. For instance, if you have a folder named my_jupyter_projects, you might cd into that.)

3. Create the Virtual Environment (the venv directory is created here!):

Creates a new directory (e.g., my_project_env) within your current working directory. This new directory is your virtual environment.

python3 -m venv my_project_env

4. Activate the Virtual Environment:

This command sets up your shell so that when you run python or pip, they use the versions from within your my_project_env directory.

source my_project_env/bin/activate

5. Install ipykernel within your activated venv:

ipykernel is the package that allows your virtual environment to be recognized as a kernel in Jupyter.

pip install ipykernel

6. Register your venv as a Jupyter Kernel:

This tells Jupyter that the Python environment located in your my_project_env directory is available for use.

python -m ipykernel install --user --name=my_project_env --display-name "Python (My Project Environment)"
  • --name=my_project_env: This should match the name of the venv directory you created.
  • --display-name "Python (My Project Environment)": This is the friendly name that will appear in the Jupyter Notebook interface.

7. Launch Jupyter Notebook:

jupyter notebook

8. Select your venv in Jupyter:

In the Jupyter interface (your web browser):

  • Click New -> Python (My Project Environment) (or whatever you named it).
  • Or, for an existing notebook, go to Kernel -> Change kernel and select it.

In summary:

The venv directory is created by the python -m venv command in your terminal. Jupyter Notebook then acts as an interface to connect to and use the Python interpreter and packages within that already-created venv directory.