>>> import tan >>> tan.__file__ '/opt/hub/miniconda3/envs/py39/lib/python3.9/site-packages/tan/__init__.py'
|
$ ls
tpf tpf.egg-info
$ pwd
/wks/aitpf/src
import sys
sys.path.append("/wks/aitpf/src")
import tpf
|
|
|
|
|
|
|
导出已安装的包:
pip freeze > requirements.txt
从文件中安装包:
pip install -r requirements.txt
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
本地包安装
pip install /path/to/package_file.whl
仅下载
pip download requests
$ pip download pip==24.0
Collecting pip==24.0
Downloading pip-24.0-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.0-py3-none-any.whl (2.1 MB)
━━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.2/2.1 MB 32.1 kB/s eta 0:01:00
-----------------------------------------------------------------
|
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple package_name
|
导出已安装的包: pip freeze > requirements.txt 这种方式导出的包中,有@符号,表示安装的包是本地的包,而不是从pypi下载的包 [python@localhost v3]$ cat requirements.txt |grep @ zh_core_web_trf @ file:///ai/wks/tousu/code1/flask/models/zh_core_web_trf-3.8.0 |
|
|
|
|
自己安装自己用
pip install --upgrade build 任意新建一个目录,比如build,要求本地磁盘,不能是外挂磁盘 将要打包的文件目录,就是包含__init__.py文件的包目录,COPY到build目录下 (py39) [root@hh build]# ls ta ta,他,其它,第三方等,自建的包 配置 (py39) [root@hh build]# cat pyproject.toml [project] name = "ta" version = "1.0.3" dependencies = [ "cx_Oracle", "pymysql==1.0.2" ] (py39) [root@hh build]# python -m build (py39) [root@hh build]# ll total 4 drwxr-xr-x. 2 root root 118 Aug 2 23:06 dist -rw-r--r--. 1 root root 93 Aug 2 23:03 pyproject.toml drwxr-xr-x. 3 root root 70 Aug 2 22:37 ta drwxr-xr-x. 2 root root 110 Aug 2 23:05 ta.egg-info (py39) [root@hh build]# ll dist/ total 8 -rw-r--r--. 1 root root 2867 Aug 2 23:06 ta-1.0.3-py3-none-any.whl -rw-r--r--. 1 root root 2489 Aug 2 23:05 ta-1.0.3.tar.gz 安装 (py39) [root@hh build]# cd dist/ (py39) [root@hh dist]# ls ta-1.0.3 ta-1.0.3-py3-none-any.whl ta-1.0.3.tar.gz 安装时,会自动卸载旧版本,然后安装新版本 (py39) [root@hh dist]# python -m pip install ta-1.0.3-py3-none-any.whl 正常情况下,到这里就结束了
有时可能会遇到,提示已安装成功,但就是无法引入包, 查看后发现egg信息,但却没有包目录 此时,可手动将包文件cp到site-packages目录下
在包中添加其他文件
看一看打包的文件 (py39) [root@hh build]# cd dist/ (py39) [root@hh dist]# ls ta-1.0.3-py3-none-any.whl ta-1.0.3.tar.gz (py39) [root@hh dist]# (py39) [root@hh dist]# tar -xvf ta-1.0.3.tar.gz ta-1.0.3/ ta-1.0.3/PKG-INFO ta-1.0.3/pyproject.toml ta-1.0.3/setup.cfg ta-1.0.3/ta/ ta-1.0.3/ta/__init__.py ta-1.0.3/ta/db.py ta-1.0.3/ta.egg-info/ ta-1.0.3/ta.egg-info/PKG-INFO ta-1.0.3/ta.egg-info/SOURCES.txt ta-1.0.3/ta.egg-info/dependency_links.txt ta-1.0.3/ta.egg-info/requires.txt ta-1.0.3/ta.egg-info/top_level.txt 再看看自己的源文件,发现以py结尾的文件全打包好了,但少了一个自定义的文件db.db (py39) [root@hh dist]# cd .. (py39) [root@hh build]# ls dist pyproject.toml ta ta.egg-info (py39) [root@hh build]# ll ta total 12 -rwxr-xr-x. 1 root root 0 Aug 2 22:37 __init__.py drwxr-xr-x. 2 root root 64 Aug 2 22:37 __pycache__ -rwxr-xr-x. 1 root root 350 Aug 2 22:37 db.db -rwxr-xr-x. 1 root root 6500 Aug 2 22:37 db.py db.db是一个特殊的文件, 它特殊在虽然是root用户安装的,但要求系统上其他用户有权限使用该文件, 就是自己安装的包,任何一个用户只要能访问python,就有权限对之读写, 所以,它的权限是666, (py39) [root@hh build]# cd ta cp db.db /opt/hub/miniconda3/envs/py39/lib/python3.9/site-packages/ta/ chmod 666 /opt/hub/miniconda3/envs/py39/lib/python3.9/site-packages/ta/db.db ll /opt/hub/miniconda3/envs/py39/lib/python3.9/site-packages/ta/ total 12 需要什么文件手动COPY过去,注意一下权限 另外,新版本安装后,db.db文件还在,权限还是666, 个人猜测,新版本安装只是覆盖相同文件
自己安装让其他人用
请接着看后面的pypi与twine
https://pypi.org/ Python Package Index(Python包索引),是 Python 编程语言的软件存储库。网站版权归Python软件官方基金会所有。 pip install安装的包从该站点(或它的镜像站)下载,由其他开发者上传共享。 PyPA 官网文档地址:https://www.pypa.io/en/latest/ Github地址:https://github.com/pypa Python Packaging Authority(python打包权利机构),开源,专门维护Python打包及发布所用到的一些工具。 pip即由该团队维护
安装
官方文档:https://twine.readthedocs.io/en/stable/ Github:https://github.com/pypa/twine wine是PyPA开发的一个PyPI交互程序,可轻松将已build好的文件上传到PyPI,以供其他人下载。 pip install --upgrade build twine
上传注意事项
包的名字:不能重复 这就跟你在大型网站上注册一个用户名一样,起个不重复...还能有一定含义的名字不那么容易... ta这个名字,代表其他,第三方等,又简短,写着也方便, 它还是本人姓氏的开头两个字母,但github上已经有了... 换个名字重新编辑 mv ta/ tpf 修改pyproject.toml中的名字, 就这两个地方,重新python -m build一下
上传
这一步需要输入在https://pypi.org/上注册的账户与密码
twine upload dist/*
过程如下 :
(base) [xt@genuine-flag-2 build]$ twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: itoracle
WARNING Error getting password from keyring
Traceback (most recent call last):
File "/opt/app/anaconda3/lib/python3.9/site-packages/twine/auth.py", line 74, in get_password_from_keyring
return cast(str, keyring.get_password(system, username))
File "/opt/app/anaconda3/lib/python3.9/site-packages/keyring/core.py", line 55, in get_password
return get_keyring().get_password(service_name, username)
File "/opt/app/anaconda3/lib/python3.9/site-packages/keyring/backends/fail.py", line 25, in get_password
raise NoKeyringError(msg)
keyring.errors.NoKeyringError: No recommended backend was available. Install a recommended 3rd party backend package; or, install the keyrings.alt package if you want to use the non-recommended backends. See
https://pypi.org/project/keyring for details.
Enter your password:
Uploading tpf-1.0.7-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 kB • 00:00 • ?
Uploading tpf-1.0.7.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.6/5.6 kB • 00:00 • ?
View at:
https://pypi.org/project/tpf/1.0.7/
验证
(base) [xt@genuine-flag-2 build]$ pip install tpf
Collecting tpf
Downloading tpf-1.0.7-py3-none-any.whl (4.3 kB)
Collecting pymysql==1.0.2
Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 43.8/43.8 kB 8.8 MB/s eta 0:00:00
Collecting cx-Oracle
Downloading cx_Oracle-8.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (888 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 888.2/888.2 kB 42.1 MB/s eta 0:00:00
Installing collected packages: cx-Oracle, pymysql, tpf
Successfully installed cx-Oracle-8.3.0 pymysql-1.0.2 tpf-1.0.7
(base) [xt@genuine-flag-2 build]$ python
Python 3.9.13 (main, Aug 25 2022, 23:26:10)
[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>from tpf.db import DbConnect
>>> exit()
写了一个连接Oracle,Mysql的demo,后续如果需要不同的库,添加一个配置就可以了,个人使用方便
tpf包使用说明
pip install --no-index --find-links=/ai/app/whl build
下载主包和依赖的 .whl 文件
pip download --only-binary=:all: --platform manylinux2014_x86_64 --python-version 3.9 --implementation cp --abi cp39 -d ./wheelhouse "polars[all]"
--only-binary=:all::强制下载预编译的 .whl 文件(避免源码编译)。
--platform:指定目标平台(如 manylinux2014_x86_64、win_amd64)。
--python-version:指定Python版本(如 3.9)。
-d ./wheelhouse:下载到当前目录的 wheelhouse 文件夹。
离线安装所有 .whl 文件
pip install --no-index --find-links=./wheelhouse polars[all]
--no-index:禁止从PyPI在线查找包。
--find-links=./wheelhouse:从本地目录安装。
下载与安装
pip download --only-binary=:all: --platform manylinux2014_x86_64 --python-version 3.9 --implementation cp --abi cp39 -d ./whl "polars[all]"
pip download \
--only-binary=:all: \
--platform manylinux2014_x86_64 \
--python-version 3.11 \
--implementation cp \
--abi cp311 \
-d ./whl \
"build"
pip install --no-index --find-links=/ai/app/whl build
重新安装 强制从本地重新安装(即使已经装过,也会重新解包、覆盖现有版本): pip install --no-index --find-links=./whl --force-reinstall build 如果还希望把依赖也一并强制重装,可再加 --upgrade --force-reinstall: pip install --no-index --find-links=./whl --upgrade --force-reinstall build
|
|
生成 requirements.txt 文件
先创建一个临时环境,生成精确的依赖列表:
# 创建临时虚拟环境(可选)
python -m venv tmp_venv
source tmp_venv/bin/activate # Linux/macOS
tmp_venv\Scripts\activate # Windows
# 安装并记录依赖
pip install polars[all]
pip freeze > requirements.txt
根据 requirements.txt 下载 .whl 文件
pip download -r requirements.txt -d ./wheelhouse
离线安装
将 wheelhouse 和 requirements.txt 复制到离线机器,运行:
pip install --no-index --find-links=./wheelhouse -r requirements.txt
|
|
平台匹配 确保下载的 .whl 文件与目标机器的Python版本、操作系统(如Linux/macOS/Windows)和架构(如x86_64)一致。 通过 pip debug --verbose 查看本机的兼容标签(如 cp39-cp39-manylinux2014_x86_64)。 pip debug --verbose|grep py311 py311-none-manylinux_2_39_x86_64 py311-none-manylinux_2_38_x86_64 py311-none-manylinux_2_37_x86_64 py311-none-manylinux_2_36_x86_64 ... py311-none-manylinux_2_5_x86_64 py311-none-manylinux1_x86_64 py311-none-linux_x86_64 依赖冲突: 如果依赖包过多,建议仅下载必要的子集(如 polars[pyarrow] 而非 polars[all])。 PyArrow兼容性: polars[all] 会安装 pyarrow,需确保下载的PyArrow版本与系统GLIBC版本兼容(Linux下常见问题)。 Windows平台下载 # 下载适用于Windows的64位Python 3.9的whl文件 pip download --only-binary=:all: --platform win_amd64 --python-version 3.9 -d ./wheelhouse "polars[all]" |
要安装一个新的依赖包名称,比如tpf 确认对应的CUP架构,比如x86_64,同时指定版本 ``` pip download --only-binary=:all: \ --platform manylinux2014_aarch64 \ --python-version 3.10 \ --implementation cp --abi cp310 \ -d ./wheelhouse \ -i https://pypi.tuna.tsinghua.edu.cn/simple \ "tpf==1.0.58" ``` - 依赖包名出现none-any.whl表示通用包,不依赖CPU框架,即linux,windows上通用 - 下面的rak_bm25与tpf都是通用包 ``` cd wheelhouse pip install --no-index --find-links=./ -r requirements.txt ``` (py310) xt@qisan:/python/app/wheelhouse$ pip show tpf Name: tpf Version: 1.0.20 Summary: AI Common Methods Set Home-page: https://gitee.com/star-spinning/tpf Author: Author-email: License: Location: /ai/wks/aitpf/src Requires: cx_Oracle, pymysql Required-by: ``` 安装遇到问题,安装1.0.58版本,安装后完却显示是1.0.54版本,原来卸载出现了问题,残留了1.0.54版本的信息 ``` (py310) xt@qisan:/python/app/micromamba/envs/py310/lib/python3.10/site-packages$ cd tpf tpf/ tpf-1.0.54.dist-info/ tpf-1.0.58.dist-info/ (py310) xt@qisan:/python/app/micromamba/envs/py310/lib/python3.10/site-packages$ rm -rf tpf* ``` 解决方法 ```bash cd /ai/wks/aitpf # 确保你的setup.py或pyproject.toml中的版本号已更新(例如改为1.0.58) pip install -e . ``` **pip安装单个依赖包下载** Linux x86_64(manylinux2014) ``` mkdir /python/app/wheelhouse cd /python/app pip download --only-binary=:all: \ --platform manylinux2014_x86_64 \ --python-version 3.10 \ --implementation cp --abi cp310 \ -d ./wheelhouse \ -i https://pypi.tuna.tsinghua.edu.cn/simple \ "rank_bm25" ``` Linux ARM64(manylinux2014) ``` pip download --only-binary=:all: \ --platform manylinux2014_aarch64 \ --python-version 3.10 \ --implementation cp --abi cp310 \ -d ./wheelhouse \ -i https://pypi.tuna.tsinghua.edu.cn/simple \ "rank_bm25" ``` ``` pip download --only-binary=:all: \ --platform win_amd64 \ --python-version 3.10 \ --implementation cp --abi cp310 \ -d ./wheelhouse \ -i https://pypi.tuna.tsinghua.edu.cn/simple \ "rank_bm25" ``` |
安装遇到问题,安装1.0.58版本,安装后完却显示是1.0.54版本,原来卸载出现了问题,残留了1.0.54版本的信息 ``` (py310) xt@qisan:/python/app/micromamba/envs/py310/lib/python3.10/site-packages$ cd tpf tpf/ tpf-1.0.54.dist-info/ tpf-1.0.58.dist-info/ (py310) xt@qisan:/python/app/micromamba/envs/py310/lib/python3.10/site-packages$ rm -rf tpf* ``` 解决方法 ```bash cd /ai/wks/aitpf # 确保你的setup.py或pyproject.toml中的版本号已更新(例如改为1.0.58) pip install -e . ``` |
pip download --only-binary=:all: \
--platform manylinux2014_aarch64 \
--python-version 3.12 \
--implementation cp --abi cp312 \
-d ./wheelhouse \
-i https://pypi.tuna.tsinghua.edu.cn/simple \
"tpf==1.0.60"
关键修改说明
--python-version 3.10 → 3.12:指定 Python 3.12 版本
--abi cp310 → cp312:对应 Python 3.12 的 ABI 标签
```
xt@qisan:/wks/python/wheelhouse$ pip install --no-index --find-links=./ tpf-1.0.60-py3-none-any.whl
Looking in links: ./
Processing ./tpf-1.0.60-py3-none-any.whl
Installing collected packages: tpf
Attempting uninstall: tpf
Found existing installation: tpf 1.0.60
Uninstalling tpf-1.0.60:
Successfully uninstalled tpf-1.0.60
Successfully installed tpf-1.0.60
```
|
|
python打包发布到PyPI全过程(入门版) Pip离线安装whl文件