Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module not found during load_entry_point in Python

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.

like image 589
llovett Avatar asked Nov 01 '13 00:11

llovett


People also ask

What is Load_entry_point?

exit( load_entry_point('rss2sms==0.0.1', 'console_scripts', 'rss2sms')() ) This executable is just a simple python module which, when we call it, uses the pkg_resources library to look up what python module our setup.py says we should call.

What is console_ scripts?

The console_scripts Entry Point Setuptools allows modules to register entrypoints which other packages can hook into to provide certain functionality. It also provides a few itself, including the console_scripts entry point.

What is entrypoint in Python?

Entry points are a way for Python packages to advertise objects with some common interface. The most common examples are console_scripts entry points, which define shell commands by identifying a Python function to run. The entrypoints module contains functions to find and load entry points.


2 Answers

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
like image 150
abarnert Avatar answered Oct 04 '22 01:10

abarnert


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.

like image 29
Chaitanya Mogal Avatar answered Oct 04 '22 01:10

Chaitanya Mogal