Question

I'm trying to create an entry point for the main method in one of my python modules, and there seems to be trouble importing the module where that function lives.

My setup.py looks like this:

...
setup(name="awesome-tool",
      ...,
      entry_points={
          'console_scripts' : [
              'awesome-tool = awesome_tool.awesome_tool:main'
          ]
      }
)

The project is organized like this:

awesome_tool
    |__ awesome_tool.py
    |__ __init__.py

It's awesome_tool.py that contains a function called main() that I want to make available in an executable called awesome-tool. When executing setup.py, all seems to go well. I can start a python prompt and do

> import awesome_tool.awesome_tool
> # (all good here)

But when I try to invoke awesome-tool from the command-line, this is what happens:

Traceback (most recent call last):
  File ".../virtualenvs/awesome-tool/bin/awesome-tool", line 9, in <module>
    load_entry_point('awesome-tool==1.1.0', 'console_scripts', 'awesome-tool')()
  File "build/bdist.macosx-10.9-intel/egg/pkg_resources.py", line 378, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "build/bdist.macosx-10.9-intel/egg/pkg_resources.py", line 2566, in load_entry_point
    return ep.load()
  File "build/bdist.macosx-10.9-intel/egg/pkg_resources.py", line 2260, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named awesome_tool.awesome_tool

What? Wasn't I just able to import this from the python shell? I suspect that there's something funny going on with the python path, but I haven't been able to diagnose the problem. Any ideas?

The problematic lines in the awesome-tool executable provided by setup.py seem to be the following:

from pkg_resources import load_entry_point
load_entry_point("awesome-tool==1.1.0", "console_scripts", "awesome-tool")()

Also, I realize that it's very odd to have both a package and module share the same name ("awesome_tool"), but this is code I've inherited and I don't want to break backwards compatibility with renaming, though this may be inevitable in the future. If possible, I'd like to keep the names the way they are for now.

Was it helpful?

Solution

It looks like you haven't installed the package in any way, so you're relying on it happening to sit in the current working directory, or some other directory you've added to sys.path.

You didn't tell us how you "invoke awesome-tool from the command-line," but my guess is that you're doing it from a different working directory.

For example:

$ cd ~/virtualenvs/awesome-tool
$ python
>>> import awesometool.awesometool
>>> ^D
$ cd ..
$ python
>>> import awesometool.awesometool
ImportError: No module named awesome_tool.awesome_tool
$ cd awesome-tool
$ python ./bin/awesome-tool
<everything works>
$ cd bin
$ python awesome-tool
ImportError: No module named awesome_tool.awesome_tool

OTHER TIPS

In case you have installed the setup.py file with the flag --editable

For example:

pip install --editable .

You have to install the package again without the --editable flag.

pip install .

It will not affect the changes you made locally in the package.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top