0

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/brython@3.8.9/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?

  • 2
    browser can run only JavaScript code - you can't use Python in browser. – furas Jul 05 '20 at 04:43
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Jul 05 '20 at 04:44
  • if script can't find module then maybe you have two Pythons installed and this script use one Python but module is installed in other Python. – furas Jul 05 '20 at 04:45
  • 1
    I d recommend you use AJAX and communicate with the server. As @furas said, you can t call a python file in a `script` tag. However, maybe the module can be imported in the client side (you have to research this) using a bundle and the `require` method. – netlemon Jul 05 '20 at 06:38
  • Sorry I forgot to state this in the original question: I'm using brython to reference python scripts. The question should reflect that update now. – Quinn Ganstern Jul 05 '20 at 17:51
  • use `print( sys.path )` to see in which folders it searchs modules. And `print( os.getcwd() )` to see Current Working Directory in which it also searchs modules. Script can be executed in different folder then you expect and then it search other files in this folder to import them. – furas Jul 05 '20 at 19:53
  • BTW: maybe bryton documentation can help you [Implementation of import](https://brython.info/static_doc/en/import.html) - it seems imported file has to be in the same folder as `index.html` – furas Jul 05 '20 at 20:03
  • I have no problem to `import module-name` if file `module-name.py` is in the same folder as `index.html`. And for starndard modules like `sys` I had to load `brython_stdlib.js` like `` – furas Jul 05 '20 at 20:10

1 Answers1

2

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/brython@3.8.9/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/brython@3.8.9/brython.min.js"> </script>
    <script src="https://cdn.jsdelivr.net/npm/brython@3.8.9/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

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you for your help! I have added a `sys.path.append('http://example.com/dist-packages')` with the soft link as you suggested. In DevTools I see that it looks for `module-name/__init.py__` and `module-name.py`, however, it still returns a 404 not found error. This seems like it should've worked, but I am still unable to `import module-name`. – Quinn Ganstern Jul 06 '20 at 21:43
  • first put `module-name.py` in folder `index.html` to see if it will work. Maybe it get 404 for different problem. – furas Jul 06 '20 at 22:07
  • 1
    BTW: if you add `sys.path.append('http://example.com/dist-packages')` before import then it should search `http://example.com/module-name.py`, next `http://example.com/module-name/__init__.py` and next `http://example.com/dist-packages/module-name.py`. But your server has to serve files in url `http://example.com/dist-packages` - standard `python3 -m http.server` serves all files in folder (and subfolders) but `Flask`, `Django` need function or changes in settings to serve it. – furas Jul 06 '20 at 22:12
  • I tried appending those links to my `sys.path` but I still receive a not found error. I believe this is because it continues to search for a file `module-name.py`. However, in the module I have installed, there is no file `module-name.py`. I have tried renaming the module directory to `module-name.py`, but no luck. I think your solution works for most python modules out there, but not the one I am trying to import? – Quinn Ganstern Jul 07 '20 at 20:52
  • why don't you use the simplest method - put `module-name.py` in the same folder as `index.html` – furas Jul 07 '20 at 23:51
  • BTW: if it can't find the you should check in DevTools what urls it tries to read. And then check if your server is configured to serve this url. If you server is not configured to serve some files then it will not work even if you put file in the same folder as `index.html` - ie. server built-in in Flask serves only files in subfolder `static` and urls defined as `@app.route('/')` and other files will not send. So important is also what server (program) you use to server these files. – furas Jul 07 '20 at 23:54
  • @furas, was it the first time you learn and use Brython when you answered this question? That is impressive! Your answer is very informative. Thanks! – RayLuo Mar 13 '21 at 18:22
  • 1
    @RayLuo some time ago I answered the question [How can I redirect all Brython output to a textarea element](https://stackoverflow.com/questions/61348313/how-can-i-redirect-all-brython-output-to-a-textarea-element) because it was interesting problem. Later I described it on blog with more examples: [Python: How to redirect output from print() to textarea in Brython](https://blog.furas.pl/python-brython-how-to-redirect-output-from-print-to-textarea-in-brython-gb.html). Probably it was the first and the last time I used `Brython`. – furas Mar 15 '21 at 18:27
  • @furas, oh I see. It was [your first comment to the question](https://stackoverflow.com/questions/62736893/cant-access-python-module-from-html-script-which-is-definitely-installed-on-my/62746152?noredirect=1#comment110943621_62736893) made me assume you did not know Brython at July 5, 2020; so I thought you went on learning it and then came up with this answer all in the same day. :-) Now I know that is not the case. But you are still awesome in sharing your knowledge. Thanks! – RayLuo Mar 16 '21 at 16:48