Cannot import urlencode name on program run

Asked

Viewed 168 times

1

I need to run a routine, but system informs that it cannot load the name urlencode (I don’t know if I can call this function, class), I searched for similar libraries, but I’m also not sure I’m doing it correctly.

from hashlib import sha256
from urllib import urlencode


def create_signature(secret_key, data=None):
    if data is None:
        data = {}

When I do a Pip search (Pip search urllib): In addition to a large list, I get a "logging error",

there is a way to find out if any of these references can resolve the need for urlencode?

aiourllib (0.1.3)                     - HTTP library for asyncio
apiclient (1.0.3)                     - Framework for making good API client libraries using urllib3.
autoauth (1.0.2)                      - authentication in urllib2 and requests for the command line
beautifulscraper (1.0.4)              - Python web-scraping library that wraps urllib2 and BeautifulSoup.
browser-cookie3 (0.6.0)               - Loads cookies from your browser into a cookiejar object so can download with urllib and other libraries the same content you see in the web browser.
browsercookie (0.6.3)                 - Loads cookies from your browser into a cookiejar object so can download with urllib and other libraries the same content you see in the web browser.
python-rest-client (0.3)              - A REST Client for use in python, using httplib2 and urllib2.
go-proxy-client (0.2)                 - Handler for urllib2 to use a go-proxy instance as proxy server
dewpoint (0.4.1)                      - urllib2 openers that sign requests for OAuth or Amazon APIs
python-ntlm-fixed (1.0)               - Python library that provides NTLM support, including an authentication handler for urllib2 (with fixed hashlib dependency).
goto (0.1.3)                          - Urllib object wrapper
httplib2shim (0.0.1)                  - A wrapper over urllib3 that matches httplib2's interface
ndg-httpsclient (0.4.2)               - Provides enhanced HTTPS support for httplib and urllib2 using PyOpenSSL
httpsproxy_urllib2 (1.0)              - Python 2.5 urllib2 patched to use CONNECT for https proxies
keepalive (0.5)                       - urllib keepalive support for python
Links (1.0)                           - Link-utilities missing from urllib|urllib2
micropython-urllib (0.0.0)            - Dummy urllib module to MicroPython
micropython-urllib.parse (0.5.2)      - CPython urllib.parse module ported to MicroPython
micropython-urllib.urequest (0.4.3)   - urllib.urequest module for MicroPython
urllib3-mock (0.3.3)                  - A utility library for mocking out the `urllib3` Python library.
MultipartPostHandler (0.1.0)          - A handler for urllib2 to enable multipart form uploading
MultipartPostHandler2 (0.1.5)         - A handler for urllib2 to enable multipart form uploading
notrequests (0.6)                     - Like Requests, but using urllib2.
python-ntlm (1.1.0)                   - Python library that provides NTLM support, including an authentication handler for urllib2. Works with pass-the-hash in additon to password authentication.
opbeat_python_urllib3 (1.1)           - An urllib3 transport for Opbeat
robotframework-pycurllibrary (0.9.5)  - Robot Framework test library for client-side URL transfer
restlib (0.9)                         - Extensions for the standard urllib2 to support RESTful client applications
ServiceAgent007 (0.3.2)               - Wrapper of urllib2 suited for HTTP REST service requests.
timeout-urllib2 (0.1)                 - Protocol/Connection based timeout facility for urllib2
urllib2_file (0.2.1)                  - urllib2 extension which permit multipart file upload
urllib2_kerberos (0.1.6)              - Kerberos over HTTP Negotiate/SPNEGO support for urllib2
urllib2_prior_auth (0.2.0)            - Module resolving http://bugs.python.org/issue19494
urllib3 (1.20)                        - HTTP library with thread-safe connection pooling, file post, and more.
  INSTALLED: 1.20 (latest)
urllib4 (0.2)                         - urllib style HTTP/FTP client library base on pycurl
urllib5 (5.0.0)                       - Just increment the number and create a new lib. Never fix the original one.
urllib_kerberos (0.2.0)               - Kerberos over HTTP Negotiate/SPNEGO support for urllib2
urllibcache (1.0.1)                   - Simple urllib2 caching handler
urlpath (1.1.2)                       - Object-oriented URL from `urllib.parse` and `pathlib`
WordInfo (1.0.2)                      - A library to find defintions, pos, synonyms, antonyms, and word origins. It is python 3 compatible and requires bs4 and urllib.request
yieldfrom.urllib.request (0.1.1)      - asyncio version of urllib.request (urllib2)
yieldfrom.urllib3 (0.1.4)             - Asyncio HTTP library with thread-safe connection pooling, file post, and more.
--- Logging error ---
Traceback (most recent call last):
  File "f:\programs\python\python36\lib\logging\__init__.py", line 989, in emit
    stream.write(msg)
  File "f:\programs\python\python36\lib\site-packages\pip\_vendor\colorama\ansitowin32.py", line 141, in write
    self.write_and_convert(text)
  File "f:\programs\python\python36\lib\site-packages\pip\_vendor\colorama\ansitowin32.py", line 169, in write_and_convert
    self.write_plain_text(text, cursor, len(text))
  File "f:\programs\python\python36\lib\site-packages\pip\_vendor\colorama\ansitowin32.py", line 174, in write_plain_text
    self.wrapped.write(text[start:end])
  File "f:\programs\python\python36\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ub294' in position 55: character maps to <undefined>
Call stack:
  File "f:\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "f:\programs\python\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\Programs\Python\Python36\Scripts\pip.exe\__main__.py", line 9, in <module>
    sys.exit(main())
  File "f:\programs\python\python36\lib\site-packages\pip\__init__.py", line 233, in main
    return command.main(cmd_args)
  File "f:\programs\python\python36\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "f:\programs\python\python36\lib\site-packages\pip\commands\search.py", line 52, in run
    print_results(hits, terminal_width=terminal_width)
  File "f:\programs\python\python36\lib\site-packages\pip\commands\search.py", line 118, in print_results
    logger.info(line)
Message: 'hacked-http (0.0.1)                   - Hacked Urllib2 \ub294 warning.or.kr \uac19\uc740 \ubc29\uc5b4\ubcbd\uc744 \ub6ab\ub294\ub370 \ubaa9\uc801\uc774 \uc788\uc2b5\ub2c8\ub2e4.'
Arguments: ()
  • Only import urllib, terminal, works?

1 answer

4


It is a compatibility problem of Python2.x with Python3.x

In python 2.x o urlencode was in the package urllib, 3.x is in the urllib.parse, exchange import should solve your problem:

from urllib.parse import urlencode

If you want it to work on both 2.x and 3.x do this:

try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode
  • Thank you very much, Marcos. It worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.