Importing Python Files without the .py Extension with ‘bareimport’

Blake Bradford Avatar

·

Importing Python Files without the .py Extension with ‘bareimport’

With the ‘bareimport’ module in Python, developers can now import Python files without the .py extension. This is especially useful for building tests of functions within standalone scripts, allowing developers to have standalone script files without the need to name them ending in .py.

To demonstrate how ‘bareimport’ works, consider the following example:

“`python
import bareimport
mod = bareimport.import_script_as_module(‘mod’, [‘./mod’, ‘../mod’])

You can also import “mod” now:

from mod import func
“`

Using the import_script_as_module function provided by ‘bareimport’, we can import the script file named ‘mod’ and treat it as a module. This means that we can now access any functions defined in the ‘mod’ script by importing them directly.

Thanks to ‘bareimport’, we no longer have to rely on the .py file extension. This can be highly advantageous in scenarios where the script filenames need to follow a different naming convention or be more descriptive.

Licensing

The ‘bareimport’ module is made available under the CC0 1.0 Universal license. This ensures that users have maximum freedom to use, modify, and distribute the code. You can find more information in the LICENSE file included with the module.

In conclusion, the ‘bareimport’ module provides a convenient solution for importing Python files without the .py extension. By leveraging this module, developers can build more flexible and modular tests for functions within standalone scripts. Whether you’re working on a testing framework or simply exploring different ways to organize your code, the ‘bareimport’ module is a valuable tool to add to your Python toolbox.

If you have any questions or would like to learn more, feel free to ask during the Q&A session.

Leave a Reply

Your email address will not be published. Required fields are marked *