Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run with python3 #39

Closed
securedirective opened this issue Aug 9, 2015 · 9 comments
Closed

Unable to run with python3 #39

securedirective opened this issue Aug 9, 2015 · 9 comments

Comments

@securedirective
Copy link

I am trying to use pytg with Python v3.2.3 on a Raspberry Pi (latest model, latest Raspbian), but I'm getting multiple SyntaxError: invalid syntax. Am I missing something really obvious? If your project is compatible with python3, then why is it complaining about python2.7-style lines of code?

The telegram-cli is installed and works great, just typing commands via its command line. For using it with pytg, I am running it as specified in your documentation: bin/telegram-cli --json -P 4458

What works:

  • After git clone for pytg, running python setup.py install works fine
  • Running python pytg/examples/ping.py works great (Python 2.7.3)

What does not:

  • After git clone for pytg, running python3 setup.py install shows
Traceback (most recent call last):
  File "setup.py", line 8, in <module>
    from pytg import VERSION
  File "/home/pi/pytg/pytg/__init__.py", line 9, in <module>
    from .encoding import to_unicode as u
  File "/home/pi/pytg/pytg/encoding.py", line 36
    return u"\\"
               ^
SyntaxError: invalid syntax
  • Running python3 pytg/examples/ping.py shows
  File "pytg/examples/ping.py", line 57
    if msg.text == u"ping":
                         ^
SyntaxError: invalid syntax
@luckydonald
Copy link
Owner

There was one python 3 version which dislike u""strings.
Can you check if

foo = u"bar"

works for you?
(Just try that quickly, in the interactive shell or something.)

If not, you can either remove all us before strings, or upgrade python 3 to some newer version
(which sadly means compiling by yourself... It will take some time, but is worth it in the end - I did that on my Pi too.)

@luckydonald
Copy link
Owner

Edit: Just remove the u in front of the strings. (Python 3+)

"text"
#instead of:
u"text"

Another option, compatible with all Python versions is to import the function pytg use to convert all strings to the right format:

from pytg.encoding import to_unicode as u

This will get any kind of string (byte-encoded or already unicode) correctly to utf-8 unicode (of the according native type str in python 3, or unicode in python 2).

Then do

foo = u("bar")

(wrap the string with u(), which is th imported to_unicode())

@securedirective
Copy link
Author

You are correct. In my Python 3.2.3 interactive terminal, foo = u"bar" causes a syntax error. According to https://docs.python.org/3/whatsnew/3.3.html?highlight=unicode, that syntax was added back in Python 3.3.

For the benefit of others, may I suggest that your Github README.md be updated to read "Works with Python 2.7 and 3.3" instead of "Works with Python 2.7 and 3"?

I tried your second suggestion in an interactive terminal. But even the line from pytg.encoding import to_unicode as u causes the following syntax error immediately:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pytg/__init__.py", line 9, in <module>
    from .encoding import to_unicode as u
  File "pytg/encoding.py", line 36
    return u"\\"
               ^
SyntaxError: invalid syntax

I'm not an avid Python programmer, so it sounds like I'm better off sticking with Python 2.7 for now, on the Raspberry Pi.

@luckydonald
Copy link
Owner

# now just change the  
u"stuff"  #python 2 (and python 3.3+)
# to
"stuff"  # python 3.0+
# or, after importing
from pytg.encoding import to_unicode as u
# to
u("stuff")  # all pythons

But I'll add that. Actually only the examples are not python 3.0-3.3 compatible, pytg itself is.

@securedirective
Copy link
Author

I don't understand. The syntax error is not happening in my script or even the setup.py script. It is happening in encoding.py itself, in the very to_unicode function that you suggest I import as 'u'. It won't even import, because right there Python 3.2 shows the syntax error.

Now, assuming you meant modifying lines 33 and 34 in your encoding.py (replacing b'' with '' and replacing u"" with ""), this at least allows sudo python3 setup.py install to succeed. But ultimately, when I run my own script to use pytg, I just get more of the same syntax errors in the DictObject/encoding.py instead (which looks almost identical to yours).

@luckydonald
Copy link
Owner

Ohh.
I thought that were the example files.
Can you check if it works, if you remove line 33 to 34 in encoding.py?
(The if x == b'\\': part)

@securedirective
Copy link
Author

Yes, that seems to do the trick. It's a workaround, at least.

I removed lines 33-34 in pytg/encoding.py:

        if isinstance(x, binary_type):
                #if x == b'\\':
                #       return u"\\"
                return x.decode("utf-8")

That solves the original error. But I just get the same error for DictObject instead. So I also removed lines 30-31 in /usr/local/lib/python3.2/dist-packages/DictObject-0.1.1-py3.2.egg/DictObject/encoding.py:

        if isinstance(x, binary_type):
                #if x == b'\\':
                #       return u"\\"
                return x.decode("utf-8")

Now, everything works in the script that I built to use pytg. For your ping example to work, though, I must edit examples/ping.py and replace all uses of u"........" with "........" (lines 57,58,59,61,64).

Thanks for your help! This is the first time I've ever worked with Unicode in Python, so I wasn't sure at first what I was even looking at or why the syntax errors were occurring. Now, to continue my Raspberry Pi project... :)

@luckydonald
Copy link
Owner

Your welcome. I will update pytg as well as DictObject (and several other projects where I use that file) to reflect that.

@luckydonald luckydonald added bug and removed wontfix labels Aug 13, 2015
luckydonald added a commit to luckydonald/luckydonald-utils that referenced this issue Aug 13, 2015
luckydonald added a commit that referenced this issue Aug 13, 2015
…luckydonald-utils```) . The file is almost the same, but changed to fix issue #39.
@luckydonald
Copy link
Owner

It should now be fixed.
If importing luckydonaldUtils fails, install it with pip install luckydonald-utils manually.

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

No branches or pull requests

2 participants