Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is setup.py?

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

like image 288
Software Enthusiastic Avatar asked Sep 24 '09 14:09

Software Enthusiastic


People also ask

What is the use of setup py in python?

The setup.py is a Python script typically included with Python-written libraries or apps. Its objective is to ensure that the program is installed correctly. With the aid of pip , we can use the setup.py to install any module without having to call setup.py directly. The setup.py is a standard Python file.

What does pip install setup py do?

Python packages have a setup.py file that allows to easily install it while handling the dependencies. By default pip is trying to install from the Pypi: The Python Package Index (PyPI) is a repository of software for the Python programming language.

What is setup py in PyCharm?

PyCharm provides an action that helps create setup.py script, intended for building, distributing, and installing modules. Once setup.py is created, the corresponding action becomes disabled.

What is setup py and setup CFG?

The setup. cfg is an ini file, containing option defaults for setup.py commands. You can pretty much specify every keyword we used in the setup.py file in the new setup. cfg file and simply use the setup.py file as the command line interface.


2 Answers

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

like image 67
Silfverstrom Avatar answered Sep 25 '22 23:09

Silfverstrom


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='[email protected]',    packages=['foo'],  #same as name    install_requires=['wheel', '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='[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.com/",    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 temporary test.pypi.org server to make oneself familiarize with the procedure, and then publish it on the permanent pypi.org server for the public to use your package.
  • publish straight away on the permanent pypi.org server, if you are already familiar with the procedure and have your user credentials (e.g., username, password, package name)

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,

(1) the first step is to locally build the distributions using:

# prereq: wheel (pip install wheel)   $ python setup.py sdist bdist_wheel    

(2) then using twine for uploading either to test.pypi.org or pypi.org:

$ twine upload --repository testpypi dist/*   username: ***   password: ***   

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:

$ twine upload dist/* --sign  

Bonus Reading:

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

  • PEP 517, setuptools

  • why twine? using twine

like image 38
kmario23 Avatar answered Sep 25 '22 23:09

kmario23