pyjnius icon indicating copy to clipboard operation
pyjnius copied to clipboard

SyntaxError: invalid syntax

Open Malkhafaji95 opened this issue 4 years ago • 3 comments

I'm trying to test out the sample code provided on Github and I'm getting a syntax error. I have a feeling this might be a pathing issue but pyjnius is in the environment path.

the words jnius and autoclass are underlined in red. When i hover over it, it was " Unresolved reference 'jnius'"

Code:

from time import sleep from jnius import autoclass

Hardware = autoclass('org.renpy.android.Hardware') print 'DPI is', Hardware.getDPI()

Hardware.accelerometerEnable(True) for x in xrange(20): print Hardware.accelerometerReading() sleep(.1)


error:

Traceback (most recent call last): File "/snap/pycharm-community/214/plugins/python-ce/helpers/pydev/pydevd.py", line 39, in from _pydevd_bundle.pydevd_comm import CMD_SET_BREAK, CMD_SET_NEXT_STATEMENT, CMD_STEP_INTO, CMD_STEP_OVER,
File "/snap/pycharm-community/214/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 107, in from _pydev_bundle import _pydev_completer File "/snap/pycharm-community/214/plugins/python-ce/helpers/pydev/_pydev_bundle/_pydev_completer.py", line 9, in import java.lang #@UnusedImport File "/home/malkhafa/PycharmProjects/pythonProject1/java.py", line 5 print 'DPI is', Hardware.getDPI() ^ SyntaxError: invalid syntax


Thanks in advance

Malkhafaji95 avatar Oct 24 '20 21:10 Malkhafaji95

It looks like the path PyCharm is using points to an environment that does not have pyjnius installed. Either you didn't install it correctly or there is an issue with PyCharm. Most likely the later. Sometimes IDEs won't automatically find the Python environment you intend and you need to manually point it to the right one. I have this issue from time to time with my IDE. I don't use PyCharm so I can't help you with that part. Good luck.

hx2A avatar Oct 24 '20 22:10 hx2A

I ran the code using the terminal and i got this error:

Traceback (most recent call last): File "java.py", line 2, in from jnius import autoclass ImportError: No module named jnius

the environmental paths are :

/home/malkhafa/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

and pyjnius us install at :

./.local/lib/python3.6/site-packages

Malkhafaji95 avatar Oct 24 '20 22:10 Malkhafaji95

  • your syntax error is from using python2 print syntax, rather than python3, (python2 needs parenthesis), we need to update the example code indeed, but i would assume a python dev would know that.
  • your ide probably defines a virtualenv, so doesn't see globaly installed packages.
  • this code won't work on desktop, it won't be able to find the relevant java classes, and to run them, as these are android-specific, to test this code you need to package your code for android using p4a/buildozer.

this place is not for user support, but for bugs.

tshirtman avatar Oct 24 '20 23:10 tshirtman

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project. Let us know if this comment was made in error, and we'll be happy to reopen the issue.

github-actions[bot] avatar Oct 27 '23 07:10 github-actions[bot]