Quinn Ganstern
Quinn Ganstern

Reputation: 23

Can't access python module from html script which is definitely installed on my web server?

I have installed a module on my server which can be imported on a python file from any directory. I have tested this by creating a test.py file and import module-name which returns no errors when run from the command line.

However, when I import the module into a python file which is referenced by a script tag in my index.html, I get an error which says the module cannot be found in the working directory. I am using brython to call a python file through a script tag here.

The example index.html code reads:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"> </script>
  </head>
  <body onload="brython()">
    <script type="text/python" src="file-name.py"></script>
  </body>

The file-name.py would import module-name which produces the not found error.

Any ideas on how to fix this?

Upvotes: 0

Views: 1385

Answers (1)

furas
furas

Reputation: 142909

Based on Brython's doc Implementation of import you have to keep files in folder with index.html.

If you want to use standard modules - like sys, os then you have to load brython_stdlib.min.js

<script src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.min.js"></script>    

When I use print( sys.path ) to see in which folders it searchs modules then it displays

['http://0.0.0.0:8000', 'moz-extension://dfafe65d-6769-4df9-8940-b084b18c2a0b/js/Lib/site-packages']

When I use print( os.getcwd() ) to see Current Working Directory them it displays

http://0.0.0.0:8000

Bryton is Python's interpreter and it doesn't uses Python installed on disk and it doesn't uses its modules. It runs code in browser and it uses AJAX to load modules so they have to be accessible by URL

http://0.0.0.0:8000/main.py
http://0.0.0.0:8000/other.py

My file structure:

project
|
+-- index.html
|
+-- main.py
|
+-- other.py

project/index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"> </script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.min.js"></script>    
  </head>
  <body onload="brython()">
    <script type="text/python" src="main.py"></script>
  </body>
</html>  

project/main.py

import sys  # OK
import os   # OK

print('Hello World')
print( sys.path )
print( os.getcwd() )

import other  # OK

import requests   # ERROR 

project/other.py

print('Other File')

Tested with server

python3 -m http.server

EDIT:

I tried load module requests using soft link on Linux

ln -s /usr/local/lib/python3.7/dist-packages/requests/ requests

and it loaded file requests/__init__.py but it needed other modules like urllib3 which I had to link too.


EDIT:

If I link

ln -s /usr/local/lib/python3.7/dist-packages/ dist-packages

and add

sys.path.append('http://0.0.0.0:8000/dist-packages')

then in DevTools in Firefox/Chrome (tab: Network) I see it loads other modules needed by requests but it take long time - probably 30 seconds.

BTW: I expect that problem can be if module uses C/C++ library because Brython rather can't execute this code. So more complex modules would need more links (or link some folder

Upvotes: 2

Related Questions