A python module is a file with extension .py. The easiest way to import a module in a file you are working, say file1.py, that you are writing is to be located in the same folder than this file, and inside it add the line
import MyModule.py
More on this can be found in this link.
You should test your package on three levels: locally, globally and as a PyPI package (through TestPyPI).
You start with nothing but your .py files wich are your modules. With testing this modules locally I mean that you have to navigate to the parent folder of your package and run py (or sage) there and then do
import mypackage
At this point you should verify that everything works fine, that the dependencies are correctly imported and there is no syntax errors.
In 2022 it is not so clear if you can still pip-install your own package system-wide withour previously having uploaded it to TestPyPI/PyPI. It seems that there is some new restriction imposed by pip. So we may have to skip this step.
In this stage we'll create the files needed to upload our package to TestPyPI and effectively upload it. If everything goes fine, we'll be ready to upload it to the real (production) PyPI.
The way packages should be created, the structure od the directory and files had suffered slight changes in 2022. So, this is a tutorial that you should follow to create a python package nowadays:
Packaging Python Projects - Python Packaging User Guide
Once you already upload it to PyPI you can open a terminal o the Sage Notebook system, and there enter the following command.
pip install packagagename
This can also be done if you only upload it to PyPITest, since when the uploading is finished you will get the url to place where it is hosted inside the PyPITest server, and there the command to install the package from there.
To configure the .pypirc file and where it should be located if your using Windows (you can still save your credentials to TestPyPI there even if the above tutorial doesn't mention it).
In every module that uses SAGE code you have to import SAGE as a library adding the following line before your code.
from sage.all import *
Note: For some unknown reason, it is not enough to add this line only on inside the __init__.py file .
To test your package (locally) you have to navigate the folder that contains the setup.py file the to the open the SAGE shell and run the sage command.
Once the interactive interpreter is running, you need to import your package.
If you run this on using the python command on the python shell, you will get an error because SAGE is not installed in it. It is needed that you use the SAGE shell.
As we are creating a python package, we need to use only python statements and avoid the syntactic sugar introduced by SAGE. For example, we shouldn't use the expression R.<x,y> = ... to declare the generators of the ring R. Instead, we write it the long way (as this is just an abbreviation for a sequence of python sentences).
For some reason, to import other modules it is needed to use the syntax from mymodule import * even if mymodule is in the same directory than the caller module.