Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Brython packages

Browser interface

Brython-specific built-in modules

Working with Brython



Implementation of import

Like in standard Python, you can install modules or packages Python in your application by putting them in the root directory, or in directories with a file

Note that modules must be encoded in utf-8 ; the encoding declaration at the top of the script is ignored.

For instance, the application can be made of the following files and directories :

+ app

A Python script in app.html can run the imports

import users
import app.records

If the standard distribution has been included in the page by

<script type="text/javascript" src="brython_stdlib.js"></script>

the script can also run

import datetime
import re

To import modules or packages, Brython uses the same mechanism as CPython : to resolve "import X", the program looks for a file in several places :

  • a module X in the standard distribution
  • a file in the root directory
  • a file in directory X

Since the browser has no direct access to the file system, looking for a file must be done by an Ajax call, which returns an error message if there is no file at the specified url.


The process described above has two main drawbacks :

  • the relatively big size of brython_stdlib.js (more than 3 Mb)
  • the time taken by Ajax calls

To optimise imports, if Brython was installed by pip, you can generate a file brython_modules.js which only holds the modules used by the application.

For that, open a console window, navigate to the application directory and execute

python -m brython --modules

Note that this program parses the Brython code in all the scripts, modules and HTML pages of the directory and its sub-directories. The CPython version used must be compliant with this Brython code : for instance if there are f-strings in the Brython code, CPython 3.6+ is required, otherwise you would get syntax errors.

You can then replace all the occurrences of

<script type="text/javascript" src="brython_stdlib.js"></script>


<script type="text/javascript" src="brython_modules.js"></script>