Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

NameError: name 'unicode' is not defined #24

Open
ossplus opened this issue Feb 12, 2015 · 16 comments
Open

NameError: name 'unicode' is not defined #24

ossplus opened this issue Feb 12, 2015 · 16 comments

Comments

@ossplus
Copy link

ossplus commented Feb 12, 2015

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

@berendbaas
Copy link

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.
e2bed09

@ossplus
Copy link
Author

ossplus commented Feb 20, 2015

yes. python3.4

@berendbaas
Copy link

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.

@ergodicbreak
Copy link

@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)

@berendbaas
Copy link

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

@kenny-evitt
Copy link

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

RockyRoad29 added a commit to RockyRoad29/Python that referenced this issue Jan 15, 2016
The Python3 way, using unicode strings instead of bytes.
@RockyRoad29
Copy link
Contributor

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.

@dav009
Copy link

dav009 commented Feb 5, 2016

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

@keith-hall
Copy link

@dav009 see #47 (comment) for why no Python fixes are being merged at the moment

@kenny-evitt
Copy link

@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.

@meownoid
Copy link

meownoid commented Sep 7, 2016

Still not working in 8.0.1. Is LT dead?

@sbauer322
Copy link
Contributor

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 #47. If anyone is able to review or test the fix then I am happy to merge it in.

@stemd
Copy link
Collaborator

stemd commented Sep 8, 2016

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.

@sbauer322
Copy link
Contributor

@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 added a commit that referenced this issue Apr 11, 2017
Python3 unicode -  Fixing issue #24 -NameError: name 'unicode' is not defined
@Aurora11111
Copy link

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

@omkarvijay5
Copy link

@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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests