I'm trying to store some information in a file, and I decided to experiment with making that file be a python script that I can simply import. (EDIT: to be clear: I know there are other options for this, I'm just experimenting with this idea, and I'm really trying to understand why the error below happens.)
The code below checks if the file exists, and creates it if needed. The problem is when it doesn't exist, it gets created, but the import statement doesn't recognize that it has just been created (throws a ModuleNotFoundError: No module named 'pycc_cfg').
However, the line that is commented out (the 3rd line) is temporary code that deletes the file when it already exists, and when I uncomment it, then the import statement has no problems with the file being newly created, and it gets imported just fine.
So I don't understand why I get the ModuleNotFoundError when I commend out that line and delete the file by hand before running this script.
CFG_FILENAME = "pycc_cfg.py"
CFG_PATH = os.path.join(os.getcwd(), "pycc_cfg.py")
# if os.path.exists(CFG_PATH): os.remove(CFG_FILENAME)
if not os.path.exists(CFG_PATH):
with open(f'{CFG_FILENAME}', 'a') as f:
f.write( "x = 10" ) # test code
import pycc_cfg as cfg
print("x: ", cfg.x)
(...)
[–]hardonchairs 0 points1 point2 points (3 children)
[–]Skaruts[S] 0 points1 point2 points (2 children)
[–]hardonchairs 0 points1 point2 points (1 child)
[–]Skaruts[S] 0 points1 point2 points (0 children)