Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 4: update Python 2 example for module file location #229

Open
pjhinton opened this issue Sep 10, 2019 · 0 comments
Open

Chapter 4: update Python 2 example for module file location #229

pjhinton opened this issue Sep 10, 2019 · 0 comments

Comments

@pjhinton
Copy link

In section 4.6, there is the following example:

>>> nltk.metrics.distance.__file__
'/usr/lib/python2.5/site-packages/nltk/metrics/distance.pyc'

This fails with Python 3.7.4.

>>> nltk.metrics.distance.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'nltk' is not defined

Attempts to import and then expose fail as well:

>>> import nltk.metrics.distance
>>> nltk.metrics.distance.__file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'nltk.translate.metrics' has no attribute 'distance'

This code does produce a result:

>>> from nltk.metrics import distance
>>> distance.__file__
'/home/pj/.pyenv/versions/nltkbook/lib/python3.7/site-packages/nltk/metrics/distance.py'
>>> quit()

The reason for this behavior may be a side effect of this import in nltk/__init__.py

from nltk.translate import *

which brings nltk.translate.metrics in as nltk.metrics.

>>> import nltk
>>> nltk.metrics
<module 'nltk.translate.metrics' from '/home/pj/.pyenv/versions/nltkbook/lib/python3.7/site-packages/nltk/translate/metrics.py'>
>>> nltk.metrics.__file__
'/home/pj/.pyenv/versions/nltkbook/lib/python3.7/site-packages/nltk/translate/metrics.py'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant