Can anyone please explain what setup.py
is and how it can be configured or used?
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.
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.
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.
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With