Exec heck out of it
Python 2.x:
execfile("C:\\X\Y\\Z")
Python 3 +:
with open("C:\\X\Y\\Z", "r") as f:
exec(f.read())
, - ( - ), . , "-f __name__ == '__main__':", ( __name__ __main__, , script).
, Zen of Python, , :
- , ( python, , python). exec()/execfile() globals()/locals(), .
?
? script:
radius = 3
def field_of_circle(r):
return r*r*3.14
print(field_of_circle(radius))
:
>>>radius = 5
>>>execfile("script_above.py")
28.26
>>>print(radius)
3
, ? , , script. . :
x = 1
script:
import very_simple_module
very_simple_module.x = 3
, :
>>>import very_simple_module
>>>print(very_simple_module.x)
1
>>>execfile("executed_script.py")
>>>print(very_simple_module.x)
3
, , python .
... python . ( ) sh ( PyPI):
>>>import subprocess
>>>subprocess.call(["python", "C:\\X\Y\\Z"], shell=True)
>>>from sh import python
>>>python("C:\\X\Y\\Z")
. script
, : script pythonpath script:
>>>import sys
>>>if "C:\\X\\Y" not in sys.path:
sys.path.append("C:\\X\\Y")
>>>import Z
, , , pythonpath, , python , script, .
, "-f __name__ == '__main__':" . :
>>>radius = 5
>>>import first_example_script
>>>print(radius)
5
>>>print(first_example_script.radius)
3
, . , script sys.py, , python sys .