>>> 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 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包使用说明
python打包发布到PyPI全过程(入门版) Pip离线安装whl文件