Python requests and beautifulsoup module not importing - python

I have installed python in my mac. When I type python3in terminal and then import requests and bs4 it imports it and run the program correctly.
But when I run it on python file as python3 file_name.py, it gives the following error:
import requests
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/__init__.py", line 52, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/__init__.py", line 27, in <module>
from . import urllib3
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 28, in <module>
from string import Template
File "/Users/dark_archer/Desktop/src/string.py", line 1, in <module>
n1,n2=map(int,input().split())
ValueError: not enough values to unpack (expected 2, got 0)
I got the same error with both python 3.5 and python 3.6.

The issue is that you named a module string.py so it's confusing the importer because the logging module is also trying to import something from the standard library module string.py. This causes an issue known as "name shadowing" where your locally defined module is loaded instead of the the standard library module.
When your version of string.py gets imported it triggers the code which is causing your error.
As an easy fix, try to rename your string.py module to something else.
For more info on name shadowing check out the "The name shadowing trap" section of this link: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

Related

Attribute error: module 'collections' has no attribute 'MutableSequence' PYTHON/SMARTSHEET SDK

I installed the Smartsheet Python sdk, imported the smartsheet module but am getting an error when I want to run the script. The error is localized to the smartsheet module and says that collections module is missing Mutable Sequence. I have already tried adding:
from collections.abc import MutableSequence
and had no change.
import smartsheet
import logging
import os
_dir = os.path.dirname(os.path.abspath(__file__))
This is what pops up in the terminal.
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\smartsheet.py", line 34, in <module>
from .models import Error, ErrorResult
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\models\__init__.py", line 21, in <module>
from .access_token import AccessToken
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\models\access_token.py", line 20, in <module>
from ..types import *
File "C:\Users\jhorvath\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\smartsheet\types.py", line 29, in <module>
class TypedList(collections.MutableSequence):
AttributeError: module 'collections' has no attribute 'MutableSequence'
A "quick and dirty" solution (I have this one - Python 3.10):
Path you-python-installation\python\Lib\site-packages\smartsheet
File
types.py
Line 29
class TypedList(collections.MutableSequence):
Replace with
class TypedList(collections.abc.MutableSequence):
I think Сolleagues from smartsheet - will fix the compatibility problem (at least - I believe it)
The main reason is
Deprecated since version 3.3, will be removed in version 3.10
Another solution I used is to modify the affected .py file in downloaded library from Smartsheet and change
import collections
to
import collections.abc as collections
If you are not able to edit the package as recommended in the first solution, use Python 3.9 that is the last version to support collections Mutable Sequence.

'import discord' not working in Python 3.5

I try to import discord in python 3.5, but I get the following traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python\Python35-32\lib\site-packages\discord\__init__.py", line 20, in <module>
from .client import Client, AppInfo, ChannelPermissions
File "C:\Python\Python35-32\lib\site- packages\discord\client.py", line 42, in <module>
from .voice_client import VoiceClient
File "C:\Python\Python35-32\lib\site- packages\discord\voice_client.py", line 65, in <module>
from .gateway import *
File "C:\Python\Python35-32\lib\site- packages\discord\gateway.py", line 31, in <module>
import aiohttp
File "C:\Python\Python35-32\lib\site- packages\aiohttp\__init__.py", line 10, in <module>
from .protocol import * # noqa
File "C:\Python\Python35-32\lib\site- packages\aiohttp\protocol.py", line 11, in <module>
from wsgiref.handlers import format_date_time
ImportError: cannot import name 'format_date_time'
I installed discord inside Python35-32\Scripts using pip install discord. Additionally, I installed asyncio using pip install asyncio in the same directory.
The error pops up in my command prompt, when I type in "python", wait for it to open, and then type "import discord". I'm saying that just to be clear that it's not a "I have a file named XXX.py which clashes with something from the discord library" type of error.
Based on the comments you have added and the line
File "C:\Python\Python35-32\lib\site- packages\aiohttp\protocol.py", line 11, in <module>
from wsgiref.handlers import format_date_time
ImportError: cannot import name 'format_date_time'
The problem seems to be that 'format_date_time' could not be imported from the built in library wsgiref. Turns out that the file ...\Python35-32\lib\wsgiref\handlers.py was completely empty indicating that something corrupted your python installation.
Two possible solutions:
Reinstall your python completely which also has the benefit of solving other potential corruptions that you might not have noticed yet (Note that the underlying cause for this corruption remains unknown)
Go into the Cython git and copy the contents of the handlers.py into the local file. This is a very specific solution however and you should be aware that other parts of your python installation might also be corrupt.
Maybe you wanted to pip install discord.py instead of discord?

Python and conflicting module names

It seems that if a file is called io.py and it imports scipy.ndimage, the latter somehow ends up failing to find its own submodule, also called io:
$ echo "import scipy.ndimage" > io.py
$ python io.py
Traceback (most recent call last):
File "io.py", line 1, in <module>
import scipy.ndimage
File "/usr/lib/python2.7/dist-packages/scipy/__init__.py", line 70, in <module>
from numpy import show_config as show_numpy_config
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 22, in <module>
from .npyio import *
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 4, in <module>
from . import format
File "/usr/lib/python2.7/dist-packages/numpy/lib/format.py", line 141, in <module>
import io
File "/tmp/rm_me/io.py", line 1, in <module>
import scipy.ndimage
File "/usr/lib/python2.7/dist-packages/scipy/ndimage/__init__.py", line 172, in <module>
from .filters import *
File "/usr/lib/python2.7/dist-packages/scipy/ndimage/filters.py", line 37, in <module>
from scipy.misc import doccer
File "/usr/lib/python2.7/dist-packages/scipy/misc/__init__.py", line 45, in <module>
from .common import *
File "/usr/lib/python2.7/dist-packages/scipy/misc/common.py", line 10, in <module>
from numpy import exp, log, asarray, arange, newaxis, hstack, product, array, \
ImportError: cannot import name exp
Is this a bug in SciPy, or am I using Python wrong?
Update: I think less surprising behavior would be if import mod2 in mod1 resolved paths relative to mod1 rather than relative to whomever imported mod1.
The simple fix is to avoid naming your module io, because it's conflicting with a core library module name.
It's not really a bug in numpy, but user error: just as we shouldn't use list as a variable name because it's shadowing the builtin list name, we shouldn't use io as a module name because it's shadowing the core library io module name.
See this part:
File "/usr/lib/python2.7/dist-packages/numpy/lib/format.py", line 141, in <module>
import io
Here numpy tries to import the io module. This is intended to find the stdlib io because of the absolute import - a numpy submodule would have been loaded with relative import like you can see in the traceback where there is another line beginning from .npyio import *.
Of course your own io.py module is found first, because in the current working directory is generally the first entry in sys.path. Whoops!
You're using python wrong.
Before you create any top-level python module or package, you should make sure there isn't already a module or package by that name.
The best solution here is to not use top-level modules, and instead put everything in a single top-level package (i.e. directory with an __init__.py file) named after your project.
To check if a top-level module or package exists, you could try importing it in the interpreter, or running pydoc name from the shell.
It is worth noting that there is a very similar error that can happen that is not your fault, if a package uses absolute-style import syntax to perform a relative import. This "feature" is removed in Python3.

Python/Flask error: "ImportError: cannot import name _compare_digest"

With Windows, I am following this Flask tutorial when I came across the following error:
C:\Users\Gregory Gundersen\Documents\Research\flask-test>python run.py
Traceback (most recent call last):
File "run.py", line 2, in <module>
from app import app
File "C:\Users\Gregory Gundersen\Documents\Research\flask-test\app\__init__.py
", line 1, in <module>
from flask import Flask
File "C:\Python27\lib\site-packages\flask\__init__.py", line 21, in <module>
from .app import Flask, Request, Response
File "C:\Python27\lib\site-packages\flask\app.py", line 26, in <module>
from . import json
File "C:\Python27\lib\site-packages\flask\json.py", line 25, in <module>
from itsdangerous import json as _json
File "C:\Python27\lib\site-packages\itsdangerous.py", line 14, in <module>
import hmac
File "C:\Python27\lib\hmac.py", line 8, in <module>
from operator import _compare_digest as compare_digest
ImportError: cannot import name _compare_digest
There are SO questions and answers, but they are for OS X/Django. Has anyone see or resolved this issue for PC/Flask before?
You appear to have half the changes made for issue 21306 (backporting hmac.compare_digest to 2.7).
Your hmac module has the lines:
from operator import _compare_digest as compare_digest
at the top, but your sys.version_info shows you are running Python 2.7.6; quoting our quick chat session:
Me: Next simple check:
import sys
print(sys.version_info)
You: sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
The hmac version you have is for Python 2.7.7 and up, however!
You'll want to reinstall your Python; download 2.7.8 and reinstall it to make sure you have the correct binary executable for your standard library files.
This can occur when you have updated python without rebuilding your virtualenv. In this case, just regenerate your virtualenv.

python & suds "ImportError: cannot import name getLogger"

I'm using Ubuntu 11.04 (natty). I have been using Suds to consume a SOAP web service. Everything was working fine... until it wasn't. I can no longer import Suds. I've uninstalled and re-installed Suds from the Ubuntu repositories but still get the same import error (see IDLE traceback below). I'm using Python 2.7.1 and Suds 0.4.1-2. Does anyone have any ideas on how to solve this problem??
>>> import suds
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import suds
File "/usr/lib/pymodules/python2.7/suds/__init__.py", line 154, in <module>
import client
File "/usr/lib/pymodules/python2.7/suds/client.py", line 23, in <module>
import suds.metrics as metrics
File "/usr/lib/pymodules/python2.7/suds/metrics.py", line 23, in <module>
from logging import getLogger
ImportError: cannot import name getLogger
>>>
logging is a standard module of Python. There are several possible reasons why Python can't find it anymore:
The is another logging module in the path (print sys.path to get a list of paths Python will search)
Someone changed PYTHONPATH (the default Python search path)
Someone messed with the Python installation (deleted the logging module)

Categories