[英]setup.py examples?
研究此頁面后:
http://docs.python.org/distutils/builtdist.html
我希望找到一些 setup.py 文件進行研究,以便制作我自己的(目的是制作 Fedora rpm 文件)。
so 社區能否為我指出一些好的例子?
您可能會發現HitchHiker 的包裝指南很有幫助,即使它不完整。 我將從快速入門教程開始。 也可以嘗試在Python Package Index上瀏覽 Python 包。 只需下載 tarball,解壓,然后查看setup.py
文件。 或者更好的是,只需費心查看列出公共源代碼存儲庫(例如托管在 GitHub 或 BitBucket 上的存儲庫)的包。 你一定會在頭版遇到一個。
我最后的建議是去嘗試做一個; 不要害怕失敗。 我真的不明白,直到我開始自己制作它們。 在 PyPI 上創建一個新包很簡單,刪除它也很容易。 因此,創建一個虛擬包並進行播放。
首先閱讀https://packaging.python.org/en/latest/current.html
安裝工具推薦
- 使用 pip 從 PyPI 安裝 Python 包。
- 使用 virtualenv 或 pyvenv 將特定於應用程序的依賴項與共享的 Python 安裝隔離。
- 使用 pip wheel 創建輪分布緩存,目的是 > 加速后續安裝。
- 如果您正在尋找對完全集成的跨平台軟件堆棧的管理,請考慮構建(主要針對 Web 開發社區)或 Hashdist 或 conda(均主要針對科學社區)。
打包工具推薦
- 使用 setuptools 定義項目並創建源代碼分發。
- 使用wheel 項目提供的bdist_wheel setuptools 擴展來創建輪子。 如果您的項目包含二進制擴展,這尤其有益。
- 使用 twine 將分布上傳到 PyPI。
這個anwser已經老化了,確實有python打包世界的救援計划叫
我在這里引用pythonwheels.com :
什么是輪子?
Wheels 是 Python 發行版的新標准,旨在取代雞蛋。 在 pip >= 1.4 和 setuptools >= 0.8 中提供支持。
輪子的優點
正確的 Python 包裝(以及關於輪子)的完整故事在Packaging.python.org 中有介紹
對於科學計算(這也推薦在 Packaging.python.org 上,見上文)我會考慮使用CONDA 打包,它可以被視為構建在 PyPI 和 pip 工具之上的 3rd 方服務。 它也適用於設置您自己的binstar版本,因此我想它可以為復雜的自定義企業包管理提供技巧。
Conda 可以安裝到用戶文件夾中(沒有超級用戶權限)並且像魔術一樣工作
康達安裝
和強大的虛擬環境擴展。
這個選項與 python-distribute.org 相關並且已經過時了(以及站點),所以讓我向您指出一個我喜歡的即用型但緊湊的 setup.py 示例:
這句話取自有關 setup.py 狀態的指南,仍然適用:
我再加一點(來自我)
我建議在嘗試無意識的復制粘貼之前,先了解一下
包裝生態系統(來自 gotgenes 指出的指南)。
互聯網上的大多數示例都以
from distutils.core import setup
但這例如不支持構建egg
python setup.py bdist_egg (以及其他一些
舊功能),這些功能可在
from setuptools import setup
原因是它們已被
棄用。
現在根據指南
警告
請使用 Distribute 包而不是 Setuptools 包,因為此包中存在可以也不會修復的問題。
不推薦使用的 setuptools 將被
distutils2替換,它“將成為 Python 3.3 標准庫的一部分”。
我必須說我喜歡 setuptools 和 egg 並且還沒有完全被 distutils2 的便利所說服。
它需要
pip install Distutils2
並安裝
python -m distutils2.run install
包裝從來都不是微不足道的(人們通過嘗試開發一個新的來學習這一點),所以我認為很多事情都是有道理的。 我只希望這次
能
正確完成。
最小的例子
from setuptools import setup, find_packages
setup(
name="foo",
version="1.0",
packages=find_packages(),
)
文檔中的更多信息
我推薦Python Packaging User Guide示例項目的setup.py 。
Python 打包用戶指南“旨在成為有關如何使用當前工具打包、發布和安裝 Python 發行版的權威資源”。
看看這個完整的例子https://github.com/marcindulak/python-mycli一個小的 python 包。 它基於來自https://packaging.python.org/en/latest/distributing.html 的打包建議,將 setup.py 與 distutils 結合使用,此外還展示了如何創建 RPM 和 deb 包。
項目的 setup.py 包含在下面(請參閱 repo 以獲取完整源代碼):
#!/usr/bin/env python
import os
import sys
from distutils.core import setup
name = "mycli"
rootdir = os.path.abspath(os.path.dirname(__file__))
# Restructured text project description read from file
long_description = open(os.path.join(rootdir, 'README.md')).read()
# Python 2.4 or later needed
if sys.version_info < (2, 4, 0, 'final', 0):
raise SystemExit, 'Python 2.4 or later is required!'
# Build a list of all project modules
packages = []
for dirname, dirnames, filenames in os.walk(name):
if '__init__.py' in filenames:
packages.append(dirname.replace('/', '.'))
package_dir = {name: name}
# Data files used e.g. in tests
package_data = {name: [os.path.join(name, 'tests', 'prt.txt')]}
# The current version number - MSI accepts only version X.X.X
exec(open(os.path.join(name, 'version.py')).read())
# Scripts
scripts = []
for dirname, dirnames, filenames in os.walk('scripts'):
for filename in filenames:
if not filename.endswith('.bat'):
scripts.append(os.path.join(dirname, filename))
# Provide bat executables in the tarball (always for Win)
if 'sdist' in sys.argv or os.name in ['ce', 'nt']:
for s in scripts[:]:
scripts.append(s + '.bat')
# Data_files (e.g. doc) needs (directory, files-in-this-directory) tuples
data_files = []
for dirname, dirnames, filenames in os.walk('doc'):
fileslist = []
for filename in filenames:
fullname = os.path.join(dirname, filename)
fileslist.append(fullname)
data_files.append(('share/' + name + '/' + dirname, fileslist))
setup(name='python-' + name,
version=version, # PEP440
description='mycli - shows some argparse features',
long_description=long_description,
url='https://github.com/marcindulak/python-mycli',
author='Marcin Dulak',
author_email='X.Y@Z.com',
license='ASL',
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
'Development Status :: 1 - Planning',
'Environment :: Console',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.4',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
keywords='argparse distutils cli unittest RPM spec deb',
packages=packages,
package_dir=package_dir,
package_data=package_data,
scripts=scripts,
data_files=data_files,
)
和和 RPM 規范文件或多或少遵循 Fedora/EPEL 打包指南可能如下所示:
# Failsafe backport of Python2-macros for RHEL <= 6
%{!?python_sitelib: %global python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
%{!?python_sitearch: %global python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
%{!?python_version: %global python_version %(%{__python} -c "import sys; sys.stdout.write(sys.version[:3])")}
%{!?__python2: %global __python2 %{__python}}
%{!?python2_sitelib: %global python2_sitelib %{python_sitelib}}
%{!?python2_sitearch: %global python2_sitearch %{python_sitearch}}
%{!?python2_version: %global python2_version %{python_version}}
%{!?python2_minor_version: %define python2_minor_version %(%{__python} -c "import sys ; print sys.version[2:3]")}
%global upstream_name mycli
Name: python-%{upstream_name}
Version: 0.0.1
Release: 1%{?dist}
Summary: A Python program that demonstrates usage of argparse
%{?el5:Group: Applications/Scientific}
License: ASL 2.0
URL: https://github.com/marcindulak/%{name}
Source0: https://github.com/marcindulak/%{name}/%{name}-%{version}.tar.gz
%{?el5:BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)}
BuildArch: noarch
%if 0%{?suse_version}
BuildRequires: python-devel
%else
BuildRequires: python2-devel
%endif
%description
A Python program that demonstrates usage of argparse.
%prep
%setup -qn %{name}-%{version}
%build
%{__python2} setup.py build
%install
%{?el5:rm -rf $RPM_BUILD_ROOT}
%{__python2} setup.py install --skip-build --prefix=%{_prefix} \
--optimize=1 --root $RPM_BUILD_ROOT
%check
export PYTHONPATH=`pwd`/build/lib
export PATH=`pwd`/build/scripts-%{python2_version}:${PATH}
%if 0%{python2_minor_version} >= 7
%{__python2} -m unittest discover -s %{upstream_name}/tests -p '*.py'
%endif
%clean
%{?el5:rm -rf $RPM_BUILD_ROOT}
%files
%doc LICENSE README.md
%{_bindir}/*
%{python2_sitelib}/%{upstream_name}
%{?!el5:%{python2_sitelib}/*.egg-info}
%changelog
* Wed Jan 14 2015 Marcin Dulak <X.Y@Z.com> - 0.0.1-1
- initial version
這是我編寫的實用程序,用於生成帶有有用注釋和鏈接的簡單setup.py文件(模板)。 我希望,它會很有用。
sudo pip install setup-py-cli
要生成setup.py文件,只需在終端中輸入即可。
setup-py
現在setup.py文件應該出現在當前目錄中。
from distutils.core import setup
from setuptools import find_packages
import os
# User-friendly description from README.md
current_directory = os.path.dirname(os.path.abspath(__file__))
try:
with open(os.path.join(current_directory, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
except Exception:
long_description = ''
setup(
# Name of the package
name=<name of current directory>,
# Packages to include into the distribution
packages=find_packages('.'),
# Start with a small number and increase it with every change you make
# https://semver.org
version='1.0.0',
# Chose a license from here: https://help.github.com/articles/licensing-a-repository
# For example: MIT
license='',
# Short description of your library
description='',
# Long description of your library
long_description = long_description,
long_description_context_type = 'text/markdown',
# Your name
author='',
# Your email
author_email='',
# Either the link to your github or to your website
url='',
# Link from which the project can be downloaded
download_url='',
# List of keyword arguments
keywords=[],
# List of packages to install with this one
install_requires=[],
# https://pypi.org/classifiers/
classifiers=[]
)
生成的setup.py 的內容:
這是存儲庫的鏈接。 免費填充以增強解決方案。
在這里你會找到使用 distutils 和 setup.py 的最簡單的例子:
https://docs.python.org/2/distutils/introduction.html#distutils-simple-example
這假設您的所有代碼都在一個文件中,並說明如何打包包含單個模塊的項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.