如何通过Jython导入PIP包? [英] How to import a PIP package through Jython?

查看:148
本文介绍了如何通过Jython导入PIP包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考从Jython插入PIP ,看起来Jython可能包含了PIP:

具体如何从Jython本身调用PIP?既要安装软件包,更重要的是要导入该软件包以供参考?

我没有看到用Jython编写的特定代码示例.

解决方案

将Jython 2.7 安装到~/之后,然后:

thufir@doge:~$
thufir@doge:~$ jython --version
Jython 2.7.0
thufir@doge:~$
thufir@doge:~$ pip --version
pip 1.6.dev1 from /home/thufir/jython/Lib/site-packages (python 2.7)
thufir@doge:~$
thufir@doge:~$ pip freeze
thufir@doge:~$
thufir@doge:~$ pip install virtualenv
Downloading/unpacking virtualenv
   Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB): 1.8MB
downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...
thufir@doge:~$
thufir@doge:~$ mkdir nfl
thufir@doge:~$
thufir@doge:~$ cd nfl
thufir@doge:~/nfl$
thufir@doge:~/nfl$ jython -m virtualenv jve
Cannot find file /home/thufir/jython/Include (bad symlink)
New jython executable in /home/thufir/nfl/jve/bin/jython
Installing setuptools, pip, wheel...done.
thufir@doge:~/nfl$
thufir@doge:~/nfl$ . jve/bin/activate
(jve) thufir@doge:~/nfl$
(jve) thufir@doge:~/nfl$ pip install nflgame
Collecting nflgame
/home/thufir/nfl/jve/Lib/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:310:
SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject
Name Indication) extension to TLS is not available on this platform. This
may cause the server to present an incorrect TLS certificate, which can
cause validation failures. You can upgrade to a newer version of Python to
solve this. For more information, see
https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
   warnings.warn(
/home/thufir/nfl/jve/Lib/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:115:
InsecurePlatformWarning: A true SSLContext object is not available. This
prevents urllib3 from configuring SSL appropriately and may cause certain
SSL connections to fail. You can upgrade to a newer version of Python to
solve this. For more information, see
https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
   warnings.warn(
   Downloading nflgame-1.2.20.tar.gz (34.0MB)
     100%
|████████████████████████████████| 34.0MB 11kB/s
Collecting pytz (from nflgame)
   Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
     100%
|████████████████████████████████| 491kB 158kB/s
Collecting httplib2 (from nflgame)
   Downloading httplib2-0.10.3.tar.gz (204kB)
     100%
|████████████████████████████████| 204kB 136kB/s
Collecting beautifulsoup4 (from nflgame)
   Downloading beautifulsoup4-4.5.3-py2-none-any.whl (85kB)
     100%
|████████████████████████████████| 92kB 147kB/s
Building wheels for collected packages: nflgame, httplib2
   Running setup.py bdist_wheel for nflgame ... done
   Stored in directory:
/home/thufir/.cache/pip/wheels/12/e2/67/97dfa7f48af53140aa18136c8fbf019c455600b00b6cd4f07d
   Running setup.py bdist_wheel for httplib2 ... done
   Stored in directory:
/home/thufir/.cache/pip/wheels/ca/ac/5f/749651f7925b231103f5316cacca82a487810c22d30f011c0c
Successfully built nflgame httplib2
Installing collected packages: pytz, httplib2, beautifulsoup4, nflgame
Successfully installed beautifulsoup4-4.5.3 httplib2-0.10.3 nflgame-1.2.20
pytz-2016.10
(jve) thufir@doge:~/nfl$
(jve) thufir@doge:~/nfl$ pip freeze
appdirs==1.4.0
beautifulsoup4==4.5.3
httplib2==0.10.3
nflgame==1.2.20
packaging==16.8
pyparsing==2.1.10
pytz==2016.10
six==1.10.0
(jve) thufir@doge:~/nfl$

然后:

thufir@doge:~/nfl$ 
thufir@doge:~/nfl$ . jve/bin/activate
(jve) thufir@doge:~/nfl$ 
(jve) thufir@doge:~/nfl$ jython sample.py 
L.McCoy 31 carries for 184 yards and 1 TDs
T.Pryor 13 carries for 112 yards and 0 TDs
S.Vereen 14 carries for 101 yards and 0 TDs
A.Peterson 18 carries for 93 yards and 2 TDs
R.Bush 21 carries for 90 yards and 0 TDs
(jve) thufir@doge:~/nfl$ 
(jve) thufir@doge:~/nfl$ cat sample.py 
import nflgame

games = nflgame.games(2013, week=1)
players = nflgame.combine_game_stats(games)
for p in players.rushing().sort('rushing_yds').limit(5):
    msg = '%s %d carries for %d yards and %d TDs'
    print msg % (p, p.rushing_att, p.rushing_yds, p.rushing_tds)
(jve) thufir@doge:~/nfl$ 

尽管并非严格要求使用虚拟环境.

In reference to hooking into PIP from Jython, It looks like, possibly, PIP is included with Jython:

How, specifically, from Jython itself, is PIP invoked? Both to install a package and, more importantly, to import that package for reference?

I've seen no specific code examples written in Jython.

解决方案

Once Jython 2.7 is installed to ~/ then:

thufir@doge:~$
thufir@doge:~$ jython --version
Jython 2.7.0
thufir@doge:~$
thufir@doge:~$ pip --version
pip 1.6.dev1 from /home/thufir/jython/Lib/site-packages (python 2.7)
thufir@doge:~$
thufir@doge:~$ pip freeze
thufir@doge:~$
thufir@doge:~$ pip install virtualenv
Downloading/unpacking virtualenv
   Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB): 1.8MB
downloaded
Installing collected packages: virtualenv
Successfully installed virtualenv
Cleaning up...
thufir@doge:~$
thufir@doge:~$ mkdir nfl
thufir@doge:~$
thufir@doge:~$ cd nfl
thufir@doge:~/nfl$
thufir@doge:~/nfl$ jython -m virtualenv jve
Cannot find file /home/thufir/jython/Include (bad symlink)
New jython executable in /home/thufir/nfl/jve/bin/jython
Installing setuptools, pip, wheel...done.
thufir@doge:~/nfl$
thufir@doge:~/nfl$ . jve/bin/activate
(jve) thufir@doge:~/nfl$
(jve) thufir@doge:~/nfl$ pip install nflgame
Collecting nflgame
/home/thufir/nfl/jve/Lib/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:310:
SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject
Name Indication) extension to TLS is not available on this platform. This
may cause the server to present an incorrect TLS certificate, which can
cause validation failures. You can upgrade to a newer version of Python to
solve this. For more information, see
https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
   warnings.warn(
/home/thufir/nfl/jve/Lib/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:115:
InsecurePlatformWarning: A true SSLContext object is not available. This
prevents urllib3 from configuring SSL appropriately and may cause certain
SSL connections to fail. You can upgrade to a newer version of Python to
solve this. For more information, see
https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
   warnings.warn(
   Downloading nflgame-1.2.20.tar.gz (34.0MB)
     100%
|████████████████████████████████| 34.0MB 11kB/s
Collecting pytz (from nflgame)
   Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
     100%
|████████████████████████████████| 491kB 158kB/s
Collecting httplib2 (from nflgame)
   Downloading httplib2-0.10.3.tar.gz (204kB)
     100%
|████████████████████████████████| 204kB 136kB/s
Collecting beautifulsoup4 (from nflgame)
   Downloading beautifulsoup4-4.5.3-py2-none-any.whl (85kB)
     100%
|████████████████████████████████| 92kB 147kB/s
Building wheels for collected packages: nflgame, httplib2
   Running setup.py bdist_wheel for nflgame ... done
   Stored in directory:
/home/thufir/.cache/pip/wheels/12/e2/67/97dfa7f48af53140aa18136c8fbf019c455600b00b6cd4f07d
   Running setup.py bdist_wheel for httplib2 ... done
   Stored in directory:
/home/thufir/.cache/pip/wheels/ca/ac/5f/749651f7925b231103f5316cacca82a487810c22d30f011c0c
Successfully built nflgame httplib2
Installing collected packages: pytz, httplib2, beautifulsoup4, nflgame
Successfully installed beautifulsoup4-4.5.3 httplib2-0.10.3 nflgame-1.2.20
pytz-2016.10
(jve) thufir@doge:~/nfl$
(jve) thufir@doge:~/nfl$ pip freeze
appdirs==1.4.0
beautifulsoup4==4.5.3
httplib2==0.10.3
nflgame==1.2.20
packaging==16.8
pyparsing==2.1.10
pytz==2016.10
six==1.10.0
(jve) thufir@doge:~/nfl$

and then:

thufir@doge:~/nfl$ 
thufir@doge:~/nfl$ . jve/bin/activate
(jve) thufir@doge:~/nfl$ 
(jve) thufir@doge:~/nfl$ jython sample.py 
L.McCoy 31 carries for 184 yards and 1 TDs
T.Pryor 13 carries for 112 yards and 0 TDs
S.Vereen 14 carries for 101 yards and 0 TDs
A.Peterson 18 carries for 93 yards and 2 TDs
R.Bush 21 carries for 90 yards and 0 TDs
(jve) thufir@doge:~/nfl$ 
(jve) thufir@doge:~/nfl$ cat sample.py 
import nflgame

games = nflgame.games(2013, week=1)
players = nflgame.combine_game_stats(games)
for p in players.rushing().sort('rushing_yds').limit(5):
    msg = '%s %d carries for %d yards and %d TDs'
    print msg % (p, p.rushing_att, p.rushing_yds, p.rushing_tds)
(jve) thufir@doge:~/nfl$ 

although it's not strictly necessary to use the virtual env.

这篇关于如何通过Jython导入PIP包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆