Managing dependencies

Many projects require some external packages to be installed in order to work properly. When the list of dependencies is very long, there comes a question as to how to manage it. The answer in most cases is very simple. Do not over-engineer it. Keep it simple and provide the list of dependencies explicitly in your setup.py script as follows:

from setuptools import setup
setup( name='some-package', install_requires=['falcon', 'requests', 'delorean'] # ... )

Some Python developers like to use requirements.txt files for tracking lists of dependencies for their packages. In some situations, you might find some reason for doing that, but in most cases, this is a relic of times where the code of that project was not properly packaged. Anyway, even such notable projects as Celery still stick to this convention. So if you are not willing to change your habits or you are somehow forced to use requirement files, then at least do it properly. Here is one of the popular idioms for reading the list of dependencies from the requirements.txt file:

from setuptools import setup 
import os 
 
 
def strip_comments(l): 
    return l.split('#', 1)[0].strip() 
 
 
def reqs(*f): 
    return list(filter(None, [strip_comments(l) for l in open( 
        os.path.join(os.getcwd(), *f)).readlines()])) 
 
setup( 
    name='some-package', 
    install_requires=reqs('requirements.txt') 
    # ... 
)

In next section, you'll learn how to add custom commands to your setup script.