Forum Archive

Importing between files

vignesh

On pythonista, how do I share information between python files. If I had a file called 'main.py' and then another called 'extension.py' and there are functions I want from extension.py in main.py, I would use 'import extension'. I do this and it calls an error that states that 'there is no module extension'

sulcud

It is easy, put the extension.py and the main.py file in a folder with a file called __init__.py, them in the main.py

import extension
extension.your_function()

I hope this work for you

sulcud

You can see this for more help

Documentation

vignesh

@sulcud Thanks so much!

JonB

The __init__is only necessary if you want to import by folder name. The key is that, if you run main.py, extension.py must be in the same folder, or otherwise on sys.path (for instance, in site-packages).