lib
within the folder that the main script is in, but I was needing to know how to import from it. I searched around for a while, but wasn't finding anything useful, and though I found something, the answer says:Note that injecting directories intoI think the reasoning behind avoiding adding paths tosys.path
is not the usual way to set up Python to search for modules. Usually it is preferable to add/home/jake/Documents
to your PYTHONPATH environment variable.
sys.path
is because it might not work across different computers (not "portable"). CircuitPython MCUs with modules on SD cards require /sd/
to be added to sys.path
because there is no other option (that I know of).Anyway, I did end up finding what I wanted, but I wish it had more graphics instead of a bunch of text and examples to follow along with. I found from there that it was just
import folder.module
, so I went to try it out with the simpler programme and it worked with no other changes besides the import. I went to copying portions of the large programme to make a much smaller testbed, and started off with the common functions, which was aptly named common.py
. There were some changes I did need to make, one of which was to move the global variable regex
into its own class and function to return the regular expression used for input validation. Another was to move the setting clipboard text function from the main window to its own class and function, another was to write/get the required precision to a file because there wouldn't be an easy way to be able to get it from the combo box of the main window, and the last major one was to move the message box function from the main window. I tested a couple simple classes and made adjustments as necessary when Python gave me errors, and eventually played around with stuff in common.py
a bit. I found out that I could just have a function for regex, setting clipboard text, and messagebox, so I made the according changes and it worked just fine.Changing the large programme was fairly easy, because I just had to replace
regex
with regex()
and window.to_clip(
to to_clip(
before I even started modulisation of any of the 37 calculation classes, and the modulisation just required me to import the usual PyQt5 classes, common.py
, changing the working directory when necessary (for reference images), and importing the module in the main script. I also made some quality-of-life improvements to the main script as well (now that it's a few hundred lines instead of a few thousand), and also took the time to insert a "clear/reset calculator" function to each of the modules instead of relying on old code that doesn't always work or causes the programme to crash. Anyway, it's much more manageable, and there's not a whole lot else to really say.I'll now go over some of the things that might be useful for anyone looking for the information. First, let's start with a file tree that will be used as reference.
Example filetree:
- project/
- main.py
- modules/
- module1.py
- module2.py
- sub_modules/
- sub_module1.py
- sub_module2.py
Things to know:
- The
modules/
folder must be in the same place asmain.py
(withinproject/
in this case). - The structure used for import is
modules.module1
formodules/module1.py
, andmodules.sub_modules.sub_module1.py
formodules/sub_modules/sub_module1.py
. - If
main.py
importsmodule2.py
, which needs to importmodule1.py
, thenmodule2.py
would still usemodules.module1
for importing becausemain.py
serves as the basis for the location. - Modules can contain (class-less) functions and classes.
- Use
from modules.module1 import *
instead ofimport modules.module1
if you'd like to use function or class names as-is (function()
instead ofmodules.module1.function()
). (Probably obvious if you've used Python enough.)
I think that's everything, since I have nothing else in my notes.
No comments:
Post a Comment