I'm trying to run a program using spyder instead of ipython notebook because it currently runs faster. The data is imported and extracted using
run util/file_reader.py C:/file_address
Obviously the run command doesn't work in normal python and I can't find an equivalent, I've looked at the various how to replace ipython magic commands Q&As on here and generally but I can't find one for the run command...
Is there a module or set of code that would work as an equivalent in normal python?
What you want is a bit weird. Precisely, the run magic runs the given file in the current ipython namespace as if it were the __main__ module. To get precisely the same effects would require a bit of effort.
with open("util/file_reader.py") as f:
src = f.read()
# set command line arguments
import sys
sys.argv = ["file_reader.py", "C:/file_address"]
# use "__main__.py" as file name, so module thinks its the main module
code = compile(src, "__main__.py", "exec")
exec(code)
If would be easier and better to define a main function in file_reader.py and then call that at the end of the file if an if __name__ == "__main__":
eg.
util/file_reader.py
def main(filename):
print(filename)
if __name__ == "__main__":
import sys
main(sys.argv[1])
So now you can easily run the code in this module by importing it and then calling the main function.
eg.
import util.file_reader
util.file_reader.main("C:/file_address")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With