1

I'm uploading the following .html to WinSCP, but facing ModuleNotFoundError: No module named 'mysql'

  • html script
<html>
    <head>
      <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
      <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    </head>

  <body>
    <b><p>title <u><label id="AAA"></label></u> </p></b>
    <br>
  



    <py-script>
        import mysql.connector

        mydb = mysql.connector.connect(
          host="196.168.100.141",
          user="root",
          password="password123", 
          database="database_db",  
          auth_plugin='mysql_native_password'
        )
                        
        mycursor = mydb.cursor()
        mycursor.execute("SELECT row_01 FROM database")                       
                                                 
        myresult = mycursor.fetchall()
        
        list_01 = []
        
        for row in myresult:
          temp_val = row[0]
          list_01.append(temp_val)
    </py-script>
  </body>
</html>
  • error message
Traceback (most recent call last):
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 435, in eval_code
    .run(globals, locals)
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 304, in run
    coroutine = eval(self.code, globals, locals)
  File "<exec>", line 1, in <module>
ModuleNotFoundError: No module named 'mysql'
  • pic: the ModuleNotFoundError on online website the ModuleNotFoundError on online website

I already do pip install mysql on my pc, but online website still get this error, so where show I pip install or what shall I do


UPDATE:

making sure I get mysql.connector installed on website host pc

>>> import mysql.connector
>>> print(mysql.connector.__version__)
8.0.31

the showing of hosting website with model

[root@localhost bin]# pip3 show mysql-connector-python
Name: mysql-connector-python
Version: 8.0.31
Summary: MySQL driver written in Python
Home-page: http://dev.mysql.com/doc/connector-python/en/index.html
Author: Oracle and/or its affiliates
Author-email:
License: GNU GPLv2 (with FOSS License Exception)
Location: /usr/local/lib/python3.6/site-packages
Requires: protobuf

I know to fix ModuleNotFoundError: No module named 'mysql'is to pip install mysql-connector-python

and I did pip3 install mysql-connector-python, and successfully download under python3.6

what else I can do?

  • 2
    Disregarding the error, you cannot use mysql inside pyscript. See: https://stackoverflow.com/questions/74246324/pyscript-pyodide-mysql-db-connection-websockets-problem-cant-connect-to and https://github.com/pyscript/pyscript/issues/381 – Mike Scotty Jan 09 '23 at 08:39
  • if this way isn't working, how can I show/print my python extract mySQL result in html layout, ex: `

    mySQL data

    `
    –  Jan 09 '23 at 08:52
  • 3
    You need to have a server-side script that acts as a bridge between the client side and the database. You send a http request to the server side script that interacts with the database and sends a response back to the client via http. – Shadow Jan 09 '23 at 09:04
  • 1
    installing Python packages via `pip install mysql`, has no effect on the Python environment inside the browser. Packages such as `mysql` require APIs (sockets, SSL) that are forbidden inside the browser sandbox. – John Hanley Jan 09 '23 at 20:49
  • Even if this did work, you're exposing your database credentials in plaintext, as root user, no less, via the HTML, and therefore anyone would be able to edit the page source and run `DROP TABLES;`, for example – OneCricketeer Jan 11 '23 at 07:07
  • yeah, I forget that will be the potential risk –  Jan 11 '23 at 08:00
  • what the key word for this `You need to have a server-side script that acts as a bridge between the client side and the database. You send a http request to the server side script that interacts with the database and sends a response back to the client via http` that at least I can find some tutorial to do so @Shadow –  Jan 11 '23 at 08:04
  • 1
    @DCcon it's called web development. – Shadow Jan 11 '23 at 08:09
  • or can I just hide part of the code from html file? @OneCricketeer –  Jan 11 '23 at 08:09
  • No? Then the browser will not see it, then the "hidden part" will not run **at all**. – OneCricketeer Jan 11 '23 at 08:20

0 Answers0