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

share|improve this question
6  
Adam: what do you think could improve the experience for newcomers? – Éric Araujo Mar 9 '13 at 1:11
3  
Eric: Better examples putting everything together would be benefitial – Das.Rot Sep 12 '13 at 19:16
    
To me, it's always felt odd how to install the package you extract it and run the script inside, rather than pointing a package manager at what you've downloaded. That would be more natural. stackoverflow.com/questions/1471994/what-is-setup-py/… – Colonel Panic Jun 2 '14 at 15:54
3  
@ColonelPanic you mean like the way modern python packages are installed, with the pip package manager? – Jason Antman Jun 19 '15 at 23:47
up vote 314 down vote accepted

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:

$ python setup.py install

and the module will install itself.

http://docs.python.org/install/index.html

share|improve this answer
3  
I would appreciate if you share your knowledge on how to create or handle this modules? For example, how to create a basic module, or how to test a script on ./mymodule/bin which imports from ./mymodule/libs/ – Paulo Oliveira Nov 23 '14 at 15:11
2  
@PauloOliveira See Distributing Python Modules which describes Distutils, specifically look into 2. Writing the Setup Script. – Yous Jan 18 '16 at 13:34
    
"setuptools is a (largely) drop-in replacement for distutils first published in 2004" from the python doc: docs.python.org/2/distributing/index.html#distributing-index‌​. setup.py is part of setuptools. – Kemin Zhou Sep 20 '16 at 23:52
1  
NEVER USE python setup.py install! It breaks your versioning! stackoverflow.com/questions/4324558/… – devinbost May 4 '17 at 18:02
    
See also: stackoverflow.com/questions/4558145/… – devinbost May 4 '17 at 18:03

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 Python prompt.

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

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:

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

In this case, your setup.py would look be,

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, you're 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 registered, nobody can use your package name. After this, 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

share|improve this answer
2  
Like the comprehensive walkthrough! +1 – user1767754 Dec 2 '17 at 11:23

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

share|improve this answer
    
So according to the analogy above, if building the module failed for some reason I would tinker with the setup.py script...correct? – MonaLisaOverdrive Jan 17 '15 at 12:48
    
Yes, there might also be some config files you can look at. – whatnick Jan 19 '15 at 23:59
2  
Correct me if I am wrong, but I believe there is a small difference between the two. python setup.py install actually runs python setup.py build first (so you don't need to run them separately unless in specific cases). I believe make always needs to be run manually prior to running make install. – Joel Ostblom Feb 1 '15 at 18:23
3  
@cheflo Actually make does not require any specific parameters (or ordering): It's completely up to the creator of the Makefile which "targets" are available (and in which order they need to be invoked). Since bare Makefiles are (usually) not very portable, they tend to be generated using commands such as ./configure (autotools) or cmake . (cmake) and it's therefor up to these programs to define whether you need to explicitly run make before make install or not. – alexander255 Dec 26 '15 at 16:34
1  
I need some one to tell me whether we should still use the setup.py any more according to the docs at docs.python.org/3/installing/index.html "While direct use of distutils is being phased out, ..." – Kemin Zhou Aug 22 '17 at 21:01

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

share|improve this answer

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/

share|improve this answer

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

python setup.py --help-commands
share|improve this answer

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!

share|improve this answer

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.

share|improve this answer
    
Then you could use easy_install or similar. Btw, Python has eggs, sort of similar to ruby gems. – Pavel Šimerda Sep 8 '14 at 19:59

protected by Matt Fenwick Nov 5 '13 at 2:57

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.