HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Listing an optional Python dependency in setup.py

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
dependencylistingoptionalsetuppython

Problem

I have a small project that requires a third-party module that's rarely updated (once every other year or so) and not maintained by any package management system. The context, here, is cognitive science & research, the third party module is for linking proprietary hardware to custom scripting, in the house project, potentially, may end up seeing wider (eventually open source) use within cog. psych so I want to plan for distribution.

Can anyone poke holes in or thumbs up this approach:

#!/usr/bin/env python

from distutils.core import setup

try:
    import third_party_package
    install_packages = ['in_house_package']
except ImportError:
    print "Warning: 'third_party_package' not found in PYTHON_PATH, installing version in_house_package v. "
    install_packages = ['in_house_package', 'in_house_package.third_party_package']

setup(
    name='InHousePackage', 
    version = '0.1', 
    description = 'A framework for building psychological experiments in Python', 
    author = 'me', 
    author_email = 'my_address', 
    url = 'a local git', 
    packages=install_packages,
    requires = ['numpy']
    )

Solution

A few small comments:

-
The error message looks wrong to me.

This is what the logic looks like to me:

if (third party package is installed):
install(in-house package)
else:
install(in-house package)
install(in-house variant of third-party package)


but the printed warning tells me that the in-house package isn’t installed. Shouldn’t it tell me the third-party package isn’t installed?

-
An ImportError doesn’t necessarily mean a module isn’t installed.

It just indicates any error when the module was imported. For example, if I have a file foo.py that contains:

import bar


and no module bar.py, I get an ImportError when trying to import foo, even though the foo file exists:

>>> import foo
Traceback (most recent call last):
File "", line 1, in
File "foo.py", line 1, in
import bar
ImportError: No module named bar


You can jump through hoops with import tools, or check the error message of the ImportError. That may or may not be something worth worrying about; just remember that this edge case is there.

-
Trivial nitpicks.

  • I’m sure this is just an artefact of posting on CR, and it isn’t actually called in_house_package, but you’ve misspelt it in the error message.



  • When specifying keyword arguments to a function (such as setup()), it’s common to omit the spaces around the equals sign.

Context

StackExchange Code Review Q#71658, answer score: 3

Revisions (0)

No revisions yet.