Skip to content

Conversation

@Ephrame-A
Copy link
Contributor

Overview

This PR is intended to enhance the import! predicate in src/metta.pl to support direct importing of Python (.py) files. This allows PeTTa to load and utilize Python modules without needing separate setup steps or complex path management.

Changes

  • src/metta.pl:
    • Updated import! to detect .py extensions.
    • Implemented logic to resolve the absolute path of the Python file, append its directory to sys.path, and import the module using
  • examples/python_import_example.metta:
    • Added a new example file demonstrating how to import a Python script and call its functions from PeTTa.

Usage

You can now import Python scripts directly using their relative or absolute path:

; Import a python script named 'utils.py' located in the same directory (or relative to working dir)
(import! &self "utils.py")

; Or use an absolute path
(import! &self "C:/path/to/utils.py")

; Call a function from the imported module
(py-call (utils.my_function arg1))

Updated import! Predicate in src/metta.pl

'import!'(Space, File, true) :- atom_string(File, SFile),
                                working_dir(Base),
                                ( file_name_extension(ModPath, 'py', SFile) ->
                                    absolute_file_name(SFile, Path, [relative_to(Base)]),
                                    file_directory_name(Path, Dir),
                                    file_base_name(ModPath, ModuleName),
                                    py_call(sys:path:append(Dir), _),
                                    py_call(builtins:'__import__'(ModuleName), _)
                                  ; atomic_list_concat([Base, '/', SFile, '.metta'], Path),
                                    load_metta_file(Path, _, '&self')
                                ).
  1. Convert File to string SFile and get working directory Base.
  2. If SFile has .py extension, resolve absolute path Path, extract directory Dir and module name ModuleName, add Dir to sys.path, and import the module.
  3. Otherwise, append .metta to SFile and load as MeTTa file.

Copy link
Owner

@patham9 patham9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you very much!
Only minor suggestion:
Please add the Python script to the ./examples folder then it is more self-contained, calling it python_import_file.py or so.

@Ephrame-A
Copy link
Contributor Author

Awesome, thank you very much! Only minor suggestion: Please add the Python script to the ./examples folder then it is more self-contained, calling it python_import_file.py or so.

Thank you for your comment. Now I have added it.

Copy link
Owner

@patham9 patham9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you!

Copy link
Owner

@patham9 patham9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues popped up:

  • load_metta_file(Path, _, '&self') should not be there. In main branch it is load_metta_file(Path, _, Space), else it can only import to &self after merging this PR.
  • The two ! cases in python_import.metta should be turned into !(test (py-call ...) expectedOutput) each, else the test system sees the metta file as failing as it did not generate a test success output.

After these two are resolved I will be happy to merge it in!
UPDATE: since it is a weekend I decided to add these two adjustments myself to speed it up a bit.

Formatting consistency with rest of the project, and changed the load_metta_file back to Space.

Signed-off-by: Patrick Hammer <[email protected]>
formatting consistency

Signed-off-by: Patrick Hammer <[email protected]>
Added to test output.

Signed-off-by: Patrick Hammer <[email protected]>
Signed-off-by: Patrick Hammer <[email protected]>
@patham9 patham9 merged commit a94394d into patham9:main Nov 30, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants