when I executed the below code in python 2.7 CLI
import nltk
it is showing the following error
SyntaxError:Invalid Syntax
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/nani/.local/lib/python2.7/site-packages/nltk/__init__.py", line 128, in <module>
from nltk.collocations import *
File "/home/nani/.local/lib/python2.7/site-packages/nltk/collocations.py", line 35, in <module>
from nltk.probability import FreqDist
File "/home/nani/.local/lib/python2.7/site-packages/nltk/probability.py", line 333
print("%*s" % (width, samples[i]), end=" ") ^
SyntaxError: invalid syntax
How to fix this?
nltk dropped support to Python2, Try to use older versions of nltk in which it supports python 2 and I found out that nltk 3.0 version supports python 2 [edited - Thanks to user2357112 supports Monica
]
So, Try to download and install previous versions of nltk with the command
pip install nltk==3.0
You can change the version number which is 3.0 in the above mentioned case and can install suitable version whichever you feels working.
It worked for me.If anyone facing same problem can try above mentioned method.
The code is using the print function, which in Python 2.7 has to be enabled with
from __future__ import print_function
However, this has to appear in the module being imported, not the code importing the module. nltk appears to assume it will be imported by a Python 3 interpreter.
Related
I'm learning Python at the moment, and after installing the docx module, I keep getting this error when trying to import it in the Terminal:
import docx
Traceback (most recent call last):
File "", line 1, in
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/init.py", line 14, in
from docx.parts.document import DocumentPart
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/parts/document.py", line 7, in
from docx.document import Document
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/document.py", line 10, in
from docx.section import Section, Sections
File "/Users/trevorstathatos/Library/Python/3.10/lib/python/site-packages/docx/section.py", line 7, in
from collections import Sequence
ImportError: cannot import name 'Sequence' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/init.py)
When trying to import it to my Mu Editor, I get this error:
import docx
~/Library/Application Support/mu/mu_venv-38-20211212-105811/lib/python3.8/site-packages/docx.py in
28 TAGS = {}
29
---> 30 from exceptions import PendingDeprecationWarning
31 from warnings import warn
32
ModuleNotFoundError: No module named 'exceptions'
Can someone please help me figure out how to fix this problem? Thanks.
I have readed on internet and a solution may be:
$ pip install python-docx
#instead of pip install docx
(for python 3.x)
Please try this and tell me if works
Link: https://flutterq.com/solved-import-no-module-named-exceptions/
Are you using the most recent version of python-docx? (currently 0.8.11). I found that when I used 0.8.10 python couldn't find all the modules.
You can use where python command in cmd to find the path that python is installed in. then you can use this command -
[python path] -m pip install python-docx
$ pip install python-docx
this worked for me.
I have recently updated to Ubuntu 18.04.1 LTS. After that I tried to run some of my python codes and I got an error related to the lmfit package, here's the code:
import scipy
from asteval import get_ast_names
import lmfit
(I have explicitly added the scipy and asteval modules to check python was finding them porperly)
I obtained this error:
Traceback (most recent call last):
File "/home/maurizio/Documents/Python/Programmini/PhD/TEAS/Test.py", line 3, in <module>
import lmfit
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/__init__.py", line 42, in <module>
from .confidence import conf_interval, conf_interval2d
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/confidence.py", line 12, in <module>
from .minimizer import MinimizerException
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/minimizer.py", line 35, in <module>
from .parameter import Parameter, Parameters
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/parameter.py", line 8, in <module>
from asteval import Interpreter, get_ast_names, valid_symbol_name
ImportError: cannot import name get_ast_names
which basically states that lmfit cannot find the function get_ast_names from asteval, although the python interpreter doesn't complain when I try to import it so it should be properly installed.
I have found this question with a similar problem and the guy in the comments claims he solved it by reinstalling it with pip3, so I tried to compile it with python3 and there's no problem so the issue concerns only python 2
Any help?
I believe you must have conflicting versions of lmfit and asteval. This should have been automatically noticed (and ideally resolved) when installing lmfit, but something there appears to have gone wrong. For the latest releases, you should get
>>> import asteval
>>> asteval.__version__
0.9.12
>>> import lmfit
>>> lmfit.__version__
0.9.11
Depending on how you installed these, you may need to check for and remove older versions of asteval in your $HOME/.local/lib/python2.7/site-packages/ folder and re-do pip install asteval.
I'm trying to write a python script that requires a connection to firebase. I've installed the python-firebase package, but when I import it into my program using 'import firebase', I get the following error:
Traceback (most recent call last):
File "C:\Users\hajel\AppData\Local\Programs\Python\Python37-32\Scripts\RFIDHandler.py", line 1, in <module>
import firebase
File "C:\Users\hajel\AppData\Local\Programs\Python\Python37-32\lib\site-packages\firebase\__init__.py", line 3
from .async import process_pool
^
SyntaxError: invalid syntax
The issue was fixed here here.
For some reason, the working python-firebase package did not make it to PyPI.
In order to fix it, pip install the latest version manually:
pip install git+https://github.com/ozgur/python-firebase
If you need a static version of the library, you could use the commit hash. For example:
pip install \
git+https://github.com/ozgur/python-firebase#0d79d7609844569ea1cec4ac71cb9038e834c355
The problem is that async is a keyword in python 3.7
the solution is quite simple.
Just rename the file async.py to something other like asyncn.py and replace every from .async import process_pool in the files firebase.py , decorators.py and others , to from .asyncn import process_pool
Edit:
Also it might still persist so change it from init.py file
ya because your action is wrong its a system-generated file
don't comment it just follows steps
1)rename .async into .async_
2)open__init__ file and change .async into .async_
3)open firebase.py and change .async into .async_
because of .async is the keyword now is current version in python
Done>>>>>>>>>>
I commented "#from .async import process_pool" in firebase.py and started working, it was incompatible with python 3.7
After installing NLTK and NLTK-DATA with PIP, i run python then i type from nltk.corpus import cmudict and it works.
But when i wrote a script like this:
from nltk.corpus import cmudict
d = cmudict.dict()
def nsyl(word):
return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]]
print nsyl("hello")
I have the following error :
Traceback (most recent call last):
File "nltk.py", line 1, in <module>
from nltk.corpus import cmudict
File "nltk.py", line 1, in <module>
from nltk.corpus import cmudict
ImportError: No module named corpus
How can i fix this ?
Thanks in advance
From your stacktrace: File "nltk.py", line 1, in <module>, you have called your file nltk.py. When python searches for a module, it looks in the current directory first, and you have "nltk.py" there. It will import this as nltk, and since your code does not define corpus, it can't find nltk.corpus.
To fix this, you should rename your file to something else, say nltkexperience.py. Also make sure to remove "nltk.pyc" from your directory if it exists, since this will also be loaded (it's the byte compiled version of your code). After that, it should work fine.
As others have pointed out, this seems to be a case of version mismatch. If you have multiple versions of Python installed, make sure that the one where you installed NLTK is the one being used when running the script.
As an example, I have Python 2.7, Python 3.3, and Anaconda Python (2.7) installed. My shell defaults to Anaconda (and its pip, e.g.). So when I install something via pip and run it on the command line, it works. At the same time, my Vim is compiled to use the system's Python, and it doesn't see Anaconda's installs/ libraries. So if from within Vim I run Python, I will get an error that the library I installed is not found.
Hope this helps.
I'm trying to install this great python module Python-Chrono to my python environment, but it fails at least with python 2.4.3 and 2.6.6 with the following error message:
Traceback (most recent call last):
File "setup.py", line 30, in ?
import chrono
File "/home/janne/python-chrono-0.3.0/chrono/__init__.py", line 22
from . import calendar
^
SyntaxError: invalid syntax
The setup is using relative import mechanism and it should work just fine, but in my environment it causes this error.
Is there a way to get this fixed? Have you seen this kind of behaviour in your projects?
Python 2.4 doesn't support that syntax - it was introduced in Python 2.5.
(Are you 100% sure that it's failing with that message in 2.6?)