What is setup.py?

后端 未结 10 1532
囚心锁ツ
囚心锁ツ 2020-11-22 09:59

Can anyone please explain what setup.py is and how it can be configured or used?

10条回答
  •  醉酒成梦
    2020-11-22 10:58

    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.

提交回复
热议问题