Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should setup.py and requirements.txt be used by app developers?

What should an app developer place in setup.py and requirements.txt? Should an app even have a setup.py?

The docs seem to only cover the scenario of writing a module. But in an app, how should package and module dependency be encoded? Using both setup.py and requirements.txt?

I've been handed a 2.7.8 project with some questionable coding, and I'm not sure if this setup.py is reasonable. I'm surprised by the amount of boilerplate coding:

#!/usr/bin/env python
from setuptools import setup, find_packages
import sys

if sys.argv[1] == 'test':
    import multiprocessing, logging
    from billiard import util

with open('requirements.txt') as f:
    required = f.read().splitlines()

if sys.version_info < (2, 7, 0):
    required.append('importlib')

setup(
    version='0.1',
    name='...',
    description='...',
    author='...',
    author_email='...',
    packages=find_packages(),
    package_data={},
    install_requires=required,
    include_package_data=True,
    tests_require=[
        'billiard',
        'nose==1.3'
    ],
    test_suite='nose.collector'
)

I'm new to Python, but I see some potential problems:

  • An irrelevant version number which needs to be manually updated.
  • The 'test' imports aren't relevant to the project ("multiprocessing").
  • We don't run tests by executing setup.py; we execute the nosetests command.
  • install_requires depends on the contents of requirements.txt. But afaik it should be the other way around: requirements.txt needs to be generated based on the install_requires section.
  • It seems to check for a python version which the app isn't coded to support.
like image 305
Dogweather Avatar asked Dec 14 '14 23:12

Dogweather


People also ask

Should I use requirements txt or setup py?

The short answer is that requirements. txt is for listing package requirements only. setup.py on the other hand is more like an installation script. If you don't plan on installing the python code, typically you would only need requirements.

How is setup py used?

Use of Setup.py It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on. Secondly, it serves as the command line interface via which packaging commands may be executed.


1 Answers

An irrelevant version number which needs to be manually updated.

The version number in setup.py is only important if your application / deployment needs it.

The 'test' imports aren't relevant to the project ("multiprocessing").

In that case you may need to remove it - it could be outdated code.

We don't run tests by executing setup.py; we execute the nosetests command.

I'd have to look it up but some testing frameworks use setup.py to install the project in a separate environment (such as tox for testing the project in various Python interpreters).

This depends on the contents of requirements.txt, but afaik it should be the other way around: requirements.txt needs to be generated based on an install_requires section in setup.py

The use of requirements.txt and setup.py varies across the Python community. So far, setup.py needs to know the requirements when installing the project but when your application is meant to be deployed, you can do without a setup.py file and only use a requirements.txt file.

There's no install_requires.

That may be an issue, depending on whether you actually use setup.py to install your dependencies (as said above, it's entirely possible to develop and deploy an application without a setup.py file).

It seems to check for a python version which the app isn't coded to support.

You may need to remove that then.

like image 183
Simeon Visser Avatar answered Sep 28 '22 16:09

Simeon Visser