Issue2852

classification
Title: Unable to import requests module in Jython
Type: Severity: urgent
Components: Library Versions: Jython 2.7.1
Milestone:
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Vikhe, jeff.allen
Priority: normal Keywords:

Created on 2019-12-18.13:27:19 by Vikhe, last changed 2020-02-01.08:50:15 by jeff.allen.

Messages
msg12854 (view) Author: yogita (Vikhe) Date: 2019-12-18.13:27:18
Hi Team,

I want to import requests, json module in my java code .
I have installed Jpython on unix & also installed requests module as well .
My code is like :
pythonInterp.exec("sys.path.append(\"/home/ec2-user/yogita/jpython/Lib/site-packages\")");
		pythonInterp.exec("print (sys.path)");
		pythonInterp.exec("import requests");

And output is like 
['/home/ec2-user/yogita/jpython/Lib', '/home/ec2-user/yogita/jpython/Lib', '__classpath__', '__pyclasspath__/', '/home/ec2-user/yogita/jpython/Lib/site-packages', '/home/ec2-user/yogita/jpython/Lib/site-packages']
Exception while importing python module: Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named requests
 

/home/ec2-user/yogita/jpython/Lib/site-packages
[ec2-user@ip-10-111-111-111 site-packages]$ ll
total 80
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 11:25 certifi
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 11:25 certifi-2019.11.28.dist-info
drwxrwxr-x  3 ec2-user ec2-user 4096 Dec 18 11:25 chardet
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 11:25 chardet-3.0.4.dist-info
-rw-rw-r--  1 ec2-user ec2-user  126 Dec 18 08:59 easy_install.py
-rw-rw-r--  1 ec2-user ec2-user 2901 Dec 18 08:59 easy_install$py.class
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 11:25 idna
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 11:25 idna-2.8.dist-info
drwxrwxr-x 10 ec2-user ec2-user 4096 Dec 18 08:59 pip
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 08:59 pip-9.0.1.dist-info
drwxrwxr-x  4 ec2-user ec2-user 4096 Dec 18 08:59 pkg_resources
-rw-rw-r--  1 ec2-user ec2-user  119 Jun 30  2017 README
drwxrwxr-x  2 ec2-user ec2-user 4096 Dec 18 11:26 requests
msg12894 (view) Author: Jeff Allen (jeff.allen) Date: 2019-12-24.17:30:10
It works for me at the prompt (Windows 10, Jython 2.7.2b2, pip 19.1), on a clean installation:

PS issue2852> inst\bin\pip install requests
...
PS issue2852> inst\bin\jython
Jython 2.7.2b2 (v2.7.2b2:b9b60766cabe, Nov 1 2019, 07:46:45)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_231
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> import sys
>>> sys.path
['', '...\\issue2852\\inst\\Lib', '__classpath__', '__pyclasspath__/', '...\\.local\\lib\\jython2.7\\site-packages', '...\\issue2852\\inst\\Lib\\site-packages']

And the same from Java. On a quick check, also good on Jython 2.7.1. Argument -v to Jython will give you more information about where it looks during import (and -vv even more).

You don't need to work so hard on the sys.path: you can see Jython has already done it.
History
Date User Action Args
2020-02-01 08:50:15jeff.allensetstatus: pending -> closed
2019-12-24 17:30:11jeff.allensetstatus: open -> pending
nosy: + jeff.allen
messages: + msg12894
priority: normal
milestone: Jython 2.7.1 ->
resolution: works for me
2019-12-18 13:27:19Vikhecreate