What is setup.py?

后端 未结 10 1528
囚心锁ツ
囚心锁ツ 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:36

    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).

    0 讨论(0)
  • 2020-11-22 10:38

    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,

    $ git clone https://github.com/user/foo  
    $ cd foo
    $ python setup.py install
    

    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).


    Creating setup.py

    If you have your package tree like,

    foo
    ├── foo
    │   ├── data_struct.py
    │   ├── __init__.py
    │   └── internals.py
    ├── README
    ├── requirements.txt
    └── setup.py
    

    Then, you do the following in your setup.py script so that it can be installed on some machine:

    from setuptools import setup
    
    setup(
       name='foo',
       version='1.0',
       description='A useful module',
       author='Man Foo',
       author_email='foomail@foo.com',
       packages=['foo'],  #same as name
       install_requires=['bar', 'greek'], #external packages as dependencies
    )
    

    Instead, if your package tree is more complex like the one below:

    foo
    ├── foo
    │   ├── data_struct.py
    │   ├── __init__.py
    │   └── internals.py
    ├── README
    ├── requirements.txt
    ├── scripts
    │   ├── cool
    │   └── skype
    └── setup.py
    

    Then, your setup.py in this case would be like:

    from setuptools import setup
    
    setup(
       name='foo',
       version='1.0',
       description='A useful module',
       author='Man Foo',
       author_email='foomail@foo.com',
       packages=['foo'],  #same as name
       install_requires=['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='foomail@foo.com',
       url="http://www.foopackage.com/",
       packages=['foo'],  #same as name
       install_requires=['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.

    First step is to claim your package name & space in pypi using:

    $ python setup.py register
    

    Once your package name is registered, nobody can claim or use it. After successful registration, you have to upload your package there (to the cloud) by,

    $ python setup.py upload
    

    Optionally, you can also sign your package with GPG by,

    $ python setup.py --sign upload
    

    Bonus: See a sample setup.py from a real project here: torchvision-setup.py

    0 讨论(0)
  • 2020-11-22 10:39

    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 :

    iMac:~ user $ cd path/pakagefolderwithsetupfile/
    

    Press enter, you should see something like this:

    iMac:pakagefolderwithsetupfile user$
    

    Then type after this python setup.py install :

    iMac:pakagefolderwithsetupfile user$ python setup.py install
    

    Press enter. Done!

    0 讨论(0)
  • 2020-11-22 10:48

    setup.py is a python file, which usually tells you that the module/package you are about to install has 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 install . 
    

    pip will use setup.py to install your module. Avoid calling setup.py directly.

    https://docs.python.org/3/installing/index.html#installing-index

    0 讨论(0)
  • 2020-11-22 10:48

    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.

    http://docs.python.org/distutils/

    0 讨论(0)
  • 2020-11-22 10:51

    If you downloaded package that has "setup.py" in root folder, you can install it by running

    python setup.py install
    

    If you are developing a project and are wondering what this file is useful for, check Python documentation on writing the Setup Script

    0 讨论(0)
提交回复
热议问题