Python icon indicating copy to clipboard operation
Python copied to clipboard

NameError: name 'unicode' is not defined

Open ossplus opened this issue 10 years ago • 16 comments

just a very simple code : print ("first"), have issue:

Traceback (most recent call last): File "~\AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 193, in handleEval code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'eval') File "~\AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 50, in ensureUtf if type(s) == unicode: NameError: name 'unicode' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "~AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 197, in handleEval code= compile(ensureUtf(code), ensureUtf(data[2]["name"]), 'exec') File "~\AppData\Local\LightTable\plugins\Python\py-src\ltmain.py", line 50, in ensureUtf if type(s) == unicode: NameError: name 'unicode' is not defined

ossplus avatar Feb 12 '15 14:02 ossplus

Same problem here, are you running python 3 by chance? In that case, It seems to be because python 3 doesn't have the unicode type anymore as it's been replaced by/incorperated into the 'str' type. Is development being done on python 3 with this plugin? Would be great if it was. e2bed09776f954308f335b6a7d5654d2692edd08

berendbaas avatar Feb 19 '15 08:02 berendbaas

yes. python3.4

ossplus avatar Feb 20 '15 10:02 ossplus

If you want to hotfix it for now, you can simply replace the "unicode" by "str" on line 51 of the /py-src/ltmain.py file in the python plugin directory for Light Table. Don't forget to switch it back if you ever need to use python 2 ;) It would be great, if there was functionality for doing this which was backwards compatible, or if you could switch in your settings between python2 and python3 evaluation, that would then select the corresponding plugin to run. I don't know which design would be preferred by the Light Table team however.

berendbaas avatar Feb 20 '15 12:02 berendbaas

@berendbaas maybe this would be a reasonable compatible fix?


def ensureUtf(s):
  try:
      if type(s) == unicode:
        return s.encode('utf8', 'ignore')
  except: 
    return str(s)

ergodicbreak avatar Feb 22 '15 20:02 ergodicbreak

@ergodicbreak Seems like a good way to go to me.

berendbaas avatar Feb 22 '15 21:02 berendbaas

@berendbaas @ergodicbreak – care to comment on PR #28?

kenny-evitt avatar Jun 28 '15 06:06 kenny-evitt

Hi, I'm completely new to LightTable and I might be wrong but I think I found a better fix that I just submitted

    def ensureUtf(s, encoding='utf8'):
      """Converts input to unicode if necessary.

      If `s` is bytes, it will be decoded using the `encoding` parameters.

      This function is used for preprocessing /source/ and /filename/ arguments
      to the builtin function `compile`.
      """
      # In Python2, str == bytes.
      # In Python3, bytes remains unchanged, but str means unicode
      # while unicode is not defined anymore
      if type(s) == bytes:
        return s.decode(encoding, 'ignore')
      else:
        return s

I tested it with python2.7.11 and python3.5.1 on my archlinux platform.

Then, if accepted it makes sense to rename the function to reflect its actual role, which I did in a later commit.

RockyRoad29 avatar Jan 15 '16 13:01 RockyRoad29

Is this issue moving at all? It seems quite relevant yet quite inactive

dav009 avatar Feb 05 '16 10:02 dav009

@dav009 see https://github.com/LightTable/Python/pull/47#issuecomment-173007386 for why no Python fixes are being merged at the moment

keith-hall avatar Feb 05 '16 11:02 keith-hall

@dav009 We need someone to takeover the Python plugin. As Keith pointed out, we've got potential fixes, but we have no-one to review and merge them. Even if you (or anyone else) doesn't want to take responsibility for the entirety of Python in LT or even just the LT Python plugin, I'm willing to merge PRs if enough commenters agreed on which ones should be merged.

kenny-evitt avatar Feb 05 '16 12:02 kenny-evitt

Still not working in 8.0.1. Is LT dead?

meownoid avatar Sep 07 '16 17:09 meownoid

Sorry to hear this is still a problem, @meownoid.

LightTable is not dead, but the current python plugin maintainer, @stemd, appears to be inactive or otherwise not available. That said, we are probably in need of someone to actively work on the plugin.

I am not familiar with Python, but there is an open pull request that may contain a fix https://github.com/LightTable/Python/pull/47. If anyone is able to review or test the fix then I am happy to merge it in.

sbauer322 avatar Sep 07 '16 23:09 sbauer322

I managed to get Python3 working on my setup, idea was to test that on all platforms (Linux, Mac, Windows) before merging. Also there was an idea to detect Python2 and Python3, and to adapt automatically, but ClojureScript is not so straightforward, and not all code is well commented.

stemd avatar Sep 08 '16 14:09 stemd

@stemd, would you be able to point out the poorly documented Clojurescript files or code that are relevant to you? Happy to bump them up in the list of things to document soon.

sbauer322 avatar Sep 08 '16 21:09 sbauer322

in python2.x: item = unicode(item, 'utf-8') in python3.x: item = str(item.encode('utf-8'))

Aurora11111 avatar Aug 27 '18 08:08 Aurora11111

@berendbaas maybe this would be a reasonable compatible fix?

def ensureUtf(s):
  try:
      if type(s) == unicode:
        return s.encode('utf8', 'ignore')
  except: 
    return str(s)

Python 3 renamed the unicode type to str, the old str type has been replaced by bytes renaming unicode occurrences with str worked for me

omkarvijay5 avatar Feb 06 '19 17:02 omkarvijay5