Python - from twython import Twython "Invalid Syntax" - python

I'm trying to set up the twitter api Twython on a Raspberry Pi, but keep getting an invalid syntax error after the first line. I can run this without error in a separate Ubuntu machine so maybe it's related to the install of twython itself. I've searched on the forum and most of the errors have been due to import errors. Not sure what's going on here with this code. I used pip to install twython using the command sudo pip install twython.
pi#raspberrypi:~ $ python
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170124] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from twython import Twython
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pi/.local/lib/python2.7/site-packages/twython/__init__.py", line 24, in <module>
from .api import Twython
File "/home/pi/.local/lib/python2.7/site-packages/twython/api.py", line 15, in <module>
import requests
File "/home/pi/.local/lib/python2.7/site-packages/requests/__init__.py", line 84, in <module>
from urllib3.contrib import pyopenssl
File "/home/pi/.local/lib/python2.7/site-packages/urllib3/contrib/pyopenssl.py", line 46, in <module>
import OpenSSL.SSL
File "/usr/lib/python2.7/dist-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import rand, crypto, SSL
File "/usr/lib/python2.7/dist-packages/OpenSSL/crypto.py", line 13, in <module>
from cryptography.hazmat.primitives.asymmetric import dsa, rsa
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/primitives/asymmetric/rsa.py", line 8, in <module>
from fractions import gcd
File "/usr/lib/python2.7/fractions.py", line 7, in <module>
from decimal import Decimal
File "/usr/lib/python2.7/decimal.py", line 140, in <module>
import numbers as _numbers
File "numbers.py", line 1
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
^
SyntaxError: invalid syntax

Related

Fix import moviepy.editor

Until 2 days ago, everything worked.
But today i have the following error:
C:\Users\Χρήστος\Desktop\Papinhio player\project\main>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import moviepy.editor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\editor.py", line 36, in <module>
from .video.io.VideoFileClip import VideoFileClip
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 3, in <module>
from moviepy.audio.io.AudioFileClip import AudioFileClip
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\audio\io\AudioFileClip.py", line 3, in <module>
from moviepy.audio.AudioClip import AudioClip
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\audio\AudioClip.py", line 7, in <module>
from moviepy.audio.io.ffmpeg_audiowriter import ffmpeg_audiowrite
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\audio\io\ffmpeg_audiowriter.py", line 7, in <module>
from moviepy.config import get_setting
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\config.py", line 36, in <module>
FFMPEG_BINARY = get_exe()
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\imageio\plugins\ffmpeg.py", line 47, in get_exe
import imageio_ffmpeg
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\imageio_ffmpeg\__init__.py", line 7, in <module>
from ._utils import get_ffmpeg_exe, get_ffmpeg_version
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\imageio_ffmpeg\_utils.py", line 5, in <module>
from pkg_resources import resource_filename
File "C:\Users\Χρήστος\AppData\Local\Programs\Python\Python38\lib\site-packages\pkg_resources.py", line 1479, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
Uninstall moviepy with pip (pip uninstall moviepy)
Upgrade python (python -m ensurepip --upgrade)
Download whl file from: https://pypi.org/project/moviepy/#files
cd in the unzip downloaded folder and then
python setup.py build
python setup.py install
And everything is ok now!!!

Getting error when trying to import seaborn library (python)

I am on Windows 10 and using python 3.7 through the command prompt.
Whenever I try to import the seaborn library through the command prompt, I get the error:
C:\Python Stuff>python3
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 01:54:44) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import seaborn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\seaborn\__init__.py", line 6, in <module>
from .rcmod import *
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\seaborn\rcmod.py", line 5, in <module>
from . import palettes, _orig_rc_params
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\seaborn\palettes.py", line 12, in <module>
from .utils import desaturate, set_hls_values, get_color_cycle
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\seaborn\utils.py", line 7, in <module>
from scipy import stats
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\scipy\stats\__init__.py", line 384, in <module>
from .stats import *
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\scipy\stats\stats.py", line 179, in <module>
from scipy.spatial.distance import cdist
File "C:\Users\berre\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\scipy\spatial\__init__.py", line 99, in <module>
from .qhull import *
ImportError: DLL load failed: The specified module could not be found.
How do I deal with this error? I have tried uninstalling and reinstalling seaborn and its dependencies multiple times, along with using old versions of some of the libraries, but none of that seems to work.

Cant install Python numpy in Cloud9

I'm new to Python and Cloud9, so I am not sure if I have a python issue or a Cloud9 issue (or both or something completely different)
My basic problem is that within Python I can import some modules, but not others. I've installed the modules 'xport' & 'numpy' with pip, but only 'xport' is working.
If I try to 'import numpy' i get the following error:
Python 2.7.6 (default, Nov 23 2017, 15:49:48)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xport
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 153, in <module>
from . import add_newdocs
File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 18, in <module>
from .polynomial import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/polynomial.py", line 19, in <module>
from numpy.linalg import eigvals, lstsq, inv
File "/usr/lib/python2.7/dist-packages/numpy/linalg/__init__.py", line 50, in <module>
from .linalg import *
File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line 29, in <module>
from numpy.linalg import lapack_lite, _umath_linalg
ImportError: liblapack.so.3: cannot open shared object file: No such file or directory
>>> exit()
I've tried 'pip install numpy --upgrade', but it didn't fix anything. Where should I go from here?
Here's a URL that should help get you started in Cloud9 - get-scipy-stack-for-aws-lambda.sh. The key is to install with yum.
I can then import numpy in the Python shell and a simple Python script, but I'm having a problem getting it to work in the Lambda function itself, and it's having the same issue, unable to find the liblapack.so.3. Still working on that part. I, too, am very new to Cloud9.

Cannot import any module in python

Python libraries are giving error on importing them.Almost all library are giving same error. This is the case for simple pip module as well as pandas,etc.
1.Pip
$python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 13, in <module>
from pip.exceptions import InstallationError, CommandError, PipError
File "/usr/lib/python2.7/dist-packages/pip/exceptions.py", line 6, in <module>
from pip._vendor.six import iteritems
File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 64, in <module>
vendored("cachecontrol")
File "/usr/lib/python2.7/dist-packages/pip/_vendor/__init__.py", line 36, in vendored
__import__(modulename, globals(), locals(), level=0)
File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/__init__.py", line 9, in <module>
File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/wrapper.py", line 1, in <module>
File "/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl/cachecontrol/adapter.py", line 3, in <module>
File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/__init__.py", line 53, in <module>
File "/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/packages/__init__.py", line 29, in <module>
File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/__init__.py", line 8, in <module>
File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 35, in <module>
File "/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl/urllib3/connection.py", line 14, in <module>
File "/usr/lib/python2.7/httplib.py", line 80, in <module>
import mimetools
File "/usr/lib/python2.7/mimetools.py", line 6, in <module>
import tempfile
File "/usr/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
File "random.py", line 2
hvsbiav
^
IndentationError: unexpected indent
Please help!!!
::::update:::::
I know this is not the ideal way but I tackled the problem by simply replacing random file from online then it worked fine.
Looking at the traceback, it seems that you have an incorrect indentation in your random.py file. Try looking at the original file and match your contents with it.
However, if the problem still persists you can try to force re-install a Python package using:
sudo pip install --upgrade --no-deps --force-reinstall <packagename>
Or, you could re-install Python 2.7 using:
sudo apt-get install --reinstall python2.7

Why can I import module bs4 or requests from python IDLE shell but not from the python interpreter?

I can import module BeautifulSoup or requests without any problem when I run it from my script or do it directly in the python IDLE shell:
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> import bs4
>>> bs4
<module 'bs4' from '/usr/local/lib/python2.7/dist-packages/bs4/__init__.pyc'>
>>> import requests
>>> requests
<module 'requests' from '/usr/lib/python2.7/dist-packages/requests/__init__.pyc'>
However, when I do it from the command prompt using the python interpreter I run into the following errors:
pi#raspberrypi:~/Desktop/A/C $ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bs4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/bs4/__init__.py", line 30, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "/usr/local/lib/python2.7/dist-packages/bs4/builder/__init__.py", line 4, in <module>
from bs4.element import (
File "/usr/local/lib/python2.7/dist-packages/bs4/element.py", line 3, in <module>
from pdb import set_trace
File "/usr/lib/python2.7/pdb.py", line 9, in <module>
import cmd
File "/usr/lib/python2.7/cmd.py", line 53, in <module>
IDENTCHARS = string.ascii_letters + string.digits + '_'
AttributeError: 'module' object has no attribute 'ascii_letters'
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 55, in <module>
import urllib3
File "/usr/lib/python2.7/dist-packages/urllib3/__init__.py", line 10, in <module>
from .connectionpool import (
File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 29, in <module>
from .packages.ssl_match_hostname import CertificateError
File "/usr/lib/python2.7/dist-packages/urllib3/packages/__init__.py", line 5, in <module>
from ssl import CertificateError
File "/usr/lib/python2.7/ssl.py", line 90, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
I read a lot of forums, but didn't really understood what could be causing this, and most importantly, how to solve it.
Many thanks for your help,
Best,
Mathieu
It is not really IDLE vs. Python shell on console but where the current working directory for the process is. Because in the directory from wich you start the console Python shell there is a string.py which ”shadows” the string module from the standard library. Delete that string.py (and the string.pyc if it exists) from that folder and other modules will find the standard string module again.

Categories