ImportError: cannot import name 'timezone' while importing NLTK - python

While importing NLTK in Python2 or Python3, the error shows up that 'timezone' cannot be imported while importing NLTK. It worked fine few days earlier.
Someone please help. The output is below.
For Python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
........
........
from datetime import timezone
ImportError: cannot import name 'timezone'
For Python2
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 89, in <module>
from nltk.internals import config_java
File "/usr/local/lib/python2.7/dist-packages/nltk/internals.py", line 31, in <module>
from nltk import compat
File "/usr/local/lib/python2.7/dist-packages/nltk/compat.py", line 45, in <module>
from datetime import tzinfo, timedelta
ImportError: cannot import name tzinfo

Moved solution from question to answer:
UPDATE (Problem Solved)
The issue has been solved now - turns out the problem was that i had a folder named datetime in my current working directory which was actually being imported by the file since the first element of the searched path is The directory containing the input script (or the current directory when no file is specified).
In short, Just renaming that folder solved all the problems. Thanks anyway to all.

I think you need to install dateutil package of python.
dateutil provides datetime parsing and timezone conversion.
Then try importing nltk

Related

Can't import cElementTree on Python 2.7

I cannot import xml.etree.cElementTree on Python 2.7. On the web, it says its already implemented, so I don't have to install it. (It's working on Python 3.6 for me, but I need 2.7 for reasons here)
Thats the console output:
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.cElementTree
Failed to import cElementTree from any known place
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named etree.cElementTree
I hope you can help me.
Kind regards,
Markus
I just found out that theres an old xml.pyc file in that directory, which didnt show up in PyCharm. It stopped the "real" XML from loading.

Python 3: No module named 'AudioSignal'

The code below gives this error when I'm trying to execute it:
>>> import nussl
>>> history = nussl.AudioSignal('HistoryRepeatingPropellerHeads.wav')
Error is:
C:\Users\MyPCUser>python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import nussl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\MyPCUser\AppData\Local\Programs\Python\Python37\lib\site-packages\nussl\__init__.py", line 26, in <modul
e>
from .core.constants import *
File "C:\Users\MyPCUser\AppData\Local\Programs\Python\Python37\lib\site-packages\nussl\core\__init__.py", line 7, in <m
odule>
from audio_signal import AudioSignal
ModuleNotFoundError: No module named 'audio_signal'
>>>
This module does not work yet (23-Feb-2019) with Python 3 according to Issue 135 and Issue 133. From the docs: This package has been tested with python 2.7, but not python 3.x yet. Use python 3 at your own peril!
I got the same issue when trying to use it with Python 3.

Cannot import python naoqi library after upgrading Ubuntu 14.04 to 16.04

I have recently upgraded the system to 16.04 Gnome. The most troubling thing that I am facing is that I cannot import a NAOqi library for my work. The python version of this library was pretty simple to set-up. One just has to untar the file and then enter a path variable called PYTHONPATH pointing to this library and it worked like a charm in 14.04. Now since upgrade I am facing:
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import naoqi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dell/nao_sdk/pynaoqi/naoqi.py", line 7, in <module>
import qi
File "/home/dell/nao_sdk/pynaoqi/qi/__init__.py", line 72, in <module>
from _qi import Application as _Application
ImportError: libqipython.so: cannot open shared object file: No such file or directory
If I add a path variable:
export LD_LIBRARY_PATH=:/home/dell/nao_sdk/pynaoqi/
The error changes to:
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import naoqi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dell/nao_sdk/pynaoqi/naoqi.py", line 7, in <module>
import qi
File "/home/dell/nao_sdk/pynaoqi/qi/__init__.py", line 72, in <module>
from _qi import Application as _Application
ImportError: libboost_regex.so.1.55.0: cannot open shared object file: No such file or directory
Please help me what should I do to get it working? I have also used python 2.6.9 but same error occurs with error below.
ImportError: libboost_python.so.1.55.0: cannot open shared object file: No such file or directory
Installing libboost1.55 did the trick. 16.04 comes with libboost1.58 but naoqi is not yet compatible with it. Manual installation of libboost1.55 solved the import error.

Why I can not import the package in Python?

I use the code from here:
https://github.com/Jefferson-Henrique/GetOldTweets-python
And every time I try to import the file folder, import got, it will raise:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\python\get_old_tweet\Main.py", line 1, in <module>
import got
File "C:\Users\USER\Desktop\python\get_old_tweet\got\__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
I have check the file and been pretty sure that they do have the file folder called models
And the file folder also contains __init__.py file.
So it should work well..
I have no idea how it doesn't work. Please help me!
Which version of Python do you use?
The library https://github.com/Jefferson-Henrique/GetOldTweets-python is written with Python 2.
Python 2 and Python 3 have a bit different behavior with import: https://www.python.org/dev/peps/pep-0404/#imports
Let me share example of import regarding your case:
$ python3
Python 3.5.0 |Anaconda 2.4.0 (x86_64)| (default, Oct 20 2015, 14:39:26)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/viach/Downloads/GetOldTweets-python-master/got/__init__.py", line 1, in <module>
import models
ImportError: No module named 'models'
>>> ^C
KeyboardInterrupt
$ python2
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import got
So, quick solution for you: use Python 2 in your app which depends on GetOldTweets-python.
Python only searches its default library paths by default (including the running script path). So you need to put them in Python default library paths or append your module path to those paths.
To append the path array:
>>> import sys
>>> sys.path.append("path/to/the_module")
>>> import the_module
If above solution doesn't worked, try:
>>> from models import got
It depends on where you are importing from. In the repository you proved a link to models is a subfolder of got. Try this:
from got import models
You can use a Python3-compatible fork of GetOldTweets-python:
https://github.com/Mottl/GetOldTweets-python3

Python ImportError: cannot import name itemgetter

Python 2.7.5 (default, Sep 12 2013, 12:43:04)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/urllib.py", line 33, in <module>
from urlparse import urljoin as basejoin
File "/usr/local/lib/python2.7/urlparse.py", line 118, in <module>
from collections import namedtuple
File "/usr/local/lib/python2.7/collections.py", line 9, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: cannot import name itemgetter
This issue occurs when I run import urllib. Python version is 2.7. Is this a Python's version problem? Could someone tell me how to fix it?
You have a file called operator.py in the current directory, so import operator is picking up your module and not the Python standard library module operator.
You should rename your file to not conflict with Python's standard library.

Categories