Python安装 ¶
1.操作系统环境 ¶
- 系统环境: Ubuntu 22.04
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.5 LTS"
2.Python版本 ¶
- Python版本(系统自带): 3.10.12
$ python3 --version
Python 3.6.15
3.pip版本和升级 ¶
- 检查当前
pip
版本
$ pip --version
pip 24.2 from ~/.local/lib/python3.10/site-packages/pip (python 3.10)
$ pip3 --version
pip 24.2 from ~/.local/lib/python3.10/site-packages/pip (python 3.10)
- 升级
pip
版本
pip install --upgrade pip
pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
4.pip国内源 ¶
- https://mirrors.aliyun.com/pypi/simple/
- https://pypi.tuna.tsinghua.edu.cn/simple/
- http://pypi.doubanio.com/simple/
- https://mirrors.cloud.tencent.com/pypi/simple/
5.pip配置 ¶
显示当前pip的配置。
pip config list
运行命令pip config edit --global --editor=code
来创建/编辑pip
配置文件pip.ini
,并制定用VSCode打开配置文件。
- 在微软Windows平台,
pip.ini
文件默认存放路径是C:\ProgramData\pip\pip.ini
。 - 在Linux平台,
pip.ini
文件默认存放路径是/etc/pip.conf
。
下面的实例添加了公司内网的Python源,其中username和token需要参考内网Python源使用方法。默认只有https://pypi.org/simple
。
extra-index-url = https://{USERNAME}:{TOKEN}@<YOUR REPOSITORY HOST>/artifactory/api/pypi/deploy-releases-hyperspace-pypi/simple
https://{USERNAME}:{TOKEN}@<YOUR REPOSITORY HOST>/artifactory/api/pypi/deploy.releases.pypi/simple
https://pypi.org/simple
trusted-host = <YOUR REPOSITORY HOST>
如果在编辑/etc/pip.conf
文件时遇到权限问题,可以参考下面方法进行调整。
$ ll /etc/pip.conf
-rw------- 1 root root 314 Aug 29 16:58 /etc/pip.conf
$ sudo chmod og+rw /etc/pip.conf
$ ll /etc/pip.conf
-rw-rw-rw- 1 root root 314 Aug 29 16:58 /etc/pip.conf
6.安装常用Python包 ¶
下面命令是通过预定义文件requirements.txt
来安装Python包。
pip install -r requirements.txt
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
安装指定包的最新版本。
pip install package_name
pip install -U package_name
显示指定包的已安装版本。
pip show package_name
pip show package_name | grep "Version:"
列出当前有更新版本的包,并安装指定版本的包。
pip list --outdated
pip install package_name==specific_version
升级指定的包。
pip install --upgrade package_name
创建文件requirements.txt
并批量安装Python包。下面的示例中只指定了包名称,未指定包的版本,即安装最新版。
$ cat > requirements.txt << EOF
sqlite-utils
numpy
matplotlib
scikit-learn
pandas
seaborn
selenium
pandas-datareader
beautifulsoup4
statsmodels
black
pipdeptree
python-dotenv
flask
mkdocs
python-minifier
mkdocs-material
mkdocs-material-extensions
mkdocs-minify-plugin
mkdocs-mermaid2-plugin
mkdocs-awesome-pages-plugin
EOF
$ pip install -r requirements.txt
在Ubuntu 20.04中安装mkdocs-material
可能会遇到下面的错误。
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/usr/local/bin/normalizer'
Consider using the `--user` option or check the permissions.
按照提示,执行命令pip3 install mkdocs-material --user
来重新安装。
提示:
- 系统级别的Python包安装路径:
/usr/lib/python3/dist-packages
- 用户级别的Python包安装路径:
$HOME/.local/lib/python3.10/site-packages
Python will follow the sequence of the output of sys.path
below to search and call python packages.
Python会按照下面sys.path
输出的路径顺序来搜索和调用安装包。
$ python3
Python 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '<YOUR HOME>/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']