将自己的python代码提供给别人pip install使用

将自己的python代码提供给别人pip install使用

环境: ubuntu 16.04,Anaconda3 python=3.7


步骤有点多,还有点繁琐,得静下心来一步一步的搞,跟着我手把手教你。

1. 首先创建pypi用户并认证

pypi 官网:https://pypi.org/
在这里插入图片描述
好了记住你的用户名和密码,后面用的着的。
登录完成后会鼠标划到最上面就是这个界面:
在这里插入图片描述

往下再往下,直到出现了这个东西:
在这里插入图片描述
点击红框标记的Options,选择下面的Resend verification email,点击。你的邮箱会受到一条信息,点击click this link to verify your email address验证。
在这里插入图片描述
再回到之前的页面刷新看看,出现这个就是验证成功了。
在这里插入图片描述

2. 编写你的代码并提交到github。

没有安装git的同学运行下面命令安装:

sudo apt install git

关于windows 系统的同学可以参考:Windows系统Git安装教程

github 地址:https://github.com/
进去之后需要登录,当然没注册得需要先注册。

获取密钥

打开刚才安装的git Bash,,输入命令:

ssh-keygen -t rsa -C "your_email"

其中 your_email 是你的github邮箱,运行完这个命令后会生成一个密钥。
Windows系统生成的密钥路径是: C:\Users\username\.ssh\id_rsa.pub ,其中 username 是你电脑系统名,打开id_rsa.pub文件,复制里面的内容。
查看Ubuntu系统生成的密钥:

cat ~/.ssh/id_rsa.pub

Ubuntu系统的同学运行上面的命令,查看密钥,然后复制。

添加密钥

已经添加过的同学可以跳过这一步。
点击右上角头像,选择settings
在这里插入图片描述
再选择 SSH and GPG keys,点击New SSH key。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
然后添加成功了。

在github上创建一个项目。

回到登录进去之后的这个界面,点击红框标记的New,创建你的项目。
在这里插入图片描述
创建你的库,我这就叫test了。
在这里插入图片描述
创建完成后复制红框标记的git连接
在这里插入图片描述

将github上的库拉下来,然后在库里创建自己的项目。

将你刚才创建的git库拉下来:

git clone you_github.git

其中 you_github.git 是你刚才复制的 git 链接。
上面经过了那么多操作,终于可以写久违的代码了,开不开心,激不激动。
创建一个目录,目录格式如下:

test/
    myCode/
        __init__.py
    LICENSE
    setup.py
    README.md

其中test是git clone下来的文件夹。
里面的文件和文件夹都是自己创建的。
myCode目录下的__init__.py是必须要的,可以为空。
话不多说,直接开始写你的代码吧,也可以用我下面demo用的代码。
我这个是基于opencv来做的,没有安装opencv的同学可以用下面命令安装下:

pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple/

用阿里源安装贼鸡儿快,快得飞起。

添加LICENSE

Copyright (c) 2018 The Python Packaging Authority

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

在myCode目录下创建一个python文件,名字就叫mycv.py

mycv.py

# -*- coding: utf-8 -*-
import cv2
import os


def readImg(filePath, channel=3):
    if not os.path.exists(filePath):
        print("FilePath Error: this path is not exists!")
        return None
    if channel == 3:
        return cv2.imread(filePath, 1)
    elif channel == 1:
        return cv2.imread(filePath, 0)
    else:
        print("Channel Error: The channels can only be =3 or =1!")
        return None


def myImgShow(windowName, img, isTransform=False):
    if isTransform:
        cv2.namedWindow(windowName, cv2.WINDOW_FREERATIO)
    if img is None:
        print("Img Error: this img is None!")
        return
    cv2.imshow(windowName, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

setup.py

# -*- coding: utf-8 -*-
import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="my_test_pip_cv",  # 这个名字要独一无二的
    version="1.0",
    author="lzw",
    author_email="your email@qq.com",
    description="A small example package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/Faner227/PIPTEST",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    install_requires=['opencv-python>=4.2.0.34'],
    py_requires=["myCode"],  # 这是你存放python代码的目录
    python_requires='>=3.6',
)

写完后将代码提交上去:

cd you_git_test_path
git add .
git commit
git push
生成whl文件并上传给PYPI

进入你python环境,打开终端,进入到 test 文件夹下。
我python环境是conda 环境,名叫 Test_lzw

conda activate Test_lzw   # 进入python环境
pip install setuptools  -i https://mirrors.aliyun.com/pypi/simple/
pip install wheel -i https://mirrors.aliyun.com/pypi/simple/
python setup.py sdist bdist_wheel

你执行完成后会在 test 文件夹生成build、dist、my_test_pip_cv三个文件夹,目录如下:

test/
	build/
		bdist.win-amd64/
	dist/
		my_test_pip_cv-1.0.tar.gz
		my_test_pip_cv-1.0-py3-none-any.whl
	my_test_pip_cv.egg-info/
	    dependency_links.txt
	    PKG-INFO
	    requires.txt
	    SOURCES.txt
	    top_level.txt
    myCode/
        __init__.py
        mycv.py
    LICENSE
    setup.py
    README.md

上传:

pip install twine -i https://mirrors.aliyun.com/pypi/simple/
python -m twine upload --repository testpypi dist/*

这时候可能会出现这个错误:
在这里插入图片描述
别慌,使用这个命令来上传

twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

然后输入用户名和密码,用户名密码是你注册PIPY时的用户名密码!!!
在这里插入图片描述
这样就上传成功了!接下来我们测试pip install我们的包,试试能不能运行我们写的代码。
Ubuntu 如果还报错就创建一个~/.pypirc文件:

vim ~/.pypirc

文件内容如下:

[distutils]
index-servers = pypi
    testpypi


[pypi]
repository: https://upload.pypi.org/legacy/
username: <username>
password: <password>


[testpypi]
repository:https://test.pypi.org/legacy/
username: <username>
password: <password>

其中 <> 括起来的 username 和 password 是pypi注册的用户名和密码,注意:把<>这个括号删掉!!
Windows 报错就在C:\Users\dev目录下创建一个.pypirc文件,内容和 Ubuntu 的一样。
然后再执行命令:

twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

出现上面成功的样子就可以继续往下走了。

测试PIP包

试试pip install

pip install my_test_pip_cv

这里需要注意一点,你上传的这个包不能使用国内源安装,所以可能会出现网络超时错误,多下载几次咯。如果你使用源安装会出现找不到包和版本的错误。
这个my_test_pip_cv是你setup.py 里面的 name 的值。
试试随便在一个文件夹下创建一个python文件,并导入包。为了方便我就直接在桌面创建了。

Ubuntu 测试

cd /home/dev/Desktop
vim testPIP.py

放入以下内容

from myCode import mycv
# myCode 是你放python文件的文件夹,setup.py里的py_requires有说明
# mycv 是之前创建的python文件
mycv.readImg("tesfdsfdsfds")

按Esc后再输入Shift+;,输入wq保存退出

python testPIP.py 

打印结果:
FilePath Error: this path is not exists!
成功!!!

Windows 测试

也一样在桌面创建一个python 文件,放入代码:

from myCode import mycv
# myCode 是你放python文件的文件夹,setup.py里的py_requires有说明
# mycv 是之前创建的python文件
mycv.readImg("tesfdsfdsfds")

运行得到结果:
FilePath Error: this path is not exists!
成功!!!

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值