Python ImportError: cannot import name urlencode - python

I've seen this question asked before but none of the answers seem to work for me.
I am using python version 2.7.13
In my code I have ..
import base64
import httplib2
Now when I run it on my own pc it works fine, but when I run it on my works p.c. behind a firewall I get ..
Traceback (most recent call last):
File "mail4.py", line 2, in
import httplib2
File "C:\Python27\lib\site-packages\httplib2__init__.py", line 39, in
import urllib
File "C:\mypy\urllib.py", line 1, in
import requests
File "C:\Python27\lib\site-packages\requests__init__.py", line 43, in
import urllib3
File "C:\Python27\lib\site-packages\urllib3__init__.py", line 8, in
from .connectionpool import (
File "C:\Python27\lib\site-packages\urllib3\connectionpool.py", line 35, in
from .request import RequestMethods>
File "C:\Python27\lib\site-packages\urllib3\request.py", line 10, in >
from .packages.six.moves.urllib.parse import urlencode
ImportError: cannot import name urlencode
I've tried setting proxies.
I've tried uninstalling and re-installing six
Also
from urllib.parse import urlencode
All to no avail ?

You are (in this case) apparently running Python 2.7.x, however parse module is in urllib package in Python 3.x. In Python 2.x its name was (just) urlparse. Based on that, I'd guess different configuration (mismatch in versions used between Code and/or 3rd party packages and/or python interpreter is behind the problem you're seeing).
EDIT: sorry, I should have also noticed you are looking for urlencode on the last bit. In python 2.7 that is a function in urllib module: from urllib import urlencode.

I fixed this issue with installing Werkzeug to <1.0, which removed the contrib module
pip install Werkzeug==0.16.1

Related

ImportError: cannot import name get_ast_names error on lmfit

I have recently updated to Ubuntu 18.04.1 LTS. After that I tried to run some of my python codes and I got an error related to the lmfit package, here's the code:
import scipy
from asteval import get_ast_names
import lmfit
(I have explicitly added the scipy and asteval modules to check python was finding them porperly)
I obtained this error:
Traceback (most recent call last):
File "/home/maurizio/Documents/Python/Programmini/PhD/TEAS/Test.py", line 3, in <module>
import lmfit
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/__init__.py", line 42, in <module>
from .confidence import conf_interval, conf_interval2d
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/confidence.py", line 12, in <module>
from .minimizer import MinimizerException
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/minimizer.py", line 35, in <module>
from .parameter import Parameter, Parameters
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/parameter.py", line 8, in <module>
from asteval import Interpreter, get_ast_names, valid_symbol_name
ImportError: cannot import name get_ast_names
which basically states that lmfit cannot find the function get_ast_names from asteval, although the python interpreter doesn't complain when I try to import it so it should be properly installed.
I have found this question with a similar problem and the guy in the comments claims he solved it by reinstalling it with pip3, so I tried to compile it with python3 and there's no problem so the issue concerns only python 2
Any help?
I believe you must have conflicting versions of lmfit and asteval. This should have been automatically noticed (and ideally resolved) when installing lmfit, but something there appears to have gone wrong. For the latest releases, you should get
>>> import asteval
>>> asteval.__version__
0.9.12
>>> import lmfit
>>> lmfit.__version__
0.9.11
Depending on how you installed these, you may need to check for and remove older versions of asteval in your $HOME/.local/lib/python2.7/site-packages/ folder and re-do pip install asteval.

Cannot import name ‘etree’

I am getting an error when trying to run a example .py from the serpscrap package.
I am on an iPhoneX in Pythonista.
Any help would be greatly appreciated.
Here is the traceback
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/testing/serpscrapetest.py", line 3, in <module>
import serpscrap
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/serpscrap/__init__.py", line 5, in <module>
from serpscrap.serpscrap import SerpScrap
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/serpscrap/serpscrap.py", line 11, in <module>
from scrapcore.core import Core
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/scrapcore/core.py", line 8, in <module>
from scrapcore.cachemanager import CacheManager
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/scrapcore/cachemanager.py", line 11, in <module>
from scrapcore.parsing import Parsing
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/scrapcore/parsing.py", line 6, in <module>
from scrapcore.parser.google_parser import GoogleParser
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/scrapcore/parser/google_parser.py", line 7, in <module>
from scrapcore.parser.parser import Parser
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/scrapcore/parser/parser.py", line 7, in <module>
import lxml.html
File "/private/var/mobile/Containers/Shared/AppGroup/C27C5B11-1804-47B9-AF18-7287A8337C55/Pythonista3/Documents/site-packages-3/lxml/html/__init__.py", line 54, in <module>
from .. import etree
ImportError: cannot import name 'etree'
SerpScrap doesn't work on iOS at the moment, because of the lxml dependency.
I will update the docs to clearify this point.
But maybe you take a look on https://github.com/pybee/Python-Apple-support .
I'm not familiar with iOS, maybe someone can check if this can help to solve this issue. Otherwise feel free to open an issue on the serpscrap github page.
serpscrap evidently requires lxml, even though for some reason it only documents that requirement for Windows, rather than for all platforms.
You can't install packages that need C extension modules, like lxml, in Pythonista. You seem to have somehow gotten the pure-Python part of lxml installed,1 but that won't do any good without the C extension modules.
There's an open issue, #245, to add lxml as a pre-installed package with Pythonista, which would probably fix your problem.
So, your options are:
Help Pythonista get lxml building so they can close that bug and add lxml to the next version.
Modify serpscrap to work without lxml (if it's just using lxml.etree, it's possible that it would work with the stdlib ETree implementation).
File a feature request with serpscrap and hope someone else does it.
Switch to a different scraper that doesn't require lxml.
1. I'm not sure how that could happen; the install should just fail. But maybe serpscrap does some weird thing that happens to work on Linux and macOS but not iOS, and that's why they only list lxml as a dependency on Windows in the first place?

Python 3: No module named 'tqdm'

I am using Raspberry PI 3 Model B running Kali Linux and i am currently coding a P2P encrypted python chat that runs on python 3. The cryptographic library i am using is called "CryptoShop", which is a '.py' file, not a imported library. I use it the same way that it's 'README' file instructed, so this is not a thing. Before i adeed crypto to the chat, it worked well, but now, i'm having errors since CryptoSHop uses the TQDM math library, and tryed installing it using APT-GET, PIP, by Source and nothing, because, firstly my chat only runs on Python3:
root#kali:~# python PyChat/pychat.py
File "PyChat/pychat.py", line 16
SyntaxError: Non-ASCII character '\xc3' in file PyChat/pychat.py on line 16, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
So when i use Python3:
root#kali:~# python3 PyChat/pychat.py
Traceback (most recent call last):
File "PyChat/pychat.py", line 4, in <module>
from cryptoshop import encryptstring
File "/root/PyChat/cryptoshop.py", line 52, in <module>
from tqdm import *
ModuleNotFoundError: No module named 'tqdm'
CryptoSHop try importing tqdm.
Here's a piece of it's code:
import os
import sys
from tqdm import *
import getpass
import argparse
I am still on the level of basic coding, i get this piece of chat code on the web, and just adeed to it basic user authentication (check if file whit the username exists), improved usability, and adeed crypto.
And sorry by my bad english, it's not my native language ;-)
Thanks in advance.
I am spanish, this happend when you use acents or special charts.
Add this in your first line:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
And use pip3 for install tqdm in python3
pip3 install tqdm

Importing Request library in Python

I am trying to use the Python Library requests using Liclipse. I have added the library but it is giving me the below error message
Traceback (most recent call last):
File "C:\programming\automate_boring_stuff\test.py", line 1, in <module>
import requests
File "C:\programming\libraries\kennethreitz-requests-> > 00fa5f1\requests\__init__.py", line 3, in <module>
from . import ssl_match_hostname
ImportError: cannot import name ssl_match_hostname
Unusually I was previously using it on another computer and it worked finebut on this one by just running import requests it gives me the above error message.
I have tried researching this but most of the answers I have found have been related to different libraries and I could not find how the fixes related to the requests library.
You have mucked up your installation; you appear to have moved the requests/packages/urllib3/packages/__init__.py file into the top-level requests folder.
Remove the package and re-install.

Python/twisted importing twisted.internet.endpoints on windows

I am trying to write a server using twisted on python.
This is the head of my file :
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
The first and the last import work perfectly. I get an error when trying to run the second import with the following traceback :
Traceback (most recent call last):
File "<ipython-input-1-c0d6286e105b>", line 1, in <module>
from twisted.internet.endpoints import TCP4ServerEndpoint
File "C:\Anaconda3\lib\site-packages\twisted-15.5.0-py3.4.egg\twisted\internet\endpoints.py", line 34, in <module>
from twisted.internet.stdio import StandardIO, PipeAddress
File "C:\Anaconda3\lib\site-packages\twisted-15.5.0-py3.4.egg\twisted\internet\stdio.py", line 30, in <module>
from twisted.internet import _win32stdio
ImportError: cannot import name '_win32stdio'
I've already tried solutions like installing pypiwin32 both manually (using whl file) and with pip install. But the problem is not solved.
I am working on windows 7 (yes !) with python 3.4.3 and Twisted 15.0
Thank you for your help.
You are using Python 3, and _win32stdio is not ported to Python 3. If you want the full set of Twisted functionality, you have to run Python 2 (PyPy 4.x+ recommended) for now.
You could install twisted-win with:
pip install twisted-win
From description:
Windows compatibility for Twisted, specifically for Scrapy
It works for me for win 7, python 3.5.2.

Categories

Resources