如何通过 Jython 导入 PIP 包



参考从Jython钩接到PIP,看起来,PIP可能包含在Jython中:

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

  • Jython easy_install错误

  • 如何使用 jython setup.py 安装?

具体来说,如何从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$ 

尽管并非绝对必须使用虚拟环境。

最新更新