setup.py is a Python file, the presence of which is an indication that the module/package you are about to install has likely been packaged and distributed with Distutils, which is the standard for distributing Python Modules.
It helps to install a python package foo on your machine (can also be in virtualenv) so that you can import the package foo from other projects and also from [I]Python prompts.
It does the similar job of pip, easy_install etc.,
Using setup.py
Let's start with some definitions:
Package - A folder/directory that contains __init__.py file. Module - A valid python file with .py extension. Distribution - How one package relates to other packages and modules.
Let's say you want to install a package named foo. Then you do,
Instead, if you don't want to actually install it but still would like to use it. Then do,
$ python setup.py develop
This command will create symlinks to the source directory within site-packages instead of copying things. Because of this, it is quite fast (particularly for large packages).
from setuptools import setup
setup(
name='foo',
version='1.0',
description='A useful module',
author='Man Foo',
author_email='[email protected]',
packages=['foo'], #same as name
install_requires=['wheel', 'bar', 'greek'], #external packages as dependencies
scripts=[
'scripts/cool',
'scripts/skype',
]
)
Add more stuff to (setup.py) & make it decent:
from setuptools import setup
with open("README", 'r') as f:
long_description = f.read()
setup(
name='foo',
version='1.0',
description='A useful module',
license="MIT",
long_description=long_description,
author='Man Foo',
author_email='[email protected]',
url="http://www.foopackage.example/",
packages=['foo'], #same as name
install_requires=['wheel', 'bar', 'greek'], #external packages as dependencies
scripts=[
'scripts/cool',
'scripts/skype',
]
)
The long_description is used in pypi.org as the README description of your package.
And finally, you're now ready to upload your package to PyPi.org so that others can install your package using pip install yourpackage.
At this point there are two options.
publish in the temporarytest.pypi.org server to make oneself familiarize with the procedure, and then publish it on the permanentpypi.org server for the public to use your package.
publish straight away on the permanentpypi.org server, if you are already familiar with the procedure and have your user credentials (e.g., username, password, package name)
It will take few minutes for the package to appear on test.pypi.org. Once you're satisfied with it, you can then upload your package to the real & permanent index of pypi.org simply with:
$ twine upload dist/*
Optionally, you can also sign the files in your package with a GPG by:
setup.py is Python's answer to a multi-platform installer and make file.
If you’re familiar with command line installations, then make && make install translates to python setup.py build && python setup.py install.
Some packages are pure Python, and are only byte compiled. Others may contain native code, which will require a native compiler (like gcc or cl) and a Python interfacing module (like swig or pyrex).
setup.py is a Python script that is usually shipped with libraries or programs, written in that language. It's purpose is the correct installation of the software.
Many packages use the distutils framework in conjuction with setup.py.
python setup.py build
python setup.py install
python setup.py sdist <distname> upload [-r urltorepo] (to upload package to pypi or local repo)
还有很多其他命令可以与 setup.py 一起使用。寻求帮助
python setup.py --help-commands
setup.py can be used in two scenarios , First, you want to install a Python package. Second, you want to create your own Python package. Usually standard Python package has couple of important files like setup.py, setup.cfg and Manifest.in. When you are creating the Python package, these three files will determine the (content in PKG-INFO under egg-info folder) name, version, description, other required installations (usually in .txt file) and few other parameters. setup.cfg is read by setup.py while package is created (could be tar.gz ). Manifest.in is where you can define what should be included in your package. Anyways you can do bunch of stuff using setup.py like
python setup.py build
python setup.py install
python setup.py sdist <distname> upload [-r urltorepo] (to upload package to pypi or local repo)
There are bunch of other commands which could be used with setup.py . for help
setup.py is a Python file like any other. It can take any name, except by convention it is named setup.py so that there is not a different procedure with each script.
Most frequently setup.py is used to install a Python module but server other purposes:
Modules:
Perhaps this is most famous usage of setup.py is in modules. Although they can be installed using pip, old Python versions did not include pip by default and they needed to be installed separately.
If you wanted to install a module but did not want to install pip, just about the only alternative was to install the module from setup.py file. This could be achieved via python setup.py install. This would install the Python module to the root dictionary (without pip, easy_install ect).
This method is often used when pip will fail. For example if the correct Python version of the desired package is not available via pipperhaps because it is no longer maintained, , downloading the source and running python setup.py install would perform the same thing, except in the case of compiled binaries are required, (but will disregard the Python version -unless an error is returned).
Another use of setup.py is to install a package from source. If a module is still under development the wheel files will not be available and the only way to install is to install from the source directly.
Building Python extensions:
When a module has been built it can be converted into module ready for distribution using a distutils setup script. Once built these can be installed using the command above.
A setup script is easy to build and once the file has been properly configured and can be compiled by running python setup.py build (see link for all commands).
Once again it is named setup.py for ease of use and by convention, but can take any name.
Cython:
Another famous use of setup.py files include compiled extensions. These require a setup script with user defined values. They allow fast (but once compiled are platform dependant) execution. Here is a simple example from the documentation:
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = 'Hello world app',
ext_modules = cythonize("hello.pyx"),
)
This can be compiled via python setup.py build
Cx_Freeze:
Another module requiring a setup script is cx_Freeze. This converts Python script to executables. This allows many commands such as descriptions, names, icons, packages to include, exclude ect and once run will produce a distributable application. An example from the documentation:
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup( name = "guifoo",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("guifoo.py", base=base)])
This can be compiled via python setup.py build.
So what is a setup.py file?
Quite simply it is a script that builds or configures something in the Python environment.
A package when distributed should contain only one setup script but it is not uncommon to combine several together into a single setup script. Notice this often involves distutils but not always (as I showed in my last example). The thing to remember it just configures Python package/script in some way.
It takes the name so the same command can always be used when building or installing.
When you download a package with setup.py open your Terminal (Mac,Linux) or Command Prompt (Windows). Using cd and helping you with Tab button set the path right to the folder where you have downloaded the file and where there is setup.py :
To make it simple, setup.py is run as "__main__" when you call the install functions the other answers mentioned. Inside setup.py, you should put everything needed to install your package.
Common setup.py functions
The following two sections discuss two things many setup.py modules have.
setuptools.setup
This function allows you to specify project attributes like the name of the project, the version.... Most importantly, this function allows you to install other functions if they're packaged properly. See this webpage for an example of setuptools.setup
These attributes of setuptools.setup enable installing these types of packages:
In an ideal world, setuptools.setup would handle everything for you. Unfortunately this isn't always the case. Sometimes you have to do specific things, like installing dependencies with the subprocess command, to get the system you're installing on in the right state for your package. Try to avoid this, these functions get confusing and often differ between OS and even distribution.
To install a Python package you've downloaded, you extract the archive and run the setup.py script inside:
python setup.py install
To me, this has always felt odd. It would be more natural to point a package manager at the download, as one would do in Ruby and Nodejs, eg. gem install rails-4.1.1.gem
A package manager is more comfortable too, because it's familiar and reliable. On the other hand, each setup.py is novel, because it's specific to the package. It demands faith in convention "I trust this setup.py takes the same commands as others I have used in the past". That's a regrettable tax on mental willpower.
I'm not saying the setup.py workflow is less secure than a package manager (I understand Pip just runs the setup.py inside), but certainly I feel it's awkard and jarring. There's a harmony to commands all being to the same package manager application. You might even grow fond it.
发布评论
评论(10)
setup.py
是一个 Python 文件,它的存在表明您要安装的模块/包可能已通过 Distutils,这是分发 Python 模块的标准。这使您可以轻松地安装 Python 包。通常这样写就足够了:
pip
将使用setup.py
来安装你的模块。避免直接调用setup.py
。setup.py
is a Python file, the presence of which is an indication that the module/package you are about to install has likely been packaged and distributed with Distutils, which is the standard for distributing Python Modules.This allows you to easily install Python packages. Often it's enough to write:
pip
will usesetup.py
to install your module. Avoid callingsetup.py
directly.它有助于在您的计算机上安装 python 包
foo
(也可以在virtualenv
中),以便您可以从其他项目导入包foo
以及 [I]Python 提示。它的作用与
pip
、easy_install
等类似,使用
setup.py
让我们从一些定义开始:
包 - 包含
__init__.py
文件的文件夹/目录。模块 - 具有
.py
扩展名的有效 python 文件。分发 - 一个包如何与其他包和模块相关。
假设您要安装一个名为
foo
的包。 那么您就可以这样做如果您不想实际安装它但仍想使用它, 。然后执行此命令,
该命令将在站点包中创建指向源目录的符号链接,而不是复制内容。因此,它的速度相当快(特别是对于大包)。
创建
setup.py
如果您的包树类似于,
那么,您可以在
setup.py
中执行以下操作> 脚本,以便可以将其安装在某些计算机上:相反,如果您的包树更复杂,如下所示:
那么,在这种情况下,您的
setup.py
将就像:向 (
setup.py
) 添加更多内容 &让它得体:long_description
在pypi.org中用作包的自述文件描述。最后,您现在已准备好将软件包上传到 PyPi.org,以便其他人可以使用
pip 安装你的包
。此时有两个选择。
一旦您的包名称在 pypi 中注册, 。 org,没有人可以声明或使用它。 Python 打包建议使用 twine 包< /a> 用于上传(将包上传到 PyPi)。因此,
第一步是使用以下方法在本地构建发行版:
然后使用
twine
上传到test.pypi.org
或pypi.org
:包需要几分钟时间才会出现在 <代码>test.pypi.org。一旦您对它感到满意,您就可以将您的包上传到真实的&amp; pypi.org 的永久索引只需:
或者,您还可以使用 < 来签署包中的文件code>GPG 作者:
额外阅读:
在此处查看来自真实项目的示例
setup.py
:torchvision-setup.py
PEP 517,设置工具
为什么要缠绕?使用麻线
It helps to install a python package
foo
on your machine (can also be invirtualenv
) so that you can import the packagefoo
from other projects and also from [I]Python prompts.It does the similar job of
pip
,easy_install
etc.,Using
setup.py
Let's start with some definitions:
Package - A folder/directory that contains
__init__.py
file.Module - A valid python file with
.py
extension.Distribution - How one package relates to other packages and modules.
Let's say you want to install a package named
foo
. Then you do,Instead, if you don't want to actually install it but still would like to use it. Then do,
This command will create symlinks to the source directory within site-packages instead of copying things. Because of this, it is quite fast (particularly for large packages).
Creating
setup.py
If you have your package tree like,
Then, you do the following in your
setup.py
script so that it can be installed on some machine:Instead, if your package tree is more complex like the one below:
Then, your
setup.py
in this case would be like:Add more stuff to (
setup.py
) & make it decent:The
long_description
is used in pypi.org as the README description of your package.And finally, you're now ready to upload your package to PyPi.org so that others can install your package using
pip install yourpackage
.At this point there are two options.
Once your package name is registered in pypi.org, nobody can claim or use it. Python packaging suggests the twine package for uploading purposes (of your package to PyPi). Thus,
the first step is to locally build the distributions using:
then using
twine
for uploading either totest.pypi.org
orpypi.org
:It will take few minutes for the package to appear on
test.pypi.org
. Once you're satisfied with it, you can then upload your package to the real & permanent index of pypi.org simply with:Optionally, you can also sign the files in your package with a
GPG
by:Bonus Reading:
See a sample
setup.py
from a real project here:torchvision-setup.py
PEP 517, setuptools
why twine? using twine
setup.py
是 Python 对多平台安装程序和make
文件的回应。如果您熟悉命令行安装,那么 make && make install 转换为
python setup.py build && python setup.py 安装
。有些包是纯Python的,并且仅是字节编译的。其他可能包含本机代码,这将需要本机编译器(例如
gcc
或cl
)和 Python 接口模块(例如swig
或>耐热玻璃
)。setup.py
is Python's answer to a multi-platform installer andmake
file.If you’re familiar with command line installations, then
make && make install
translates topython setup.py build && python setup.py install
.Some packages are pure Python, and are only byte compiled. Others may contain native code, which will require a native compiler (like
gcc
orcl
) and a Python interfacing module (likeswig
orpyrex
).如果您下载的包的根文件夹中包含“setup.py”,则可以通过运行来安装它。
如果您正在开发项目并且想知道该文件的用途,请检查 有关编写安装脚本的 Python 文档
If you downloaded package that has "setup.py" in root folder, you can install it by running
If you are developing a project and are wondering what this file is useful for, check Python documentation on writing the Setup Script
setup.py 是一个 Python 脚本,通常与用该语言编写的库或程序一起提供。其目的是正确安装软件。
许多软件包将
distutils
框架与setup.py
结合使用。http://docs.python.org/distutils/
setup.py
is a Python script that is usually shipped with libraries or programs, written in that language. It's purpose is the correct installation of the software.Many packages use the
distutils
framework in conjuction withsetup.py
.http://docs.python.org/distutils/
setup.py可以在两种场景下使用,一是要安装Python包。其次,您想要创建自己的 Python 包。通常标准 Python 包有几个重要的文件,如 setup.py、setup.cfg 和 Manifest.in。当您创建Python包时,这三个文件将确定(egg-info文件夹下的PKG-INFO中的内容)名称、版本、描述、其他所需的安装(通常在.txt文件中)和一些其他参数。 setup.cfg 在创建包时由 setup.py 读取(可以是 tar.gz )。您可以在 Manifest.in 中定义包中应包含的内容。无论如何,你可以使用 setup.py 做很多事情,比如
还有很多其他命令可以与 setup.py 一起使用。寻求帮助
setup.py can be used in two scenarios , First, you want to install a Python package. Second, you want to create your own Python package. Usually standard Python package has couple of important files like setup.py, setup.cfg and Manifest.in. When you are creating the Python package, these three files will determine the (content in PKG-INFO under egg-info folder) name, version, description, other required installations (usually in .txt file) and few other parameters. setup.cfg is read by setup.py while package is created (could be tar.gz ). Manifest.in is where you can define what should be included in your package. Anyways you can do bunch of stuff using setup.py like
There are bunch of other commands which could be used with setup.py . for help
setup.py
是一个与其他文件一样的 Python 文件。它可以采用任何名称,但按照惯例,它被命名为setup.py
,以便每个脚本没有不同的过程。setup.py
最常用于安装 Python 模块,但用于其他目的:模块:
也许这是
setup.py
最著名的用法是在模块中。虽然它们可以使用pip
安装,但旧的 Python 版本默认不包含pip
,需要单独安装。如果您想安装模块但不想安装 pip,唯一的选择是从 setup.py 文件安装模块。这可以通过 python setup.py install 来实现。这会将 Python 模块安装到根字典(没有
pip
、easy_install
等)。当
pip
失败时经常使用此方法。例如,如果无法通过 pip 获取所需包的正确 Python 版本(可能是因为它不再维护),则下载源代码并运行 python setup.py install 将会执行相同的操作,除非需要编译二进制文件(但将忽略 Python 版本 - 除非返回错误)。setup.py 的另一个用途是从源安装包。如果模块仍在开发中,则轮文件将不可用,唯一的安装方法是直接从源安装。
构建Python扩展:
构建模块后,可以使用distutils 安装脚本。构建完成后,可以使用上面的命令安装它们。
安装脚本很容易构建,一旦文件被正确配置,就可以通过运行 python setup.py build 进行编译(请参阅所有命令的链接)。
为了便于使用和按照惯例,它再次被命名为
setup.py
,但可以采用任何名称。Cython:
setup.py 文件的另一个著名用途包括编译扩展。这些需要具有用户定义值的设置脚本。它们允许快速(但一旦编译就依赖于平台)执行。这是来自 文档:
可以通过
python setup.py build
编译Cx_Freeze:
另一个需要安装脚本的模块是
cx_Freeze
。这会将 Python 脚本转换为可执行文件。这允许许多命令(例如描述、名称、图标、包)包含、排除等,并且一旦运行将生成可分发的应用程序。 文档中的示例:可以通过
python 进行编译setup.py 构建
。那么什么是
setup.py
文件?很简单,它是一个在 Python 环境中构建或配置某些内容的脚本。
分发时的包应仅包含一个安装脚本,但将多个安装脚本组合在一起并形成一个安装脚本的情况并不罕见。请注意,这通常涉及
distutils
但并非总是如此(正如我在上一个示例中所示)。需要记住的是,它只是以某种方式配置 Python 包/脚本。它采用名称,因此在构建或安装时始终可以使用相同的命令。
setup.py
is a Python file like any other. It can take any name, except by convention it is namedsetup.py
so that there is not a different procedure with each script.Most frequently
setup.py
is used to install a Python module but server other purposes:Modules:
Perhaps this is most famous usage of
setup.py
is in modules. Although they can be installed usingpip
, old Python versions did not includepip
by default and they needed to be installed separately.If you wanted to install a module but did not want to install
pip
, just about the only alternative was to install the module fromsetup.py
file. This could be achieved viapython setup.py install
. This would install the Python module to the root dictionary (withoutpip
,easy_install
ect).This method is often used when
pip
will fail. For example if the correct Python version of the desired package is not available viapip
perhaps because it is no longer maintained, , downloading the source and runningpython setup.py install
would perform the same thing, except in the case of compiled binaries are required, (but will disregard the Python version -unless an error is returned).Another use of
setup.py
is to install a package from source. If a module is still under development the wheel files will not be available and the only way to install is to install from the source directly.Building Python extensions:
When a module has been built it can be converted into module ready for distribution using a distutils setup script. Once built these can be installed using the command above.
A setup script is easy to build and once the file has been properly configured and can be compiled by running
python setup.py build
(see link for all commands).Once again it is named
setup.py
for ease of use and by convention, but can take any name.Cython:
Another famous use of
setup.py
files include compiled extensions. These require a setup script with user defined values. They allow fast (but once compiled are platform dependant) execution. Here is a simple example from the documentation:This can be compiled via
python setup.py build
Cx_Freeze:
Another module requiring a setup script is
cx_Freeze
. This converts Python script to executables. This allows many commands such as descriptions, names, icons, packages to include, exclude ect and once run will produce a distributable application. An example from the documentation:This can be compiled via
python setup.py build
.So what is a
setup.py
file?Quite simply it is a script that builds or configures something in the Python environment.
A package when distributed should contain only one setup script but it is not uncommon to combine several together into a single setup script. Notice this often involves
distutils
but not always (as I showed in my last example). The thing to remember it just configures Python package/script in some way.It takes the name so the same command can always be used when building or installing.
当您使用
setup.py
下载软件包时,打开终端(Mac、Linux)或命令提示符(Windows)。使用cd
并帮助您使用Tab按钮将路径设置为您下载文件的文件夹以及setup.py
:按回车键,您应该看到一些内容像这样:
然后在
python setup.py install
之后输入:按
enter
。完毕!When you download a package with
setup.py
open your Terminal (Mac,Linux) or Command Prompt (Windows). Usingcd
and helping you with Tab button set the path right to the folder where you have downloaded the file and where there issetup.py
:Press enter, you should see something like this:
Then type after this
python setup.py install
:Press
enter
. Done!为了简单起见,当您调用 安装函数提到的其他答案。在 setup.py 中,您应该放置安装软件包所需的所有内容。
常见的 setup.py 函数
以下两节讨论许多 setup.py 模块具有的两件事。
setuptools.setup
此函数允许您指定项目属性,例如项目名称、版本....最重要的是,此函数允许您安装其他正确打包的函数。有关 setuptools.setup 的示例,请参阅此网页
setuptools.setup 的这些属性允许安装这些类型的包:
导入到项目并在 PyPI 中列出的包使用setuptools.findpackages:
packages=find_packages(exclude=["docs","tests", ".gitignore", "README.rst","DESCRIPTION.rst"])
包不在 PyPI< /a>,但可以使用 dependency_links
dependency_links=["http://peak.telecommunity.com/snapshots/",]
自定义函数
理想情况下的 世界,
setuptools.setup
将为您处理一切。不幸的是情况并非总是如此。有时您必须执行特定的操作,例如使用 subprocess 命令安装依赖项,以让您要安装的系统处于适合您的软件包的正确状态。尽量避免这种情况,这些函数会变得混乱,并且在 操作系统 和甚至分发。To make it simple, setup.py is run as
"__main__"
when you call the install functions the other answers mentioned. Inside setup.py, you should put everything needed to install your package.Common setup.py functions
The following two sections discuss two things many setup.py modules have.
setuptools.setup
This function allows you to specify project attributes like the name of the project, the version.... Most importantly, this function allows you to install other functions if they're packaged properly. See this webpage for an example of setuptools.setup
These attributes of setuptools.setup enable installing these types of packages:
Packages that are imported to your project and listed in PyPI using setuptools.findpackages:
packages=find_packages(exclude=["docs","tests", ".gitignore", "README.rst","DESCRIPTION.rst"])
Packages not in PyPI, but can be downloaded from a URL using dependency_links
dependency_links=["http://peak.telecommunity.com/snapshots/",]
Custom functions
In an ideal world,
setuptools.setup
would handle everything for you. Unfortunately this isn't always the case. Sometimes you have to do specific things, like installing dependencies with the subprocess command, to get the system you're installing on in the right state for your package. Try to avoid this, these functions get confusing and often differ between OS and even distribution.要安装您下载的 Python 包,您需要解压存档并运行其中的 setup.py 脚本:
对我来说,这一直感觉很奇怪。将包管理器指向下载会更自然,就像在 Ruby 和 Nodejs 中所做的那样,例如。
gem install Rails-4.1.1.gem
包管理器也更舒服,因为它熟悉且可靠。另一方面,每个 setup.py 都是新颖的,因为它是特定于包的。它需要对约定的信心“我相信这个 setup.py 与我过去使用过的其他命令采用相同的命令”。这对精神意志力来说是一种令人遗憾的负担。
我并不是说 setup.py 工作流程不如包管理器安全(我理解 Pip 只是在里面运行 setup.py),但我当然觉得它很尴尬和不和谐。所有命令都指向同一个包管理器应用程序,这是一种和谐。您甚至可能会喜欢它。
To install a Python package you've downloaded, you extract the archive and run the setup.py script inside:
To me, this has always felt odd. It would be more natural to point a package manager at the download, as one would do in Ruby and Nodejs, eg.
gem install rails-4.1.1.gem
A package manager is more comfortable too, because it's familiar and reliable. On the other hand, each
setup.py
is novel, because it's specific to the package. It demands faith in convention "I trust this setup.py takes the same commands as others I have used in the past". That's a regrettable tax on mental willpower.I'm not saying the setup.py workflow is less secure than a package manager (I understand Pip just runs the setup.py inside), but certainly I feel it's awkard and jarring. There's a harmony to commands all being to the same package manager application. You might even grow fond it.