4 Python
Python is another major programming language used in Shen Lab. We use Python for data processing, automation, machine learning, bioinformatics workflows, API access, visualization, and building reproducible analysis pipelines. This chapter introduces the recommended setup for Python, editors, environments, and daily project practice.
4.1 What To Install
New members who will use Python should install:
- Python;
- a Python environment manager;
- Visual Studio Code or another suitable editor;
- JupyterLab or Jupyter Notebook, when interactive analysis is useful;
- Git, so code can be version controlled and shared;
- common scientific Python packages.
For most lab members, the recommended approach is to use a managed Python environment instead of installing packages into the system Python.
4.2 Installing Python
Python can be installed in several ways. The best choice depends on your operating system, project needs, and whether you are working locally or on a lab server.
4.2.1 Recommended Options
Common installation options include:
- official Python from https://www.python.org/;
- Miniforge or Miniconda for environment management;
- Homebrew Python on macOS;
- system package manager Python on Linux;
- server-managed Python modules or Conda environments on shared servers.
For scientific computing, Miniforge or Miniconda is often convenient because it supports isolated environments and can install packages with compiled dependencies.
4.2.2 macOS
Recommended options:
- install Miniforge or Miniconda for scientific Python environments;
- or install Python through Homebrew if you are comfortable using the command line.
After installation, open Terminal and check:
If you use Conda or Miniforge, also check:
4.3 Environment Management
Python projects should use isolated environments. This prevents package conflicts and makes analyses easier to reproduce.
Recommended tools include:
-
condaormambafor scientific environments; -
venvfor lightweight Python-only environments; -
pipfor installing Python packages; -
requirements.txt,environment.yml, or another lock file to record dependencies.
4.4 Editors And Notebooks
4.4.1 Visual Studio Code
Visual Studio Code is a good default editor for Python projects. Recommended extensions include:
- Python;
- Jupyter;
- GitLens, if you want more Git integration;
- Ruff or another linter/formatter, if used by the project.
After opening a project folder in VS Code, select the correct Python interpreter or environment. This is important because different projects may use different environments.
4.4.2 JupyterLab
JupyterLab is useful for exploration, visualization, and interactive analysis. It should be used carefully for reproducible work.
Good practice:
- use notebooks for exploration and communication;
- move stable workflows into scripts or packages when possible;
- restart the kernel and run all cells before sharing a notebook;
- avoid hidden state from out-of-order execution;
- keep large outputs out of version control unless they are needed.
Start JupyterLab:
4.5 Common Packages
Commonly used Python packages include:
-
numpyfor numerical computing; -
pandasfor tabular data; -
scipyfor scientific computing; -
matplotlibandseabornfor visualization; -
scikit-learnfor machine learning; -
statsmodelsfor statistical modeling; -
jupyterlabfor notebooks; -
pyarrowfor efficient table formats; -
openpyxlfor Excel files; -
requestsfor APIs; -
pytestfor testing.
Project-specific omics or mass spectrometry analyses may require additional packages. Follow the project README or ask your mentor before installing many packages into an existing environment.
4.6 Project Organization
Use one folder per project and keep code, data, results, and documentation organized.
Example structure:
project-name/
data/
notebooks/
scripts/
src/
results/
figures/
docs/
environment.yml
README.md
Good practice:
- keep raw data unchanged;
- place reusable code in
src/or a package-like structure; - use
scripts/for command-line workflows; - use
notebooks/for exploration; - write a README explaining how to run the analysis;
- save results and figures in clear output folders;
- avoid absolute paths that only work on your computer.
4.7 Working With Data
When using Python for lab data analysis:
- preserve raw data;
- document each processing step;
- keep sample metadata close to the analysis;
- check missing values, duplicates, and inconsistent identifiers;
- make plots from code rather than manual editing;
- save processed data only when the processing script is also saved;
- keep sensitive or unpublished data in approved storage locations.
For omics data, pay careful attention to feature identifiers, sample names, batch variables, normalization, quality control, and file provenance.
4.8 Reproducibility
Python analyses should be rerunnable by another lab member.
Recommended habits:
- use a project-specific environment;
- record dependencies in
environment.ymlorrequirements.txt; - set random seeds when using stochastic methods;
- use relative paths within the project;
- write functions for repeated steps;
- keep parameters in code or configuration files;
- restart the Python session and rerun the analysis before sharing final results;
- include enough comments for non-obvious analysis choices.
For important analyses, consider adding simple tests or checks, especially when code is reused across projects.
4.9 Getting Help
If you encounter a Python problem, collect:
- your operating system;
- Python version;
- environment name and package versions;
- the exact error message;
- the command or code that produced the error;
- a small example that reproduces the problem, if possible;
- what you already tried.
Then ask your mentor, a relevant lab member, or the PI. A clear and reproducible error report saves everyone time.
Python 是 Shen Lab 另一门主要使用的编程语言。我们用 Python 做数据处理、自动化、机器学习、生物信息学流程、API 调用、可视化和可复现分析管线。本章介绍 Python、编辑器、环境管理和日常项目实践。
需要使用 Python 的成员通常应安装 Python、环境管理工具、Visual Studio Code 或其他编辑器、JupyterLab、Git,以及常用科学计算 packages。建议不要把所有 package 都装进系统 Python,而是为每个项目建立独立环境。
Python 有多种安装方式,包括 Python 官网、Miniforge/Miniconda、macOS Homebrew、Linux package manager,以及服务器上的 module 或 Conda 环境。科学计算场景通常推荐 Miniforge 或 Miniconda,因为它们更容易管理独立环境和带编译依赖的 packages。
可以安装 Miniforge/Miniconda,或通过 Homebrew 安装 Python。安装后在 Terminal 检查:
推荐安装 Miniforge/Miniconda,或从 Python 官网安装。安装时注意让 Python 可以在命令行中使用。安装后在 PowerShell 检查:
或:
Linux 通常已经有 Python,可用 python3 --version 检查。在共享服务器上不要修改系统 Python,应使用项目环境、Conda 环境、module system 或服务器管理员提供的方式。
Python 项目应使用独立环境,避免 package 冲突,并提高可复现性。常用工具包括 conda、mamba、venv、pip、requirements.txt 和 environment.yml。
conda create -n project-name python=3.11
conda activate project-name
conda install numpy pandas scipy matplotlib seaborn scikit-learn jupyterlab
conda env export > environment.ymlpython3 -m venv .venv
source .venv/bin/activate
pip install numpy pandas scipy matplotlib seaborn scikit-learn jupyterlab
pip freeze > requirements.txtWindows PowerShell 使用:
Visual Studio Code 是 Python 项目的常用编辑器。建议安装 Python、Jupyter、GitLens、Ruff 或项目指定的 linter/formatter。打开项目后一定要选择正确的 Python interpreter。
JupyterLab 适合探索、可视化和交流,但用于可复现分析时要谨慎。分享 notebook 前应 restart kernel 并 run all cells,避免隐藏状态和乱序执行。稳定流程应尽量整理成 scripts 或 package。
常用 packages 包括 numpy、pandas、scipy、matplotlib、seaborn、scikit-learn、statsmodels、jupyterlab、pyarrow、openpyxl、requests 和 pytest。组学或质谱项目可能需要额外 packages,应优先参考项目 README 或询问 mentor。
建议每个项目使用独立文件夹,并清楚区分数据、代码、结果和文档:
project-name/
data/
notebooks/
scripts/
src/
results/
figures/
docs/
environment.yml
README.md
raw data 应保持不变,可复用代码放在 src/,命令行流程放在 scripts/,探索性分析放在 notebooks/。README 应说明如何运行分析,路径尽量使用项目内相对路径。
使用 Python 分析实验室数据时,应保留 raw data、记录每一步处理、检查 sample metadata、missing values、duplicates 和 identifier 不一致问题。Figures 应尽量从代码生成,processed data 只有在对应处理脚本保存时才保存。敏感或未发表数据必须放在批准的位置。
组学数据尤其要注意 feature identifiers、sample names、batch variables、normalization、quality control 和 file provenance。
Python 分析应能被其他成员重新运行。建议使用项目独立环境,用 environment.yml 或 requirements.txt 记录依赖,设置 random seed,使用相对路径,把参数写入代码或配置文件,并在分享最终结果前重启环境重新运行。重要可复用代码应考虑加入简单测试。
遇到 Python 问题时,请提供操作系统、Python 版本、环境名称、package versions、完整错误信息、产生错误的命令或代码、可复现小例子,以及已经尝试过的解决方法。清楚的问题描述能节省大家时间。