brython-dev / brython

Brython (Browser Python) is an implementation of Python 3 running in the browser
BSD 3-Clause "New" or "Revised" License
6.36k stars 507 forks source link

Using Psutil with Brython #1464

Closed kethan1 closed 4 years ago

kethan1 commented 4 years ago

I am using Brython, and I want to use the module psutil. I ran the following command in the command prompt.

pip install brython
brython-cli --install
python -m brython --make_package psutil

I then loaded the appropriate js scripts (brython.js, brython_stdlib.js, psutil.brython.js).

Importing Brython works fine, but when I try to do import psutil, anything after import psutil does not work.

My code is:

<!DOCTYPE HTML>
<html> 
<head lang="en"> 
    <title>Home</title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="brython.js"></script>
    <script type="text/javascript" src="brython_stdlib.js"></script>
    <script type="text/javascript" src="psutil.brython.js"></script>
</head>
<body onload="brython()">
    <script type="text/python">
        from browser import document, alert
        import psutil

        alert("Hi!")
    </script>
</body> 
</html>

I ran it with the following command: python -m http.server 8000

PierreQuentel commented 4 years ago

In order to use a CPython package such as psutil, you need to install it locally with pip install psutil, then in the Brython directory run

brython-cli --add_package psutil

From a quick look at the package, you will probably have to set the attributes sys.platform and os.name to be able to use it.

The option --make_package is different, it creates Brython packages.

kethan1 commented 4 years ago

Thanks!