解决Win下Jython安装pip失败的问题

安装Jython时发现pip没成功装上,导致不能方便地安装模块。这里用一种临时的解决方式来解决这个问题,主要还是期待Jython官方支持CPython3。

我的环境:

Win10 x64
Java 1.8.0
Jython 2.7.1

Jython安装步骤

  1. 下载Jython Installer的jar包,到https://www.jython.org/download 或者 http://repo.maven.apache.org/maven2/org/python/jython-installer/ 下载

  2. 运行jython Installer的jar包,选择Standard模式安装(假设安装到D:\jython2.7.1目录)

  3. Standard模式下是有勾选安装pip的,实际上是安装失败了。

    找问题所在:
    通过命令行安装方式(java -jar D:\jython-installer-2.7.1.jar --console)安装打印如下错误:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Installing pip and setuptools
    90 %
    Exception:
    Traceback (most recent call last):
    ...

    File "D:\test\jython\Lib\platform.py", line 1396, in system
    return uname()[0]
    File "D:\test\jython\Lib\platform.py", line 1213, in uname
    machine = os.uname()[4]
    IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

    调用os.uname()时错误。搜了下,windows下没有os.uname()函数。所以就修改调用os.uname()的地方

执行CPython命令python -c "import platform; print(platform.uname())",例如打印出:

1
uname_result(system='Windows', node='DESKTOP-XXX', release='10', version='10.0.18362', machine='AMD64', processor='...')

打开D:\jython2.7.1\Lib\platform.py,根据上面的输出修改两处:

1
2
3
4
5
6
7
8
9
10
11
1213行:
node = _node()
# machine = os.uname()[4]
machine = "AMD64"

1271行:
try:
# system,node,release,version,machine = os.uname()
system,node,release,version,machine = ('Windows', 'DESKTOP-XXX', '10', '10.0.18362', 'AMD64')
except AttributeError:
no_os_uname = 1
  1. 安装pip:D:\jython2.7.1\bin\jython.exe -m ensurepip

  2. 使用Jython pip安装模块,以jsbeautifier模块为例:D:\jython2.7.1\bin\jython.exe -m pip install jsbeautifier

    安装jsbeautifier时打印如下错误,也给出了错误原因和解决方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    java.lang.RuntimeException: java.lang.RuntimeException:
    Encountered too large method code in
    D:\jython2.7.1\Lib\site-packages\jsbeautifier\tests\generated\tests.py

    Please provide a CPython 2.7 bytecode file (.pyc) to proceed, e.g. run
    python -m py_compile D:\jython2.7.1\Lib\site-packages\jsbeautifier\tests\generated\tests.py
    and try again.

    Alternatively provide proper CPython 2.7 execute command via
    cpython_cmd property, e.g. call
    jython -J-Dcpython_cmd=python
    or if running pip on Jython:
    pip install --global-option="-J-Dcpython_cmd=python" <package>

    由于我没装CPython2.7,而且jsbeautifier模块也可以正常导入就没去管了。