2

I have following setup.py:

from setuptools import setup
from distutils.core import setup
setup(
    name="foobar",
    version="0.1.0",
    author="Batman",
    author_email="batman@gmail.com",
    packages = ["foobar"],
    include_package_data=True,
    install_requires=[
        "asyncio",
    ],
    entry_points={
        'console_scripts': [
            'foobar = foobar.__main__:main'
         ]
    },
)

Now, the main.py file gets installed and callable by foobar out of console after installation, which is what I wanted. Problem is, main.py has import at line 3 and that does not work.

So my folder structure is as follows

dummy/setup.py
dummy/requirements.txt
dummy/foobar/__init__.py
dummy/foobar/__main__.py
dummy/foobar/wont_be_imported_one.py

I run python3 setup.py bdist being in dummy directory. Upon running foobar after installation, I get error

File "/usr/local/bin/foobar", line 9, in <module>
    load_entry_point('foobar==0.1.0', 'console_scripts', 'foobar')()

[...]

ImportError: No module named 'wont_be_imported_one'.

UPDATE. __init__.py has content of

from wont_be_imported_one import wont_be_imported_one

wont_be_imported_one.py has from wont_be_imported_one function which I actually need to import.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

1 Answers1

3

In Python 3, imports are absolute by default, and so from wont_be_imported_one import ... inside of foobar will be interpreted as a reference to some module named wont_be_imported_one outside of foobar. You need to use a relative import instead:

from .wont_be_imported_one import wont_be_imported_one
#    ^ Add this

See PEP 328 for more information.

jwodder
  • 54,758
  • 12
  • 108
  • 124