I have created a file (module) "functions.py" with functions that I want to use with different programs that are in different folders (01.py, 02.py, 03.py). The tree structure is something like this:
Python 3
funciones
funciones.py
01
01.py
02
02.py
03
03.py
I have searched for information and I have seen that in order to be able to call a function inside "functions.py" from any other program, I have to include an " init .py" file in the folder where "functions.py" is located . . I have done it and the tree is as follows:
Python 3
funciones
funciones.py
__init__.py
01
01.py
02
02.py
03
03.py
If, for example, I open "01.py" and import the functions file import funciones as fun
and then call one of the functions, fun.saludo()
I get an error because the module has not been imported.
I clarify that if I put "functions.py" in the same folder as one of the programs (01,02 or 03), then I can call it from the program in that folder without problem.
What do I have to do to be able to import the "functions.py" module from any other .py file?
PS: I work with Spuder 3 through Anaconda.
Apparently you have already found a solution with PYTHONPATH :
But there is another similar alternative, taking into account the following tree structure:
and assuming that restructuring is not an option. You can use
sys.path.append('/path/')
as follows:01.py :
Simply add the subdirectory ('/python3/functions') to the
path
Python one, in order to try to import in a somewhat normal way.