Search code examples
pythonsetuptools

setup.py installing package and script - NameError referencing package from script


I'm struggling with my first Python package + script project using setuptools. Here's a skeleton of my setup.py:

setuptools.setup(
    name=<PROJECT>,
    packages=[<PACKAGE_NAME>],
    scripts=['bin/<PACKAGE_NAME>.py'],
    python_requires='>=3',
)

My package is in package_name/ and my script, which has the same base name as the package, is in bin/package_name.py. The script does from package_name import *. There are no dependencies.

When I run python3 setup.py install, it succeeds. Thereafter, when I do import package_name in a Python console, it succeeds. But when I run the script from the command line, it fails with a NameError on the first reference to a component from the package. I've reproduced the same error on Mac OS X and Linux.

Why does it fail this way, but doesn't throw an ImportError? How can I fix my script or my setup.py?


Solution

  • has the same base name as the package

    That's exactly the problem. When you run package_name.py and the script imports package_name Python tries to import it from that script because it's the first package_name.py in sys.path (Python automatically prepends script's directory to sys.path).

    Rename the script to package_name. Or even better create __main__.py in your package and use entry_points:

    setup(
        …
        entry_points={
            'console_scripts': [
                'package_name = package_name.__main__:main'
            ]
        },
        …
    )
    

    That way setuptools create the script package_name for you automatically.

    See an example in my mimedecode.