Diaoul / babelfish

BabelFish is a Python library to work with countries and languages
BSD 3-Clause "New" or "Revised" License
25 stars 13 forks source link

ImportError: No module named alpha2 when calling babelfish.Language.fromietf('EN') #19

Closed caronc closed 8 years ago

caronc commented 8 years ago

Using you're subliminal backend, I construct the languages (with babelfish) ahead of time. But I had one report from a user using Python 2.7.3 who gets an import error (of alpha2) by the time it gets to babelfish's sub libraries.

I don't expect you to support my script at all... I was just curious on how to interpret the last part of the exception being thrown. Maybe it's a bug with babel fish? Maybe it isn't? Have you seen something like that? Perhaps an obvious issue I'm overlooking or a syntax error on my part?

This syntax works for me, so i can't reproduce it

import babelfish
babelfish.Language.fromietf('EN')

Here is the output passed along to me in it's entirety:

./Subliminal.py -S /media/data/Movies/ -l EN -f
2015-10-09 09:26:55,195 - 7241 - INFO - Found 43 matched file(s).
2015-10-09 09:26:55,196 - 7241 - INFO - Using advanced search mode
2015-10-09 09:26:55,201 - 7241 - ERROR - Fatal Exception:
  Traceback (most recent call last):
    File "./Subliminal/nzbget/ScriptBase.py", line 2398, in run
    exit_code = main_function(*args, **kwargs)
    File "./Subliminal.py", line 1478, in main
    use_nzbheaders=False,
    File "./Subliminal.py", line 955, in subliminal_fetch
    lang = set( babelfish.Language.fromietf(l) for l in lang )
    File "./Subliminal.py", line 955, in <genexpr>
    lang = set( babelfish.Language.fromietf(l) for l in lang )
    File "./Subliminal/babelfish/language.py", line 124, in fromietf
    language = cls.fromalpha2(language_subtag)
    File "./Subliminal/babelfish/language.py", line 110, in fromcode
    return cls(*language_converters[converter].reverse(code))
    File "./Subliminal/babelfish/converters/__init__.py", line 250, in __getitem__
    plugin = ep.load(require=False)
    File "./Subliminal/pkg_resources.py", line 1948, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  ImportError: No module named alpha2

Thoughts?

Diaoul commented 8 years ago

I believe this is because subliminal or babelfish isn't installed properly but just dropped in PYTHONPATH. Entrypoints are thus not installed.

caronc commented 8 years ago

Does babelfish use entry points? Even after installing it in a virtualenv, i don't see any _entrypoints.txt files defined (in any egg files that is within its packaging - using v5.4). Nor is there reference to anything in its setup.py. That said, I've never played with entrypoints so I'm also not really familiar with where else it could be... perhaps the _toplevel.txt would have an impact?

The tool in question is in fact a download of several packages with a virtualenv library directory (you're right with that assumption). But the part that baffles me, is that it works for me (doing the same thing) (different version of python, but works with both 2.6.6 and 2.7.5).

It wouldn't have anything to do with this bug fixed here would it? The issue specifically hovers around a bug with the *import() function. It later got documented as fixed in Python v2.7.4 here as bug no 1776? Could it be a python version issue?

Diaoul commented 8 years ago

Right, babelfish doesn't use entry points from the setup.py but rather runtime registered entry points: https://github.com/Diaoul/babelfish/blob/3a177fb4097b971ce32522fdfc96785be24761dd/babelfish/language.py#L33-L44

I'm assume something is wrong with the setup but I don't know what.

caronc commented 8 years ago

Thanks for you're feedback.

The problem turned out to be a relative path include I had at the top of my script (later which i changed directories below with chdir()).

Hence, but just changing this:

from os.path import join
from os.path import dirname
import sys
sys.path.insert(0, join(dirname(__file__), 'MyLibPath'))

To this (the addition of abspath()); everything worked again.

from os.path import abspath
from os.path import join
from os.path import dirname
import sys
sys.path.insert(0, join(abspath(dirname(__file__)), 'MyLibPath'))

Sorry for wasting your time! :)