1

I installed NLTK package, and i can import NLTK in Jupyter Notebook, but cannot import it in PyCharm.

>>> import nltk
>>> import sys
>>> print('\n'.join(sys.path))

In jupyter notebook:

/Users/morphy/anaconda3/lib/python36.zip
/Users/morphy/anaconda3/lib/python3.6
/Users/morphy/anaconda3/lib/python3.6/lib-dynload
/Users/morphy/anaconda3/lib/python3.6/site-packages
/Users/morphy/anaconda3/lib/python3.6/site-packages/aeosa
/Users/morphy/anaconda3/lib/python3.6/site-packages/IPython/extensions 
/Users/morphy/.ipython

But in Pycharm:

/Users/morphy/Documents/workplace/practice
/Users/morphy/Documents/workplace/practice
/Users/morphy/anaconda3/lib/python36.zip
/Users/morphy/anaconda3/lib/python3.6
/Users/morphy/anaconda3/lib/python3.6/lib-dynload
/Users/morphy/anaconda3/lib/python3.6/site-packages
/Users/morphy/anaconda3/lib/python3.6/site-packages/aeosa
    Traceback (most recent call last):
  File "/Users/morphy/Documents/workplace/practice/text_classsfier_test.py", line 3, in <module>
    import nltk
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/nltk/__init__.py", line 114, in <module>
    from nltk.collocations import *
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/nltk/collocations.py", line 39, in <module>
    from nltk.metrics import ContingencyMeasures, BigramAssocMeasures, TrigramAssocMeasures
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/nltk/metrics/__init__.py", line 16, in <module>
    from nltk.metrics.scores import          (accuracy, precision, recall, f_measure,
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/nltk/metrics/scores.py", line 18, in <module>
    from scipy.stats.stats import betai
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/scipy/__init__.py", line 61, in <module>
    from numpy import show_config as show_numpy_config
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/numpy/core/__init__.py", line 74, in <module>
    from numpy.testing.nosetester import _numpy_tester
  File "/Users/morphy/anaconda3/lib/python3.6/site-packages/numpy/testing/__init__.py", line 10, in <module>
    from unittest import TestCase
  File "/Users/morphy/anaconda3/lib/python3.6/unittest/__init__.py", line 59, in <module>
    from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
  File "/Users/morphy/anaconda3/lib/python3.6/unittest/case.py", line 5, in <module>
    import difflib
  File "/Users/morphy/Documents/workplace/practice/difflib.py", line 921
    raise ValueError, 'unknown tag %r' % (tag,)
                    ^
SyntaxError: invalid syntax

The same error is also occurred when i import other packages like numpy and sklearn.

Morphy Z
  • 23
  • 4
  • Please provide the code in your question. An image is not helpful. See here on How to create a Minimal, Complete, and Verifiable examplea https://stackoverflow.com/help/mcve – johnashu Mar 07 '18 at 03:29
  • 1
    Thanks for you advice! i've already corrected the question. – Morphy Z Mar 07 '18 at 03:39
  • Whats inside `text_classsfier_test.py`? – Vivek Kumar Mar 07 '18 at 04:39
  • @VivekKumar just the three lines code: import nltk, import sys, print('\n'.join(sys.path)) – Morphy Z Mar 07 '18 at 06:56
  • Ok. Then do you have a file called `difflib.py` in your current working directory aside `text_classifier_test.py`. The stack trace says so. See last line of stack trace. And I think thats conflicting with a library level file with same name. – Vivek Kumar Mar 07 '18 at 07:29
  • @VivekKumar Thanks for your reminder. I noticed that i had create a file difflib.py. When numpy is imported, it was calling this file instead of it's own library file hence resulting in the error. – Morphy Z Mar 07 '18 at 22:51

1 Answers1

0

Update: Resolved.

I created a file named difflib.py in my current working directory, hence when numpy is imported, it will call this file instead of it's own library file.

This question is similar to Numpy import throws AttributeError: 'module' object has no attribute 'core'

Morphy Z
  • 23
  • 4