Python包的安装#

首先,我们需要安装一下,今天演示所需要的包,正常情况下,以numpy为例,我们可以用以下方式安装包:

  • 在指定环境下,在命令行中输入pip install numpy

  • 在指定环境下,在命令行中输入python -m pip install numpy

  • 或者使用conda安装,在命令行中输入conda install numpy --name [你的环境名称]

请注意,在国内使用上述命令,可能速度较慢,我们建议使用清华源,可以显著提升安装速度:

在jupyter notebook中,提供了更加便捷的方式安装这些包:

!pip install --upgrade pip
!pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
!pip install numpy matplotlib pandas scikit-learn sympy scipy scikit-opt
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pip in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (23.2.1)
Writing to /home/xuyisen/.config/pip/pip.conf
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: numpy in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (1.26.0)
Requirement already satisfied: matplotlib in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (3.8.0)
Requirement already satisfied: pandas in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (2.1.1)
Requirement already satisfied: scikit-learn in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (1.3.1)
Requirement already satisfied: sympy in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (1.12)
Requirement already satisfied: scipy in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (1.11.3)
Requirement already satisfied: scikit-opt in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (0.6.6)
Requirement already satisfied: contourpy>=1.0.1 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (1.1.1)
Requirement already satisfied: cycler>=0.10 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (4.43.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (1.4.5)
Requirement already satisfied: packaging>=20.0 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (23.2)
Requirement already satisfied: pillow>=6.2.0 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (10.0.1)
Requirement already satisfied: pyparsing>=2.3.1 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (3.1.1)
Requirement already satisfied: python-dateutil>=2.7 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from pandas) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from pandas) (2023.3)
Requirement already satisfied: joblib>=1.1.1 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from scikit-learn) (1.3.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from scikit-learn) (3.2.0)
Requirement already satisfied: mpmath>=0.19 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from sympy) (1.3.0)
Requirement already satisfied: six>=1.5 in /home/xuyisen/anaconda3/envs/JBtest/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)

在安装完成后,我们需要导入包,才可以使用。我们可以使用import来导入我们需要的包,下面展示了几种import的用法

import math    #导入后,调用时需要加入前缀
print(math.pi)

from math import pi   #可以直接使用pi,不需要前缀
print(pi)

import math as ma   #同第一种,但是使用别名
print(ma.pi)

from math import pi as yzl   #同第二种,但是使用别名
print(yzl)
3.141592653589793
3.141592653589793
3.141592653589793
3.141592653589793