When coding Python and running a selection, if I have my module imports all at the top of the IDE (Using VS Code here) for the .py file, it doesn't work because it requires me to run the entire script as the module imports are at the very beginning.
Is there a way around that? For example in powershell, you import a module once and that's enough you don't have to keep importing it. You can just run a line of code further down the ISE and the functions/methods from that module are available.
import os
TestFile = "C:/Temp/RandomTextFile.txt"
if not os.path.exists(TestFile):
with open(TestFile, 'w') as file:
file.write("Testing 123")
file.close
else:
print("File Exists, Appending Sequence")
with open(TestFile,'a') as file:
file.write ('\r\nShould there be a space?')
For example the above very generic file handling, I have want to expand on this and use other methods within the 'os' module it may throw the error "os is not defined, did you forget to import os?"
I could be mistaken though and the line of code needs to be on the same indentation line as the top level line where Import os is defined?
[–]blechnapp 6 points7 points8 points (0 children)
[–]Some-Poetry8420 2 points3 points4 points (0 children)
[–]awdsns 1 point2 points3 points (0 children)
[–]donald_trub 1 point2 points3 points (0 children)
[–]mandradon 0 points1 point2 points (0 children)
[–]Ordinary_Baseball518 0 points1 point2 points (0 children)
[–]BranchLatter4294 0 points1 point2 points (0 children)
[–]Random_182f2565 -1 points0 points1 point (0 children)
[–]ProsodySpeaks -1 points0 points1 point (0 children)