pipenvのlibrary作成
自身でライブラリを作成する方法と、そのライブラリをpipenvでinstallするまでの話
まず適当な、リポジトリを作成します参考 https://github.com/kiwamunet/testpipenv
その次に、setup.pyを作成しましょう
基本的に、リポジトリの所在や、パッケージ名を定義します。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import os
import sys
from setuptools import setup
name = "kuroko"
package = "testpipenv"
description = "testpipenv description"
url = "https://github.com/kiwamunet/testpipenv"
author = "kuroko"
author_email = "kuroko@gmail.com"
license = "Apache"
def get_version(package):
"""
Return package version as listed in `__version__` in `init.py`.
"""
init_py = open(os.path.join(package, "__init__.py")).read()
return re.search("^__version__ = ['\"]([^'\"]+)['\"]", init_py, re.MULTILINE).group(
1
)
def get_packages(package):
"""
Return root package and all sub-packages.
"""
return [
dirpath
for dirpath, dirnames, filenames in os.walk(package)
if os.path.exists(os.path.join(dirpath, "__init__.py"))
]
def get_package_data(package):
"""
Return all files under the root package, that are not in a
package themselves.
"""
walk = [
(dirpath.replace(package + os.sep, "", 1), filenames)
for dirpath, dirnames, filenames in os.walk(package)
if not os.path.exists(os.path.join(dirpath, "__init__.py"))
]
filepaths = []
for base, filenames in walk:
filepaths.extend([os.path.join(base, filename) for filename in filenames])
return {package: filepaths}
version = get_version(package)
if sys.argv[-1] == "publish":
if os.system("pip freeze | grep wheel"):
print(
"wheel not installed.\nUse `pip install wheel`.\nPress <Enter> to continue or <Ctrl+C> to abort."
)
input()
os.system("python setup.py sdist bdist_wheel")
os.system("twine upload dist/*")
print("You probably want to also tag the version now:")
print(" git tag -a v{0} -m 'version {0}'".format(version))
print(" git push origin v{0}".format(version))
sys.exit()
setup(
name=name,
version=version,
url=url,
license=license,
description=description,
long_description=open("README.rst").read(),
author=author,
author_email=author_email,
packages=get_packages(package),
package_data=get_package_data(package),
install_requires=[],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Framework :: Django",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Natural Language :: English",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Topic :: Internet :: WWW/HTTP",
],
)
次に、実際にライブラリの中身を書いていきましょう。
class Echo:
def __init__(self):
pass
@classmethod
def echo(cls, message):
print(message)
今度は、自分で開発中のプロジェクトから参照してみましょう
pipenvでインストールします。
Pipfileに以下を追加します。URLを書くだけでOK
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
// ここに追記
pipenv install git+https://github.com/kiwamunet/testpipenv.git#egg=testpipenv
[dev-packages]
[requires]
python_version = "3.7"
[scripts]
_manage = "python manage.py"
makemigrations = "python manage.py makemigrations"
migrate = "python manage.py migrate"
migrate-reset = "python manage.py migrate users zero"
runserver = "python manage.py runserver"
手動でも大丈夫
pipenv install git+https://github.com/kiwamunet/testpipenv.git#egg=testpipenv
Installing git+https://github.com/kiwamunet/testpipenv.git#egg=testpipenv…
Warning: You installed a VCS dependency in non-editable mode. This will work fine, but sub-dependencies will not be resolved by $ pipenv lock.
To enable this sub-dependency functionality, specify that this dependency is editable.
Collecting testpipenv from git+https://github.com/kiwamunet/testpipenv.git#egg=testpipenv
Cloning <https://github.com/kiwamunet/testpipenv.git> to /private/var/folders/b9/wz436_6130gf2qdzl5r22c9r0000gp/T/pip-install-en53e8wk/testpipenv
Building wheels for collected packages: kuroko, kuroko
Building wheel for kuroko (setup.py): started
Building wheel for kuroko (setup.py): finished with status 'done'
Created wheel for kuroko: filename=kuroko-0.1-cp37-none-any.whl size=1725 sha256=9838205845d407063234c78394df90724956de7b50afcde4bf41c63603613b26
Stored in directory: /private/var/folders/b9/wz436_6130gf2qdzl5r22c9r0000gp/T/pip-ephem-wheel-cache-li64zkto/wheels/19/dc/32/43c507d7b4af4a67d72502aa694a34903fd2d94bd93c74ae99
Building wheel for kuroko (setup.py): started
Building wheel for kuroko (setup.py): finished with status 'error'
Running setup.py clean for kuroko
Successfully built kuroko
Failed to build kuroko
Installing collected packages: kuroko
Found existing installation: kuroko 0.1
Uninstalling kuroko-0.1:
Successfully uninstalled kuroko-0.1
Successfully installed kuroko-0.1
Adding testpipenv to Pipfile's [packages]…
✔ Installation Succeeded
Installing dependencies from Pipfile.lock (984041)…
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 71/71 — 00:00:08
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
あとは、コードでimportしてあげればよかと
from testpipenv.testpipenv import Echo
class Test:
@classmethod
def test_logs(cls, message):
print(message)
Echo.echo(message) <--これが使ったやつ
この記事が気に入ったらサポートをしてみませんか?