Invalid syntax error using Requests library with Python 2.7 - python-2.7

I'm using the Requests library with Python 2.7 and get the following error:
Traceback (most recent call last):
File "/home/user/Documents/workspaces/controller/main.py", line 2, in <module>
import requests
File "/usr/local/lib/python2.7/dist-packages/requests/__init__.py", line 45, in <module>
from .exceptions import RequestsDependencyWarning
File "/usr/local/lib/python2.7/dist-packages/requests/exceptions.py", line 11, in <module>
from .compat import JSONDecodeError as CompatJSONDecodeError
File "/usr/local/lib/python2.7/dist-packages/requests/compat.py", line 14, in <module>
import charset_normalizer as chardet
File "/usr/local/lib/python2.7/dist-packages/charset_normalizer/__init__.py", line 24, in <module>
from .api import from_bytes, from_fp, from_path, normalize
File "/usr/local/lib/python2.7/dist-packages/charset_normalizer/api.py", line 38
sequences: bytes,
^
SyntaxError: invalid syntax
I installed Requests with the following command:
sudo pip install requests -t /usr/local/lib/python2.7/dist-packages
The file I'm running is just main.py with the content:
import json
import requests
if __name__ == "__main__":
print('hello world')
I have both Python 3.6 and 2.7 installed and selected Python 2.7 as the interpreter in VS Code.

I just ran into this issue; the package is expecting to be executed with Python 3, not Python 2.

Related

Python Cryptography run time error on _init_cffi_1_0_external_module

installing cryptography:
successfully installed gcc, libffi-devel on python 2.7.
When i try executing the script getting the below error:
from cryptography.fernet import Fernet
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.7/site-packages/cryptography-2.4.2-py2.7-linux-x86_64.egg/cryptography/fernet.py", line 17, in
from cryptography.hazmat.primitives import hashes, padding
File "/usr/lib64/python2.7/site-packages/cryptography-2.4.2-py2.7-linux-x86_64.egg/cryptography/hazmat/primitives/padding.py", line 13, in
from cryptography.hazmat.bindings._padding import lib
AttributeError: _init_cffi_1_0_external_module

Getting "No module named queue" when installing tensorflow

I am using Anaconda and trying to install tensorflow with pip, but I receive the following error and I am not sure what to do next?
Traceback (most recent call last):
File "/home/xieyangyang/anaconda3/envs/tensorflow1/bin/pip", line 7, in <module>
from pip._internal import main
File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/pip/_internal/__init__.py", line 20, in <module>
from pip._vendor.urllib3.exceptions import DependencyWarning
File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/pip/_vendor/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/pip/_vendor/urllib3/connectionpool.py", line 44, in <module>
from .util.queue import LifoQueue
ImportError: No module named queue
Maybe just reinstall pip without pip?
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
See https://pip.pypa.io/en/stable/installing/ for more info.

ImportError from greenlet with Python 2.7.3 in virtualenv

I installed locustio
pip install locustio
Once installed, I tried running
locust --help
But I get the following error:
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/chalktalk-legacy/bin/locust", line 7, in <module>
from locust.main import main
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/locust/__init__.py", line 1, in <module>
from core import HttpLocust, Locust, TaskSet, task
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/locust/core.py", line 1, in <module>
import gevent
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/gevent/__init__.py", line 49, in <module>
from gevent.hub import get_hub, iwait, wait, PYPY
File "/home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/gevent/hub.py", line 11, in <module>
from greenlet import greenlet, getcurrent, GreenletExit
ImportError: /home/vagrant/.virtualenvs/chalktalk-legacy/local/lib/python2.7/site-packages/greenlet.so: undefined symbol: _PyTrash_thread_deposit_object
Is the solution to rebuild gevent? If so, how does one go about rebuilding greenlet in debian virtualenv with python 2.7.3? If not, how do I solve this issue without upgrading python?

Trouble creating a conda enviroment with numpy 10.1.2

I'm having trouble getting numpy working with conda + python 2.7. It works fine with python 3, but when I try below I get "cannot import name scalarmath".
conda create -n test numpy python=2.7 --no-default-packages
source activate test
python -c 'import numpy'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/me/anaconda/envs/test/lib/python2.7/site-packages/numpy/__init__.py", line 153, in <module>
from . import add_newdocs
File "/Users/me/anaconda/envs/test/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/Users/me/anaconda/envs/test/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/Users/me/anaconda/envs/test/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/Users/me/anaconda/envs/test/lib/python2.7/site-packages/numpy/core/__init__.py", line 20, in <module>
from . import scalarmath
ImportError: cannot import name scalarmath
You need to activate your environment with source activate test. This works for me on OS X 10.9.:
conda create -n test numpy python=2.7 --no-default-packages
source activate test
python -c 'import numpy'

conda update --all = "ImportError: DLL load failed: %1 is not a valid Win32 application." when trying to import matplotlib.pyplot

I use the Python distribution. Python 2.7 x64 with Windows 7 SP1 x64 Ultimate.
After a conda update --all, whenever I try to import matplotlib.pyplot I get ImportError: DLL load failed: %1 is not a valid Win32 application.. Why?
Full error stack:
Traceback (most recent call last):
File "C:\svn\hw4\code\test_con.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\Anaconda\lib\site-packages\matplotlib\backends\__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 17, in <module>
from .backend_qt5agg import NavigationToolbar2QTAgg
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 18, in <module>
from .backend_qt5 import QtCore
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5.py", line 31, in <module>
from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__
File "C:\Anaconda\lib\site-packages\matplotlib\backends\qt_compat.py", line 91, in <module>
from PyQt4 import QtCore, QtGui
ImportError: DLL load failed: %1 is not a valid Win32 application.
I had the same issue after running conda update anaconda. The solution that worked for me was to simply to download the latest windows installer, then uninstall and reinstall.
I suspect this is related to the pathname of your conda environment, as if you do a clean install anaconda now wants to live in C:\Users\yourname\AppData\Local\Continuum\Anaconda2, where previously it was just Anaconda (no 2). I suspect after running the upgrade scripts some things are pointing to the nonexistant (for you) "new" path.
I found that if I explicitly activate a conda environment with activate myenvname before running anything it works fine. Likewise if you you run from the "anaconda prompt" (which activates your default environment for you) it works fine. But to get my default environment to stay active from anywhere like its supposed to I had to reinstall.
Here is what worked for me:
Run conda update -f matplotlib (from the Anaconda command prompt)
This gave me a new error:
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "C:\svn\hw4\code\test_con.py", line 1, in <module>
import matplotlib.pyplot
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "C:\Anaconda\lib\site-packages\matplotlib\colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "C:\Anaconda\lib\site-packages\matplotlib\artist.py", line 12, in <module>
from .transforms import Bbox, IdentityTransform, TransformedBbox, \
File "C:\Anaconda\lib\site-packages\matplotlib\transforms.py", line 39, in <module>
from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
ImportError: numpy.core.multiarray failed to import
Run conda update -f numpy
This gave me a new error:
Traceback (most recent call last):
File "C:\svn\hw4\code\test_con.py", line 1, in <module>
import matplotlib.pyplot
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 27, in <module>
import matplotlib.colorbar
File "C:\Anaconda\lib\site-packages\matplotlib\colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "C:\Anaconda\lib\site-packages\matplotlib\collections.py", line 27, in <module>
import matplotlib.backend_bases as backend_bases
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 56, in <module>
import matplotlib.textpath as textpath
File "C:\Anaconda\lib\site-packages\matplotlib\textpath.py", line 22, in <module>
from matplotlib.mathtext import MathTextParser
File "C:\Anaconda\lib\site-packages\matplotlib\mathtext.py", line 63, in <module>
import matplotlib._png as _png
ImportError: DLL load failed: The specified module could not be found.
Run conda install anaconda. This rolled your system back to a stable anaconda distribtution, and solved the issue. (I guess you could skip steps 1 and 2, but I don't feel like going through it again to confirm…)