0

I have two python scripts (main.py, sub.py) in different locations. I want to run sub.py from main.py.

sub.py

print("I am for test")
x = 100

I have found multiple options to do that from multiple source which works partially.

main.py

exec(open("c:\\SomePath\\sub.py").read())

# or

%run "c:\\SomePath\\sub.py"

Both the commands execute the sub.py and create 'x' in globals() but don't print anything.Is there a way (simple, a single line code preferable) to do that?

Raja
  • 157
  • 1
  • 11
  • try import this file: `import sub`. If the file in another diretory: https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – Mikhail Stepanov Aug 24 '18 at 06:53
  • Note: you'll get access to `x` through `sub.x`. Bug you can get it directly, if you'll imort module as `from sub import x` or `from sub import *` – Mikhail Stepanov Aug 24 '18 at 06:58
  • If I import sub will it print "I am for test" on the sys.stdout? @MikhailStepanov – Raja Aug 24 '18 at 07:36
  • It works for me (py3.7, ubuntu 16), I've got "I am for test" printed. It's a basic language feature, guess it will worksl for you too – Mikhail Stepanov Aug 24 '18 at 07:44

0 Answers0