The programs published in this blog uses extensively multiple files located in so many different folders but I have not done any documentation about this topic except this previous post Global Variables Handling across Pyqt6 Windows but it does not covers what if I want to store the files on a different folder? Also another important topic to discuss is what if I want to access functions from other files located on a different folder?
Why it is necessary to use multiple files and located in different folders? Well as the program really grows so large it will be difficult to maintain if it uses only one file and what a bunch of programmers need to create additional features and modules? These are some of the situations wherein it is very necessary to use multiple files and folders. I have discuss another scenario why it is important in my previous post(Spoiler Alert: Upgrading My Python Flask Login Demo Program).
At this point, you are maybe familiar how to handle global variables if you have previously read my blog post Global Variables Handling across Pyqt6 Windows. It is not just for PyQt6 framework, it can also be applied generally on any large python projects. In my sample program, I used config.py to declare the global variable and functions.py to update those global variables. But the situation in that sample program assumed that all python scripts are located on the same folder. There is a big difference if you just store python scripts on different folders, the handling of global variables is different. I have prepared a demo program below which addresses the issue storing python scripts on different folders:
The file structure:
The codes:
app.py:
1 | from general import * |
__init__.py:
1 2 3 4 5 6 | from general import config, functions functions.addx(2, 3) print(config.x) functions.suby(5,4) print(config.y) |
config.py:
1 2 | x = 0 y = 0 |
functions.py:
1 2 3 4 5 6 7 | import config def addx(arg1, arg2): config.x = arg1 + arg2 def suby(arg1, arg2): config.y = arg1 - arg2 |
You can see from the file structure that __init__.py, config.py and functions.py are inside the folder general which is the subfolder of the project root directory global where the app.py is located.
If I run this program(app.py), the python script __init__.py gets automatically executed because it acts as the constructor and it treats the folder general as a class. What this program does is to basically perform addition and subtraction and store the results of both operations in global variables x and y which are declared in config.py. These functions are in the python script functions.py There will be no error but the output will be 0 and 0 because app.py could not see the global variables x and y. But if I modify __ini_.py which is below:
1 2 3 4 5 6 7 8 9 | import sys sys.path.append('c:/web_flask/global/general') import config, functions #from general import config, functions functions.addx(2, 3) print(config.x) functions.suby(5,4) print(config.y) |
and run app.py again, I will get 5 and 1 as a result because app.py can now see the global variable. What I did was, I added the location of the python scripts config.py and funtions.py to the python path so that it can recognize this python scripts as part of the program or as a library. The result will also be the same if I move config.py and functions.py to another subfolder.
No comments:
Post a Comment