温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python安装py4j的方法

发布时间:2020-07-31 09:23:14 来源:亿速云 阅读:742 作者:清晨 栏目:编程语言

小编给大家分享一下python安装py4j的方法,相信大部分人都还不怎么了解,因此分享这边文章给大家学习,希望大家阅读完这篇文章后大所收获,下面让我们一起去学习方法吧!

Mac安装:

pip 安装

pip install py4j
py4j.jar包路径:/usr/share/py4j/py4j0.x.jar or /usr/local/share/py4j/py4j0.x.jar

conda 安装(或者使用Anaconda-Navigator安装)

conda install py4j
py4j.jar包路径:/anaconda3/share/py4j/py4j0.x.jar

使用:

1、java代码

// 需要导入py4j.jar包
import py4j.GatewayServer;
import java.util.Random;
public class EntryPoint {
    private static final Random random = new Random();
    public int randInt() {
        return random.nextInt();
    }
    public static void main(String[] args) {
        EntryPoint app = new EntryPoint();
        // py4j服务
        GatewayServer gatewayServer = new GatewayServer(app);
        gatewayServer.start();
        System.out.println("Gateway Server Started");
    }
}

2、Python代码

from py4j.java_gateway import JavaGateway
# 初始化
gateway = JavaGateway()
def main():
    # 调用Java-EntryPoint中的randInt函数
    a = gateway.entry_point.randInt()
    print(a)
if __name__ == '__main__':
    main()

注意:

1.导入py4j.jar包

2.运行Java代码

3.运行Python代码(必须在2之后)

以上是python安装py4j的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI