ImportError: cannot import name 'urlencode' - python

try:
from urllib import urlencode
except ImportError:
from urllib.parse import urlencode
import speech_recognition
when I run import speech_recognition normally it gives an error
Traceback (most recent call last):
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\site-packages\speech_recognition\__init__.py", line 29, in <module>
from urllib import urlencode
ImportError: cannot import name 'urlencode'
During handling of the above exception, another exception occurred: Traceback (most recent call last):
File "C:\Users\Vatsa\OneDrive\Desktop\New folder\New folder\virtual ai assistant.py", line 2, in <module>
import speech_recognition as sr
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\site-packages\speech_recognition\__init__.py", line 33, in <module>
from urllib.request import Request, urlopen
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 88, in <module>
import http.client
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 71, in <module>
import email.parser
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\email\parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\email\feedparser.py", line 27, in <module>
from email._policybase import compat32
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\email\_policybase.py", line 9, in <module>
from email.utils import _has_surrogates
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\email\utils.py", line 33, in <module>
from email._parseaddr import quote
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\email\_parseaddr.py", line 16, in <module>
import time, calendar
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\calendar.py", line 50, in <module>
class _localized_month:
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\calendar.py", line 52, in _localized_month
_months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
File "C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\lib\calendar.py", line 52, in <listcomp>
_months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
AttributeError: module 'datetime' has no attribute 'date'
[Finished in 0.7s with exit code 1]
[shell_cmd: python -u "C:\Users\Vatsa\OneDrive\Desktop\New folder\New folder\virtual ai assistant.py"]
[dir: C:\Users\Vatsa\OneDrive\Desktop\New folder\New folder]
[path: C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Android;C:\Windows\System32;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\Scripts\;C:\Users\Vatsa\AppData\Local\Programs\Python\Python36\;C:\Users\Vatsa\AppData\Local\Microsoft\WindowsApps;C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\bin;C:\Users\Vatsa\AppData\Local\GitHubDesktop\bin;C:\Users\Vatsa\AppData\Local\atom\bin;C:\Users\Vatsa\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Vatsa\.dotnet\tools]
so I checked about this and changed the code as it is at the very top but when I run that I still receive the same error.

Reposting from my comment:
You have a file named datetime.py that's being imported instead of stdlib datetime, just rename the file to something different.

Related

ImportError: cannot import name 'total_ordering' from 'functools' error in Pycharm virtual env

All of a sudden i am getting below error while running my code in Pycharm. Earlier It was working fine. I tried changing the interpretar from virtual en to base interpreter but no resolve
Traceback (most recent call last):
File "/Users/ubhatia/PycharmProjects/ModerationReport/resources/user_keywords/keyword.py", line 3, in <module>
from robot.api.deco import library, keyword
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/__init__.py", line 43, in <module>
from robot.rebot import rebot, rebot_cli
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/rebot.py", line 40, in <module>
from robot.conf import RebotSettings
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/conf/__init__.py", line 27, in <module>
from .settings import RobotSettings, RebotSettings
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/conf/settings.py", line 16, in <module>
import glob
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/glob.py", line 4, in <module>
import re
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 127, in <module>
import functools
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/functools.py", line 21, in <module>
from collections import namedtuple
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/collections/__init__.py", line 22, in <module>
from keyword import iskeyword as _iskeyword
File "/Users/ubhatia/PycharmProjects/ModerationReport/resources/user_keywords/keyword.py", line 3, in <module>
from robot.api.deco import library, keyword
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/api/__init__.py", line 71, in <module>
from robot.model import SuiteVisitor
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/model/__init__.py", line 28, in <module>
from .body import BaseBody, Body, BodyItem, Branches
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/model/body.py", line 18, in <module>
from .itemlist import ItemList
File "/Users/ubhatia/.virtualenvs/ModerationReport/lib/python3.7/site-packages/robot/model/itemlist.py", line 17, in <module>
from functools import total_ordering
ImportError: cannot import name 'total_ordering' from 'functools' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/functools.py)
This is the piece of code i am running
import time
from datetime import date, timedelta
from robot.api.deco import library, keyword
from robot.libraries.BuiltIn import BuiltIn
import pandas as pd
def get_current_date():
today = time.strftime('%d/%m/%Y')
return today
def get_yesterday_date():
today = date.today()
yesterday = today - timedelta(days=1)
return yesterday.strftime('%d/%m/%Y')
def read_excel_and_change_to_list(excel_path):
return pd.read_excel(excel_path)
print(get_current_date())
Can anyone help me out on how to resolve this issue please?

Whenever I run my discord bot using discord.py I'm getting an AttributeError 'logging' has no attribute 'getlogger'

All my code is on github except for the token.py file for obvious reasons.
bot.py file
I have had similar issues in the past and have resolved them by reconstructing some code so I apologize in advance if this is another one of those issues where I missed a capital or something but I have tried all the obvious syntax issues and none of them seemed to work. Atom also didn't show any errors.
Here is the whole error message:
Traceback (most recent call last):
File "C:\Users\james\Documents\botpy\bot.py", line 5, in <module>
import discord
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\__init__.py", line 21, in <module>
import logging
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\logging\__init__.py", line 26, in <module>
import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\traceback.py", line 5, in <module>
import linecache
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\linecache.py", line 11, in <module>
import tokenize
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\tokenize.py", line 34, in <module>
from token import *
File "C:\Users\james\Documents\botpy\token.py", line 2, in <module>
from discord.ext import commands
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\__init__.py", line 13, in <module>
from .bot import Bot, AutoShardedBot, when_mentioned, when_mentioned_or
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 27, in <module>
import asyncio
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\asyncio\__init__.py", line 8, in <module>
from .base_events import *
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 18, in <module>
import concurrent.futures
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\__init__.py", line 8, in <module>
from concurrent.futures._base import (FIRST_COMPLETED,
File "C:\Users\james\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\_base.py", line 42, in <module>
LOGGER = logging.getLogger("concurrent.futures")
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Try to rename your file into main.py
I had a similar error just because the filename.

when I run this code it shows error

filename='metamorphosis_clean.txt'
file=open(filename,'rt')
text=file.read()
file.close()
from nltk import sent_tokenize
sentences=sent_tokenize(text)
print(sentences[0])
Error:
Traceback (most recent call last):
File "split_into_sentenes.py", line 1, in <module>
import nltk
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 114, in <module>
from nltk.collocations import *
File "/usr/local/lib/python2.7/dist-packages/nltk/collocations.py", line 37, in <module>
from nltk.probability import FreqDist
File "/usr/local/lib/python2.7/dist-packages/nltk/probability.py", line 47, in <module>
from collections import defaultdict, Counter
File "/usr/local/lib/python2.7/dist-packages/nltk/collections.py", line 13, in <module>
import pydoc
File "/usr/lib/python2.7/pydoc.py", line 56, in <module>
import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
File "/usr/lib/python2.7/inspect.py", line 39, in <module>
import tokenize
File "/usr/lib/python2.7/tokenize.py", line 39, in <module>
COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined
In all likelihood you have a file named token.py in the current directory, i.e. the directory from which you are running your split_into_sentenes.py script.
If present locally, token.py will be imported before the one in the standard library, and this would result in the error that you see.
Check whether it exists and if necessary rename it to something that doesn't clash with the standard library.

ImportError: No module named 'httplib'

My code:
from selenium import webdriver
driver= webdriver.Firefox()
driver.get("http://google.com")
getting the following error messages
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/selenium.py", line 23, in <module>
NewjerseyCalifornia
import http.client as http_client
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 71, in <module>
import email.parser
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/feedparser.py", line 27, in <module>
from email import message
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/message.py", line 16, in <module>
from email import utils
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/charset.py", line 15, in <module>
import email.quoprimime
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
ImportError: cannot import name 'ascii_letters'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/Users/asimali/PycharmProjects/Asim/com/introduction/Basics_Selenium.py", line 4, in <module>
from selenium import webdriver File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/__init__.py", line 18, in <module>
from selenium import selenium File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/selenium.py", line 25, in <module>
import httplib as http_client ImportError: No module named 'httplib' Process finished with exit code 1
Note : I also ran the command 2to3 -w Basics_Selenium.py

Weird behaviour with tweepy in Python?

I have python 2.7.9 on OS X (10.11). I have installed tweepy 3.5.0 and simplejson 3.8.1. When i write a program (IDE: textWrangler) for updating status of twitter account (by using tweepy) and run the program using command line it throws error on "import tweepy" in a program. However when i run the same code on python shell, it execute sucessfuly (without any error). Any suggestions or comments?
Traceback (most recent call last):
File "tweepytesting.py", line 1, in <module>
import tweepy
File "build/bdist.macosx-10.6-intel/egg/tweepy/__init__.py", line 14, in <module>
File "build/bdist.macosx-10.6-intel/egg/tweepy/api.py", line 12, in <module>
File "build/bdist.macosx-10.6-intel/egg/tweepy/binder.py", line 11, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/__init__.py", line 27, in <module>
from . import urllib3
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 33, in <module>
from .packages.ssl_match_hostname import CertificateError
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/packages/__init__.py", line 3, in <module>
from . import ssl_match_hostname
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/packages/ssl_match_hostname/__init__.py", line 3, in <module>
from ssl import CertificateError, match_hostname
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 90, in <module>
import textwrap
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
If I add this line:
import string; print(string)
just above import tweepy, then the following is printed:
<module 'string' from '/Users/yasirmuhammad/Desktop/learnpython/string.pyc'>

Categories

Resources