4

Using Python 2.7 on Mac, and IDE is PyCharm. Need to use some Java functions inside Python script, and prefer to use import of Python, for example from com.a.b.c import fooFunc, so that I can use fooFunc in Python script as a Python function. The Java functions are defined in goo.jar.

My question is, where should I place the goo.jar file, so that Python/PyCharm could find the jar file?

Edit 1, add screen snapshot for PyDev for Jython project,

enter image description here enter image description here

Edit 2, add screen snapshot for auto-config error message,

enter image description here

Edit 3, cannot select installed jython interpreter from Eclipse, enter image description here enter image description here

Edit 4, can only select .jar file when configuring jython interpreter,

enter image description here

Edit 5, program runs ok but with red alert error messages from console,

enter image description here

enter image description here

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 3
    Python by itself has no wrappers around the java framework, therefore there is no way for it to import java classes or use java methods. You need to make use of jython. The [jython book](http://www.jython.org/jythonbook/en/1.0/JythonIDE.html) has a chapter on how to use jython in an IDE – smac89 Jul 04 '16 at 22:11
  • @khredos, thanks and vote up. I followed the guide and confused by the following statement, Once you have selected the ‘Interpreter – Jython’ section, you will be presented with an empty list of Jython interpreters at the top of the right side. We clearly need to fix that! So, click the New button, enter ‘Jython 2.5.1’ as the Interpreter Name, click Browse, and find jython.jar inside your Jython 2.5.1 installation. – Lin Ma Jul 04 '16 at 22:48
  • @khredos, (cont'd), my confusion is it seems PyDev is just a toolkit? And I need to install Jython separately, correct? If so, would you mind to point me some guide how to install Jython on Mac OSX (which could be used together as PyDev Eclipse plug-in)? Thanks. – Lin Ma Jul 04 '16 at 22:49
  • 2
    Your IDE isn't really PyCharm - it's Eclipse + PyDev. Any reason for mentioning / tagging the question with PyCharm? – sebastian Jul 05 '16 at 06:13
  • @sebastian, good question and vote up, actually I prefer to use PyCharm. But from the discussion, if I need to import java package into a Python program, I have to use PyDev + Eclipse (which support jython)? Is there a solution to use Java package inside Python program using PyCharm? – Lin Ma Jul 05 '16 at 06:38
  • 1
    I haven't used Jython - but PyCharm should support it just fine: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-sdk.html – sebastian Jul 05 '16 at 06:55
  • @sebastian, vote up for the reply and wondering do you have any experience how to import java package into a python script in PyCharm? Thanks. – Lin Ma Jul 05 '16 at 07:09

1 Answers1

3

Follow up on the comments above, yes if you are using pydev for Eclipse, then that's just the plugin that allows for writing python code with Eclipse. To install Jython, there are a number of guides out there, but it boils down to a two step process:

  1. Download and install Jython. The installation process will usually tell you where Jython is installed
  2. Next Configure your IDE to include the location of jython. As the JetBrains family of IDE's are very much the same, the way I added Jython to IntelliJ was File > Project Structure > SDK > + > Python SDK > Navigate to the Jython Binary

Also take some time to peruse the Jython Book.

smac89
  • 39,374
  • 15
  • 132
  • 179
  • Thanks khredos, vote up for your reply. I tried to create a new project of type PyDev, it seems there is already an option of Jython? Does it mean Jython already installed? Is there a way to check if Jython is already installed? Please refer to **Edit 1** section of edited post. – Lin Ma Jul 05 '16 at 02:09
  • 1
    @LinMa Press that link on the first image that says "Please configure an interpreter before proceeding", this should give you the option to navigate to the install location of jython, then pycharm shall do the rest – smac89 Jul 05 '16 at 02:11
  • Thanks khredos, vote up. Add screen snapshot in **Edit 2** section for error message when select auto-config. Does it mean Jython is not installed? Is there a way to check if Jython is installed somewhere? – Lin Ma Jul 05 '16 at 02:17
  • 1
    @LinMa Did install jython? If so, where did the installer install jython? – smac89 Jul 05 '16 at 02:18
  • Thanks khredos, vote up. I mean this is no my dedicated computer, and is a shared work station which I have sudo access. Just want to learn from you if I have a way to check if Jython is already installed somewhere, so that I do not need to install again? :) – Lin Ma Jul 05 '16 at 02:20
  • 1
    @LinMa If you visit the command prompt and type `jython` and get an error message or something to the same effect, then jython is not installed. You can also type something like `which jython` or `command -v jython`. If nothing promising shows up, then you most likely don't have jython installed – smac89 Jul 05 '16 at 02:29
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116416/discussion-between-khredos-and-lin-ma). – smac89 Jul 05 '16 at 02:31
  • Thanks khredos, vote up. What do you mean `command -v`? Is there a command in Linux called `command`? :) – Lin Ma Jul 05 '16 at 03:50
  • 1
    @LinMa Yes there is, you might not have it on osx by default but if you tried the other methods and couldn't find jython, then just use homebrew to install it – smac89 Jul 05 '16 at 04:06
  • Thanks khredos, vote up. I checked the Central Repository page you referred for jython, it seems there is no install command? Sorry for asking this dumb question, I always use pip, anaconda or yum/rpm for package install, never used the Central Repository you mentioned. :) – Lin Ma Jul 05 '16 at 04:22
  • 1
    @LinMa Under the download section, you click [jar](http://search.maven.org/remotecontent?filepath=org/python/jython-installer/2.7.1b3/jython-installer-2.7.1b3.jar). This will download the jar file to your machine, then run the command `java -jar location/of/the/jar/you/download` – smac89 Jul 05 '16 at 04:24
  • Thanks khredos, vote up and installed successfully (use the graphical interface), but when I execute command like `jython` or `which jython` still no output. If you have any further thoughts to verify the installation, it will be great. – Lin Ma Jul 05 '16 at 04:37
  • 1
    @LinMa That's ok, now you just need to open pycharm again and navigate to the location of jython – smac89 Jul 05 '16 at 04:37
  • Thanks khredos, vote up. Please refer to **edit 3** for the new screen snapshots, it seems I cannot select the location of the interpreter -- there is a forbidden sign around directory root. I install under directory `/private/var/root/jython2.7.1b3/jython.jar`, if you have any further thoughts, it will be great. – Lin Ma Jul 05 '16 at 04:46
  • 1
    @LinMa you need to navigate to the `bin` directory under `jython2.7.1b3`. There you will find `jython`, that's what you want. Note there is no `.jar` extension – smac89 Jul 05 '16 at 05:17
  • Thanks khredos, vote up and it seems I can only select .jar file when configuring jython interpreter. Please refer to **Edit 4** section for screen snapshot for updated post. From the link you referred (http://www.jython.org/jythonbook/en/1.0/JythonIDE.html#minimal-configuration), it seems we can only select .jar as well. If you have any further comments, it will be great. – Lin Ma Jul 05 '16 at 05:21
  • 1
    @LinMa Ok try selecting the jar, but this time enter a name for the interpreter (from picture 3). Name it something like `Jython2.7.1b3` – smac89 Jul 05 '16 at 05:58
  • Thanks khredos, vote up and tried the sample program at, http://www.jython.org/jythonbook/en/1.0/JythonIDE.html#hello-pydev-creating-projects-and-executing-modules, it works with PyDev project with jython interpreter. But it has error message each time I run, error message like this, is it a key error? I post it as **edit 5** section of the post, `*sys-package-mgr*: can't write cache file for '/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/ext/zipfs.jar' *sys-package-mgr*: can't write index file` – Lin Ma Jul 05 '16 at 06:02
  • 1
    @LinMa The reason for this is that you have installed jython in a place where it does not have write access to. In this case, it is trying to create a cache directory in the place where it was installed, but the filesystem permissions are not set for doing that. Possible solutions include reinstall it somewhere a non root user will have access to it, remove and reinstall it using homebrew (as I mentioned before). [Someone else](http://stackoverflow.com/questions/17758458/sys-package-mgr-cant-create-package-cache-dir-when-run-python-script-with-j) might have seen this before. Gl and gnite – smac89 Jul 05 '16 at 06:20
  • 1
    Thanks khredos, vote up and such error are index/cache related, I think they are only related to performance boost and has nothing to do with functional? – Lin Ma Jul 05 '16 at 06:37
  • Thanks khredos, mark your reply as answer. Thanks for all the help. :) – Lin Ma Jul 05 '16 at 06:51
  • There is no more File -> Project Structure, not sure when this changed – Jason S Aug 06 '18 at 22:38
  • 1
    @JasonS: In PyCharm, open the File>Settings>[YourProjectName]>Project Interpreter>Add and select the Jython executable in [JyhonInstallDir]/bin/. This will register Jython interpreter. Then make sure you have selected Jython as the Project Interpreter and press Apply button followed by OK. – Sampath Mar 06 '19 at 13:08