Cannot import any module in python - 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

Related

Is there any way to fix azure cli import pyparsing error on mac?

I'm trying to run azure CLI after reinstalling it I'm getting:
$ az
Traceback (most recent call last):
File "/usr/local/Cellar/python#3.8/3.8.11/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/local/Cellar/python#3.8/3.8.11/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "/usr/local/Cellar/azure-cli/2.25.0/libexec/lib/python3.8/site-packages/azure/__init__.py", line 1, in <module>
__import__('pkg_resources').declare_namespace(__name__)
File "/usr/local/Cellar/azure-cli/2.25.0/libexec/lib/python3.8/site-packages/pkg_resources/__init__.py", line 78, in <module>
__import__('pkg_resources.extern.packaging.requirements')
File "/usr/local/Cellar/azure-cli/2.25.0/libexec/lib/python3.8/site-packages/pkg_resources/_vendor/packaging/requirements.py", line 9, in <module>
from pkg_resources.extern.pyparsing import stringStart, stringEnd, originalTextFor, ParseException
File "/usr/local/Cellar/azure-cli/2.25.0/libexec/lib/python3.8/site-packages/pkg_resources/extern/__init__.py", line 52, in create_module
return self.load_module(spec.name)
File "/usr/local/Cellar/azure-cli/2.25.0/libexec/lib/python3.8/site-packages/pkg_resources/extern/__init__.py", line 44, in load_module
raise ImportError(
ImportError: The 'pyparsing' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
I checked the python binary path and I saw I already have this package.
Found the issue.
One of the sub-packages that called in the pyparsing package was called to a local folder script that has the same name. and as a result the import faild.
$ /usr/local/Cellar/azure-cli/2.25.0/libexec/bin/python
Python 3.8.11 (default, Jun 29 2021, 03:08:07)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyparsing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/azure-cli/2.25.0/libexec/lib/python3.8/site-packages/pyparsing.py", line 104, in <module>
import copy
File "/Users/shprayev/copy.py", line 4, in <module>
from pymongo import MongoClient
ModuleNotFoundError: No module named 'pymongo'
So pyparsing imports copy and I had copy.py in my path.
after rename the script it worked

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!!!

ImportError: DLL load failed: The specified procedure could not be found Error on windows 10

C:\Users\Jeff>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python36\lib\site-packages\tensorflow\__init__.py", line 98, in <module>
from tensorflow_core import *
File "C:\Python\Python36\lib\site-packages\tensorflow_core\__init__.py", line 40, in <module>
from tensorflow.python.tools import module_util as _module_util
File "C:\Python\Python36\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
module = self._load()
File "C:\Python\Python36\lib\site-packages\tensorflow\__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "C:\Python\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Python\Python36\lib\site-packages\tensorflow_core\python\__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "C:\Python\Python36\lib\site-packages\tensorflow_core\core\framework\graph_pb2.py", line 7, in <module>
from google.protobuf import descriptor as _descriptor
File "C:\Python\Python36\lib\site-packages\google\protobuf\descriptor.py", line 47, in <module>
from google.protobuf.pyext import _message
ImportError: DLL load failed: The specified procedure could not be found.
I installed python3.6.0 on windows 10 and pip. I also was able to pip install everything but when trying to test in the python interpreter it gave me this DLL error. Looking around I found a couple of things but nothing worked. Has anyone tried this before? When using jupyter notebook it gives a kernel error
I just want to be able to use tensorflow and jupyter notebook. I have been stuck on this problem for a month now and wanted to get some new directions.

Python - from twython import Twython "Invalid Syntax"

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

Not able to install requests on cygwin - ImportError: No module named _io

When I try the following command
pip install requests
I am getting the following error
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 343, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2307, in load_entry_point
return ep.load()
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 2013, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
File "/usr/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg/pip/__init__.py", line 10, in <module>
from pip.util import get_installed_distributions, get_prog
File "/usr/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg/pip/util.py", line 8, in <module>
import zipfile
File "/usr/lib/python2.7/zipfile.py", line 6, in <module>
import io
File "/usr/lib/python2.7/io.py", line 60, in <module>
import _io
ImportError: No module named _io
But when I tried to check the env using the following command
env python
I could see that it is running from python 2.7 only
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
So in the interactive command line I tried the following
>>> import _io
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named _io
It is probably because your Anti-Virus program deleted io.py. I just had to reinstall python on cygwin and try installing the program with the Anti-Virus turned off. I successfully installed request this way.
Your pip has broken or conflict with two package installation easy_install vs pip.
1) upgrade or re install with easy_install pip
will solve the problem, if you have other issue with your virtual env due to 14.04 upgrade then look at the question here.similar stackoverflow question

Categories