如何在 Jython 中安装各种 Python 库?

发布于 2024-11-25 17:44:44 字数 224 浏览 2 评论 0原文

我知道我可以使用 Java 安装 Jython,并且可以在使用 Python 的地方使用 Jython。 Jython shell 工作正常。

在 Jython 中,如何安装通常通过 pip 安装的 lxmlScrappyBeautifulSoup 等库或easy_install

I know that I can install Jython with Java and that I can use Jython where I use Python. The Jython shell is working fine.

In Jython, how can I install libraries like lxml, Scrappy and BeautifulSoup that I'd normally install via pip or easy_install

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤蝉 2024-12-02 17:44:44

某些 Python 模块(例如 lxml)需要使用 C 语言编写组件。这些组件在 Jython 中不起作用。

大多数 Python 包都可以正常工作,并且您可以使用与 CPython 中使用的工具相同的工具来安装它们。这是 Jython Book 附录 A 中描述的

要获取 setuptools,请从以下位置下载 ez_setup.py
http://peak.telecommunity.com/dist/ez_setup.py。然后,前往
下载文件所在的目录并执行:

$ jython ez_setup.py

[easy_install脚本将被]安装到bin目录下
Jython 安装(示例中的 /home/lsoto/jython2.5.0/bin
多于)。如果您经常使用 Jython,那么最好
将此目录添加到 PATH 环境变量中,这样就不需要
每次要使用 easy_install 时都必须输入完整路径或
其他脚本安装到此目录。

自己测试了一下,在Jython中安装setuptools后,pip安装正确:

$ sudo /usr/bin/jython2.5.2b1/bin/easy_install pip
Searching for pip
[...]
Installing pip-2.5 script to /usr/bin/jython2.5.2b1/bin
Installing pip script to /usr/bin/jython2.5.2b1/bin

Installed /usr/bin/jython2.5.2b1/Lib/site-packages/pip-1.0.2-py2.5.egg
Processing dependencies for pip
Finished processing dependencies for pip

$ sudo /usr/bin/jython2.5.2b1/bin/pip install bottle
Downloading/unpacking bottle
  Downloading bottle-0.9.6.tar.gz (45Kb): 45Kb downloaded
  Running setup.py egg_info for package bottle
    Installing collected packages: bottle
  Running setup.py install for bottle
Successfully installed bottle
Cleaning up...

$ jython
Jython 2.5.2b1 (Release_2_5_2beta1:7075, Jun 28 2010, 07:44:20) 
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_26
Type "help", "copyright", "credits" or "license" for more information.
>>> import bottle
>>> bottle
<module 'bottle' from '/usr/bin/jython2.5.2b1/Lib/site-packages/bottle$py.class'>
>>>

Some Python modules, like lxml, have required components in C. These won't work in Jython.

Most Python packages will work fine, and you can install them using the same tools as you use in CPython. This is described in Appendix A of Jython Book:

To get setuptools, download ez_setup.py from
http://peak.telecommunity.com/dist/ez_setup.py. Then, go to the
directory where you left the downloaded file and execute:

$ jython ez_setup.py

[The easy_install script will be] installed to the bin directory of
the Jython installation (/home/lsoto/jython2.5.0/bin in the example
above). If you work frequently with Jython, it’s a good idea to
prepend this directory to the PATH environment variable, so you don’t
have to type the whole path each time you want to use easy_install or
other scripts installed to this directory.

Testing it myself, after installing setuptools in Jython, pip installed correctly:

$ sudo /usr/bin/jython2.5.2b1/bin/easy_install pip
Searching for pip
[...]
Installing pip-2.5 script to /usr/bin/jython2.5.2b1/bin
Installing pip script to /usr/bin/jython2.5.2b1/bin

Installed /usr/bin/jython2.5.2b1/Lib/site-packages/pip-1.0.2-py2.5.egg
Processing dependencies for pip
Finished processing dependencies for pip

$ sudo /usr/bin/jython2.5.2b1/bin/pip install bottle
Downloading/unpacking bottle
  Downloading bottle-0.9.6.tar.gz (45Kb): 45Kb downloaded
  Running setup.py egg_info for package bottle
    Installing collected packages: bottle
  Running setup.py install for bottle
Successfully installed bottle
Cleaning up...

$ jython
Jython 2.5.2b1 (Release_2_5_2beta1:7075, Jun 28 2010, 07:44:20) 
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_26
Type "help", "copyright", "credits" or "license" for more information.
>>> import bottle
>>> bottle
<module 'bottle' from '/usr/bin/jython2.5.2b1/Lib/site-packages/bottle$py.class'>
>>>
深爱不及久伴 2024-12-02 17:44:44

从 v2.7b4 开始,Jython 发行版包含 ensurepip 模块,这简化了安装pip 和 setuptools:

jython -m ensurepip

谨防 sys.platform=='win32' 问题 将妨碍您使用依赖此方法来确定主机的 PyPI 包平台。

As of v2.7b4, the Jython distribution includes the ensurepip module, which simplifies installation of pip and setuptools:

jython -m ensurepip

Beware of sys.platform=='win32' issue that will get in your way of using PyPI packages which relying on this method to determine host platform.

滿滿的愛 2024-12-02 17:44:44

作为 Jython,尽管您拥有 Java 库的强大功能,但不受无法安装一些 python C 库的限制。

例如,您最好使用 Jsoup 而不是 Beautiful soup 或寻求完整的解决方案,例如 Jtidy

使用 Jaxp 而不是 lxml。

另一个适合您需求的选项是 NekoHTML

Being Jython though you have the power of the Java libraries, not the limitation of being unable to install a few python C libraries.

For example you would be better using Jsoup instead of Beautiful soup or go for a full solution like Jtidy.

Use Jaxp instead of lxml.

Also another option that fits your reuirements is NekoHTML

泅渡 2024-12-02 17:44:44

我安装了 CPython 和 Jython。如果我想通过 pip 在 Jython 中安装软件包,我会执行以下操作。

jython -m pip install <package_name>

例如,要在 jython 中安装机器人框架(因为我想用 Java 编写关键字库),我做了

jython -m pip install robotframework

I have both CPython and Jython installed. Here is what I do if I want to install a package in Jython via pip.

jython -m pip install <package_name>

For example to install the robot framework in jython (as I want to write the keyword libraries in Java), I did

jython -m pip install robotframework
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文