Python 安装和配置

Jupyter Notebook

[Update 2022-07-12] Some outputs were updated with python 3.10.

安装 python

安装 python 3

  • 检查系统自带的版本:
1
2
3
import sys
print(sys.version)
!{sys.executable} --version
3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:04:59) [GCC 10.3.0]
Python 3.10.5
1
2
3
%%bash
python --version
python3 --version
Python 3.10.5
Python 3.10.5

pip

安装 pip

检查 pip 是否安装以及版本

1
2
%%bash
pip --version
pip 22.1.2 from /home/lydia/miniconda3/lib/python3.10/site-packages/pip (python 3.10)
1
2
%%bash
python -m pip --version
pip 22.1.2 from /home/lydia/miniconda3/lib/python3.10/site-packages/pip (python 3.10)

安装 (Ubuntu):

1
sudo apt-get install python3-pip

用 pip 安装 python 包

1
2
3
4
5
6
pip install <package>
pip install --upgrade <package>
pip uninstall <package>
pip list
pip list --outdated
pip show <package>

安装到根目录 (不推荐!)

1
sudo -H pip install <package>

[选项]

-U, --upgrade 升级, 配合 install 使用
--force-reinstall 强制重新安装依赖
-I, --ignore-installed 强制安装 (无论是否已安装)
--no-cache-dir 不生成cache
-i url 使用指定源 (更改配置见 pip 镜像源)
package==x.x.x 指定版本, 错误版本号 (或为空) 将返回可用版本号
"package<x.x.x" 指定小于某版本的最新版本, 必须有引号

检查包 (尝试导入, 查看包版本):

1
2
3
python -c "import <package>"
python -c "import <package>; print(<package>.__version__)" # 版本号
python -c "import <package>; print(<package>.version)" # 安装位置

使 pip 安装 python 包到用户路径

1
2
%%bash
python -m site | grep 'USER_SITE'
USER_SITE: '/home/lydia/.local/lib/python3.10/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
  • 确认已创建用户包安装路径.
  • ~/.bashrc 中设置 PATH 变量:
1
export PATH=$HOME/.local/bin:$PATH
  • 使用 installlist 时加上选项 --user (注意: uninstall 不用)

使用 Anaconda 路径下的 pip 时不需要这个选项

1
2
3
pip install --user <pkg_name>
pip list --user
pip list --outdated --user

Anaconda

安装 Anaconda 或 Miniconda (推荐)

1
bash Miniconda3-latest-Linux-x86_64.sh
  • ~/.bashrc 加入如下语句:
1
2
export PATH=$HOME/miniconda3/bin:$PATH
. $HOME/miniconda3/etc/profile.d/conda.sh

 完成后执行:

1
source ~/.bashrc
  • 安装好后先更新 conda 和 pip:
1
conda update conda pip
  • 帮助
1
conda [cmd] --help

miniconda 下载 https://conda.io/miniconda.html
安装参考 https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html

environment

默认环境为 base
-n, --name <env_name> 指定环境

1
2
3
4
5
conda create --name <env_name> [<packages>] python=3.6
conda create --name <env_name> --clone base

# e.g.
conda create --name intel intelpython3_core python=3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 删除环境
conda-env remove --name <env_name>
*
# 列出信息
conda info -e/--envs
conda-env list

# 进入环境
source activate <env_name>
conda activate <env_name>

# 离开环境
source deactivate
conda deactivate

# 在某环境中执行操作
conda <commands> --name <env_name>

使用 conda 命令

1
2
3
4
5
6
7
8
conda info
conda list [<packages>]
conda search <package>
conda search <package> --info
conda install <packages>
conda update/upgrade <packages>
conda remove/uninstall <packages>
conda clean --all

conda --helpconda cmd --help, conda search 语法见 https://github.com/conda/conda/blob/master/conda/models/match_spec.py

配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安装确认时显示源的 url, 默认已开启
conda config --set show_channel_urls True

# 禁止每次更新自动检查 conda 的更新, 默认True
conda config --set auto_update_conda False

# 禁止源优先而使用版本优先, 默认True
conda config --set channel_priority False

# 增加设置, 例如增加源 (可使用 url 或别名)
conda config --add channels <channel>

# 删除某项设置的所有值, 例如删除所有自定义源
conda config --remove-key channels

# 删除某项设置的某个值, 例如删除某个源
conda config --remove channels <channel>

用户配置文件 ~/.condarc (见 conda 镜像源)
参考链接 https://conda.io/docs/user-guide/configuration/use-condarc.html

也可以在不同的环境用不同的配置文件: ~/miniconda3/envs/<env_name>/.condarc

镜像源

pip 镜像源

用户配置文件:

1
2
%%bash
cat ~/.pip/pip.conf
[global]
index-url = <https://pypi.douban.com/simple>
trusted-host = pypi.doubanio.com

conda 镜像源

1
2
conda config --add/--prepend channels <new_channel>
conda config --append channels <new_channel>

清华源:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

channels 的默认的优先级为: 前排优先
镜像源中 pkgs/main 通常比 pkgs/free 新, 因此将其放在最前.
或使用 channel_priority: false, 见上一节 conda 设置.

e.g.

1
conda config --add channels <https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/>

此时 ~/.condarc 的内容:

channels:
 - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
 - defaults

Troubleshooting

conda 安装时指定源且禁用其他源

1
conda install <packages> -c <channel> --override-channels

Astropy 新版不再支持旧版 Python

1
pip install "astropy<3.0" # for python < 3.5

安装常用包

numpy, scipy, matplotlib

1
2
3
conda install numpy
conda install scipy
conda install matplotlib

 Or

1
2
3
pip install numpy
pip install scipy
pip install matplotlib

配置启动文件 (Ubuntu), 在 ~/.bashrc 中添加:

1
export PYTHONSTARTUP=$HOME/.pythonrc

1
cat ~/.pythonrc
# Python startup file
# ~/.pythonrc
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import os

print(">>> import matplotlib.pyplot as plt")
print(">>> from matplotlib import cm")
print(">>> import numpy as np")
print(">>> import os")

try:
    from pprint import pprint
except ImportError:
    pass
else:
    print(">>> from pprint import pprint")

# Tab completion & history
try:
    import atexit, readline, rlcompleter
except ImportError:
    pass
else:
    readline.parse_and_bind('tab: complete') # Tab completion
    readline.parse_and_bind('''control-l:"    "''') # Indent, since TAB is occupied
    histfile = os.path.join(os.environ['HOME'], '.pyhistory') # History file
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)
    del histfile, readline, rlcompleter
    print("--- Tab completion: [ON] ---")
    print("--- History: [ON] ---")
    print("--- [Tab] -> [Ctrl+L] ---")

Astropy, SunPy

1
2
conda install astropy
conda install sunpy -c conda-forge

 Or

1
2
pip install astropy
pip install sunpy[all] pytest

测试:

1
2
>>> import astropy
>>> astropy.test()
1
2
>>> import sunpy
>>> sunpy.self_test(online=False)

如果出错: '$USER'权限问题, 见下面修改 ~/.sunpy/sunpyrc 说明.

查看包信息:

1
2
%%bash
conda search "conda-forge::sunpy>=4.0"
Loading channels: ...working... done
# Name                       Version           Build  Channel             
sunpy                          4.0.0 py310hde88566_0  conda-forge         
sunpy                          4.0.0  py38h71d37f0_0  conda-forge         
sunpy                          4.0.0  py39hd257fcd_0  conda-forge         
sunpy                          4.0.1 py310hde88566_0  conda-forge         
sunpy                          4.0.1  py38h71d37f0_0  conda-forge         
sunpy                          4.0.1  py39hd257fcd_0  conda-forge         
sunpy                          4.0.2 py310hde88566_0  conda-forge         
sunpy                          4.0.2  py38h71d37f0_0  conda-forge         
sunpy                          4.0.2  py39hd257fcd_0  conda-forge         
sunpy                          4.0.3 py310hde88566_0  conda-forge         
sunpy                          4.0.3  py38h71d37f0_0  conda-forge         
sunpy                          4.0.3  py39hd257fcd_0  conda-forge         
1
2
%%bash
conda search "sunpy 4.0.3 py310hde88566_0" --info
Loading channels: ...working... done
sunpy 4.0.3 py310hde88566_0
---------------------------
file name   : sunpy-4.0.3-py310hde88566_0.tar.bz2
name        : sunpy
version     : 4.0.3
build       : py310hde88566_0
build number: 0
size        : 5.7 MB
license     : BSD-2-Clause
subdir      : linux-64
url         : <https://conda.anaconda.org/conda-forge/linux-64/sunpy-4.0.3-py310hde88566_0.tar.bz2>
md5         : 5831b8fde0edd2aa0ff9f42b237b7ba2
timestamp   : 2022-07-08 16:21:32 UTC
dependencies: 
  - asdf >=2.8.0
  - asdf-astropy >=0.1.1
  - astropy >=4.1.0
  - beautifulsoup4 >=4.8.0
  - cdflib >=0.3.19,!=0.4.0
  - drms >=0.6.1
  - glymur >=0.8.18,!=0.9.0,!=0.9.5
  - h5netcdf >=0.8.1
  - h5py >=3.1.0
  - libgcc-ng >=12
  - matplotlib-base >=3.2.0
  - mpl_animators >=1.0.0
  - numpy >=1.21.6,<2.0a0
  - pandas >=1.0.0
  - parfive >=1.2.0
  - python >=3.10,<3.11.0a0
  - python-dateutil >=2.8.0
  - python_abi 3.10.* *_cp310
  - reproject
  - scikit-image >=0.16.0
  - scipy >=1.3.0
  - setuptools
  - sqlalchemy >=1.3.4
  - tqdm >=4.32.1
  - zeep >=3.4.0

用户文件位置: ~/.sunpy/sunpyrc

1
cat ~/.sunpy/sunpyrc
[general]
time_format = %Y-%m-%d %H:%M:%S
working_dir = /home/lydia/sunpy-downloads

[downloads]
download_dir = /home/lydia/sunpy-downloads/data
sample_dir = /home/lydia/sunpy-downloads/data/sample_data

[database]
url = sqlite:////home/lydia/sunpy-downloads/sunpydb.sqlite
1
2
import sunpy
sunpy.util.system_info()
==============================
sunpy Installation Information
==============================

General
#######
OS: Ubuntu (18.04, Linux 4.19.128-microsoft-standard)
Arch: 64bit, (x86_64)
sunpy: 4.0.3
Installation path: /home/lydia/miniconda3/lib/python3.10/site-packages/sunpy-4.0.3.dist-info

Required Dependencies
#####################
astropy: 5.1
numpy: 1.23.1
packaging: 21.3
parfive: 1.5.1

Optional Dependencies
#####################
asdf: 2.12.0
asdf-astropy: 0.2.1
beautifulsoup4: 4.11.1
cdflib: 0.4.4
dask: 2022.7.0
drms: 0.6.2
glymur: 0.10.1
h5netcdf: 0.0.0
h5py: 3.7.0
matplotlib: 3.5.2
mpl-animators: 1.0.1
pandas: 1.4.1
python-dateutil: 2.8.2
reproject: 0.8
scikit-image: 0.19.3
scipy: 1.8.1
sqlalchemy: 1.4.39
tqdm: 4.64.0
zeep: 4.1.0

设置下载位置:
(参考 http://docs.sunpy.org/en/stable/guide/customization.html?=sunpy.config.set)

e.g. > [downloads]
 download_dir = ...

1
2
3
>>> import sunpy
>>> sunpy.config.set('downloads', 'download_dir',
'/home/<user>/<your_path>/sunpy-downloads/data')

退出后再进入 python 如果发现没有修改成功, 则需要手动修改 ~/.sunpy/sunpyrc 文件.
不能用 '$USER' 代替具体的用户名 '<user>'

Jupyter

1
conda install jupyter -c conda-forge

 Or

1
pip install jupyter

1
2
%%bash
jupyter --path
config:
    /home/lydia/.jupyter
    /home/lydia/.local/etc/jupyter
    /home/lydia/miniconda3/etc/jupyter
    /usr/local/etc/jupyter
    /etc/jupyter
data:
    /home/lydia/.local/share/jupyter
    /home/lydia/miniconda3/share/jupyter
    /usr/local/share/jupyter
    /usr/share/jupyter
runtime:
    /home/lydia/.local/share/jupyter/runtime
  • 设置双击打开 *.ipynb 文件
1
pip install nbopen

再执行:

Linux/BSD:

1
2
3
4
5
6
python3 -m nbopen.install_xdg<br>
```

Windows: <br>
```bash
python3 -m nbopen.install_win<br>

Mac:
 Clone the repository (https://github.com/takluyver/nbopen.git)
 and run ./osx-install.sh

之后即可在文件浏览器中选择文件的打开方式为 Jupyter Notebook.

注意可能会更改 bash 脚本的默认打开方式.

  • Jupyter 插件集合
    (https://github.com/ipython-contrib/jupyter_contrib_nbextensions)
1
conda install jupyter_contrib_nbextensions -c conda-forge

 Or

1
pip install jupyter_contrib_nbextensions
> This also automatically installs the Javascript and CSS files:
jupyter contrib nbextension install --sys-prefix

安装好后将同时启用 Nbextensions 选项卡, 手动选择需要的插件, 或者在命令行启用和禁用:

1
2
3
jupyter nbextension list # 查看
jupyter nbextension enable <path> # <path> 为上述 list 中的 <extension>/main
jupyter nbextension disable <path>

注意用如果 jupyter 安装在 conda 路径, 手动安装插件 (jupyter nbextension install) 时需要指定 --sys-prefix

  • notebook 主题 (https://github.com/dunovank/jupyter-themes)
1
pip install jupyterthemes
  • 演示代码过程的插件 (https://github.com/lgpage/nbtutor)
1
conda install nbtutor -c conda-forge

 Or

1
2
3
pip install nbtutor
jupyter nbextension install --overwrite --py nbtutor
jupyter nbextension enable --py nbtutor

载入:

1
2
# ipython/jupyter
%load_ext nbtutor

使用:
CodeCell 中首行加入下面语句, 然后执行 Cell (numpy 等需要在 Cell 内导入)

1
2
3
4
# ipython/jupyter
%%nbtutor -r -f
# 或
%%nbtutor -r -f -i # 缩减显示

其他

  • PeakUtils (http://peakutils.readthedocs.io/en/latest/)
1
2
3
git clone <https://bitbucket.org/lucashnegri/peakutils.git>
cd peakutils
python setup.py install
1
>>> import peakutils
  • LMfit-py (https://github.com/lmfit/lmfit-py)
1
2
3
conda install lmfit -c conda-forge
# 或
pip install lmfit
1
>>> import lmfit
  • HDF5包 (http://docs.h5py.org/en/latest/index.html)

pandas

1
conda install pytables pandas
 Or
1
pip install tables pandas


h5py (http://docs.h5py.org/en/latest/index.html)
1
conda install h5py  # 推荐. 将同时安装 hdf5, 并得到 h5dump 等命令
 Or
1
pip install h5py  # 需要 sudo apt-get install libhdf5-dev

  • CDF包

安装 spacepy (https://pythonhosted.org/SpacePy)

1
pip install cdflib # <https://github.com/MAVENSDC/cdflib>
  • EVTK (https://bitbucket.org/pauloh/pyevtk)
1
2
3
4
5
sudo apt-get install mercurial  # 得到 hg 命令

hg clone <https://bitbucket.org/pauloh/pyevtk>
cd pyevtk
python setup.py build --debug install
1
hg pull && hg update default # 更新
1
>>> from evtk.hl import gridToVTK
  • Mayavi (http://docs.enthought.com/mayavi/mayavi)

 需要先安装 cython (conda 或 pip)

1
pip install mayavi  # 将同时安装 vtk. (pip 是目前 mayavi 最保险的安装方式)
1
2
# shell
mayavi2