1.Python
1.基础
跟着廖雪峰学习python即可:https://www.liaoxuefeng.com/
开发工具:推荐 pycharm
基本语法:
- 1基本语法
- 2基本数据类型
- 3运算符
- 4控制语句
- 6数据结构
- 7模块
- 8文件
- 9异常处理
- 10面向对象
- 11标准库
- 12CGI
- 13数据库
- 14网络编程
- 15多线程
- 16json
- 16XML解析
- 17时间
- 18高级语法糖
- 19爬虫
2.pip
2.1.介绍
pip 是一个通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。
可以在官网查看python支持的库有哪些
2.2.自行安装
pip 已内置于 Python 3.4 和 2.7 及以上版本,其他版本需另行安装。
在ubuntu下pip需要自行安装
- python3:sudo apt install python3-pip
- python2:sudo apt install python2-pip
注:安装前最好sudo apt-get update一下
2.3.安装库的目录
可通过pip3 和 pip分别为python3.5和python2.7安装各自的库
python的可执行文件的目录一般在/usr/bin下,通过apt-get安装的应用一般会在这个目录。自行安装的一般在/usr/local/bin下
python3.5的自带库目录在
/usr/lib/python3/dist-packages
/usr/lib/python3.5/
python2.7的自带库目录在
/usr/lib/python2.6/dist-packages
/usr/lib/python2.7/
通过pip安装的模块目录在
/usr/local/lib/python3.5/dist-packages
/usr/local/lib/python2.7/site-packages
或者
/usr/local/lib/python2.7/dist-packages
2.4.常用参数
1.参数的帮助文档
C:\Users\zhangxue>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to
WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
(a)bort).
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the
certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output
2.常用参数
1、判断是否已安装pip:
pip --version # Python2.x 版本命令
pip3 --version # Python3.x 版本命令
2、显示版本和路径:pip --version
3、安装包:
pip install SomePackage # 最新版本
pip install SomePackage==1.0.4 # 指定版本
pip install 'SomePackage>=1.0.4' # 最小版本
4、升级包:pip install --upgrade SomePackage(升级指定的包,通过使用==, >=, <=, >, < 来指定一个版本号。)
5、卸载包:pip uninstall SomePackage
6、搜索包:pip search SomePackage
7、查看指定包的详细信息:pip show -f SomePackage
8、列出已安装的包:pip list
2.5.离线安装
1.需要再一个有网络的环境下载安装包
pip download -d 路径 包名 -i 镜像
pip download -d ./pandas pandas -i https://pypi.tuna.tsinghua.edu.cn/simple/
2.在路径文件夹中创建 requirements.txt 文件,在里面写入要安装的包名。例如
pandas-2.2.2-cp35-cp35m-manylinux1_x86_64.whl
3.将pandas文件夹打包好上传到目标服务器。解压后执行安装命令
pip install -r requirements.txt
2.6.常见问题
问题1:connection timeout
若由于一些局域网的原因,使用 pip 出现 "connection timeout",连接超时可以使用国内的镜像网站下载:
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com packagename # packagename是要下载的包的名字
问题2:Permission denied
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/lib/python3.5/dist-packages/pylab.py' Consider using the --user option or check the permissions.
我们在实际工作中,一会不允许使用root权限,这就导致我们无法正常安装python的包。解决方案有以下2个:
1、sudo pip3安装。但是需要有root权限
sudo pip3获取了管理员权限,把第三方包安装在/usr/local/lib/python3.5/dist-packages/路径下,可供任何用户使用。
2、pip3 --user安装
根据报错信息提示,可以使用 pip3 install 包名 --user 的方法进行安装。
该包自动安装在/home/用户名/.local/lib/python3.5/site-packages/路径下,仅供该用户使用。
3.实现原理
不同于Java,现在很多开发语言把高级的数据结构封装到了虚拟机中,对用户是黑盒不用关系。
1.列表实现原理
列表的结构和Java的ArrayList一样,通过数组和设置最大容量实现,让元素超过容量则创建新数组,copy旧数据过去,实现扩容。
2.字典实现原理
hash桶:也就是 数组 + 数组。结构比较复杂
3.垃圾回收
4.项目管理工具
UV
uv是一个用 Rust 编写的极快的 Python 包和项目管理器。源码: https://github.com/astral-sh/uv
环境相关命令
1. 创建虚拟环境:uv venv create myenv
2. 激活虚拟环境:uv venv activate myenv
3. 列出所有虚拟环境:uv venv list
4. 删除虚拟环境 :uv venv remove myenv
依赖管理
1. 安装依赖: uv install requests
此命令可安装 `requests` 包,还可以指定版本,像 `uv install requests==2.28.1` 这样。
2. 安装项目所有依赖:uv install
在项目根目录运行该命令,会依据 `pyproject.toml` 或者 `requirements.txt` 文件来安装所有依赖。
3. 卸载依赖:uv uninstall requests
4. 更新依赖:uv update requests
运行此命令可将 `requests` 包更新到最新版本。若要更新所有依赖,可使用 `uv update`。
其他命令
1. 初始化新项目:uv init
该命令会在当前目录初始化一个新的 Python 项目,同时创建 `pyproject.toml` 文件。
2. 运行自定义脚本:uv run test
假定 pyproject.toml 文件中有如下配置:
toml
[tool.uv.scripts]
test = "pytest"
Hatch
Hatch 是一个综合性的 Python 项目管理工具,提供了虚拟环境管理、依赖管理、项目构建和发布等功能。它使用 hatch.toml 文件来配置项目,可以方便地管理多个 Python 版本和环境。
- 安装:可以通过
pip进行安装:pip install hatch - 使用示例:创建一个新的 Python 项目可以使用如下命令:
hatch new myproject
PDM
PDM 是一个现代的 Python 包管理工具,采用了 PEP 582 规范,允许在项目根目录下创建一个本地的 __pypackages__ 目录来存储包,而不需要创建虚拟环境。 它支持依赖解析、版本锁定和项目发布等功能。
- 安装:通过以下命令安装:
pip install pdm - 使用示例:在项目中添加依赖:
pdm add requests
- 本文作者:zhangxue
- 本文链接:https://lucksnow1989.github.io/article/13.脚本语言/python/1.python.html
- 版权声明:本文为本人原创文章,遵循 CC 4.0 BY-NC-SA版权协议,转载请附上原文出处链接和本声明。